项目作者: MicroDroid

项目描述 :
A fancy Materialize CSS datatable VueJS component.
高级语言: Vue
项目地址: git://github.com/MicroDroid/vue-materialize-datatable.git
创建时间: 2017-03-13T23:49:23Z
项目社区:https://github.com/MicroDroid/vue-materialize-datatable

开源协议:MIT License

下载


vue-materialize-datatable NPM version

A fancy Materialize CSS datatable VueJS component

BTC donations: 16dt5DdjGvduZ3KZcFrwsBA82qqfrcbeUQ

Screenshot

Demo

https://microdroid.github.io/vue-materialize-datatable/

Features

  • Sorting, with numerical sorting
  • Pagination
  • Localization
  • Fuzzy searching
  • Server searching
  • Excel export
  • Printing
  • Custom topbar buttons
  • Flexible data-from-row extractor
  • Follows the Material Design spec
  • Really, really efficient.. handles thousands of rows flawlessly
  • And much more..

Requirements

Installation

  1. npm i vue-materialize-datatable --save

You also need to include Material Design icons. You have two ways of doing this:

The first and the recommended way is loading via Google’s CDN, by adding this tag to your HTML

  1. <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Or this in your CSS/Sass files:

  1. @import url(http://fonts.googleapis.com/icon?family=Material+Icons);

Alternatively, you can use the NPM package and add the font to your bundle by:

  1. npm i material-design-icons-iconfont -S

and then include it in your Sass/CSS files

  1. @import "~material-design-icons-iconfont/dist/material-design-icons";

Usage

Include the component,

  1. import DataTable from "vue-materialize-datatable";

Then, register the component, however you like:

  1. {
  2. ...
  3. components: {
  4. ...
  5. "datatable": DataTable
  6. }
  7. }

And then.. use the component:

  1. <datatable></datatable>

Of course, code above will render garbage. Here are the props it accepts to render some sensible data:





























































































Prop name

Description

Example

title

The title of the datatable


  1. Todos // Name in top


columns

Columns



  1. [ // Array of objects
    {
    label: Name”, // Column name
    field: name”, // Field name from row
    // Use dot for nested props
    // Can be function with row as 1st param
    numeric: false,// Affects sorting
    html: false // Escapes output if false.
    }
    ];



rows

Rows



  1. [ // Array of objects
    {
    name: test’, // Whatever

    }
    ];



perPage

Numbers of rows per page


  1. [10, 20, 30, 40, 50] (default) // Results per page


defaultPerPage

Default rows per page


  1. 10 (default) // Default results per page, otherwise it will be the
    first value of perPage


initSortCol

Default column for sorting on component initialization


  1. -1 (default) // By default table is not sorted by any column


onClick

Function to execute on click


  1. console.log(‘hey’) // Function, row 1st param


clickable

Clickable rows. Will fire row-click event


  1. true (default) // Row is passed in the event payload


sortable

Cause column-click to sort


  1. true (default) // Whether sortable


searchable

Add fuzzy search functionality


  1. true (default) // Whether searchable


exactSearch

Disable fuzzy search


  1. true (default) // Whether only exact matches are returned


serverSearch

Server search is used to fetch data from server


  1. false (default) // If you wanna do server search then searchable and
    serverSearch must be true and use serverSearchFunc as callback.


serverSearchFunc

Function for search search


  1. function // For this searchSearch criteria is must.


paginate

Add footer next/prev. buttons


  1. true (default) // Whether paginated


exportable

Add button to export to Excel


  1. true (default) // Whether exportable


printable

Add printing functionality


  1. true (default) // Whether printable


customButtons

Custom buttons thingy



  1. // Array of objects
    [
    {
    hide: false, // Whether to hide the button
    icon: search”, // Materialize icon
    onclick: aFunc() // Click handler
    }
    ];


Localization

You can use the property locale to set the display language. Available languages:

  • en (English, default)
  • ar (Arabic)
  • es (Spanish)
  • cat (Catalan)
  • br (Brazilian Portuguese)
  • nl (Netherlands)
  • fr (French)
  • de (German)

You can very easily contribute a locale. Just clone locales/en.json and require in locales/index.js

React to click on row

The datatable will emit the row-click event if clickable is set to true (default).

The events payload will contain the row object, you can bind to the event like this:

  1. <datatable v-on:row-click="onRowClick"></datatable>
  2. <script>
  3. var app = new Vue({
  4. el: '#app',
  5. ...
  6. methods: {
  7. onRowClick: function (row) {
  8. //row contains the clicked object from `rows`
  9. console.log(row)
  10. }
  11. },
  12. })
  13. </script>
  14. ...

Rows per page

You can specify the options of rows per page with the perPage prop. The first value will be the default value and the array will be sorted, so you can put whatever number you want.

  1. <!-- The default value will be 100 -->
  2. <datatable :perPage="[100, 10, 25, 50, 500]"></datatable>

The options will be rendered as [10, 20, 50, 100, 500]

Rows per page

Otherwise, you can specify the default rows per page with the defaultPerPage prop.

  1. <!-- The default value will be 100 -->
  2. <datatable :perPage="[10, 25, 50, 100, 500]" :defaultPerPage="100"></datatable>

Row buttons

Alright actually this is a hack. We probably should’ve implemented actual support for this but for now, here’s an example on how to achieve something similar to the screenshot above:

  1. <datatable title="News" ...>
  2. <th slot="thead-tr">
  3. Actions
  4. </th>
  5. <template slot="tbody-tr" scope="props">
  6. <td>
  7. <button class="btn red darken-2 waves-effect waves-light compact-btn"
  8. @click="(e) => deleteItem(props.row, e)">
  9. <i class="material-icons white-text">delete</i>
  10. </button>
  11. </td>
  12. </template>
  13. </datatable>

Feel free to copy paste the code above, heh.