2.0.0-beta-43 • Published 5 years ago

mui-datatables-fork v2.0.0-beta-43

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

MUI-Datatables - Datatables for Material-UI

Build Status Coverage Status dependencies Status npm version

MUI-Datatables is a data tables component built on Material-UI V1. It comes with features like filtering, resizable + view/hide columns, search, export to CSV download, printing, selectable rows, pagination, and sorting. On top of the ability to customize styling on most views, there are two responsive modes "stacked" and "scroll" for mobile/tablet devices.

Install

npm install mui-datatables --save

Demo

Edit react-to-print

Usage

For a simple table:

import MUIDataTable from "mui-datatables";

const columns = ["Name", "Company", "City", "State"];

const data = [
 ["Joe James", "Test Corp", "Yonkers", "NY"],
 ["John Walsh", "Test Corp", "Hartford", "CT"],
 ["Bob Herm", "Test Corp", "Tampa", "FL"],
 ["James Houston", "Test Corp", "Dallas", "TX"],
];

const options = {
  filterType: 'checkbox',
};

<MUIDataTable
  title={"Employee List"}
  data={data}
  columns={columns}
  options={options}
/>

Or customize columns:

import MUIDataTable from "mui-datatables";

const columns = [
 {
  name: "Name",
  options: {
   filter: true,
   sort: true,
  }
 },
 {
  name: "Company",
  options: {
   filter: true,
   sort: false,
  }
 },
 {
  name: "City",
  options: {
   filter: true,
   sort: false,
  }
 },
 {
  name: "State",
  options: {
   filter: true,
   sort: false,
  }
 },
];

const data = [
 ["Joe James", "Test Corp", "Yonkers", "NY"],
 ["John Walsh", "Test Corp", "Hartford", "CT"],
 ["Bob Herm", "Test Corp", "Tampa", "FL"],
 ["James Houston", "Test Corp", "Dallas", "TX"],
];

const options = {
  filterType: 'checkbox',
};

<MUIDataTable
  title={"Employee List"}
  data={data}
  columns={columns}
  options={options}
/>

API

<MUIDataTable />

The component accepts the following props:

NameTypeDescription
titlearrayTitle used to caption table
columnsarrayColumns used to describe table. Must be either an array of simple strings or objects describing a column
dataarrayData used to describe table. Must be an array of strings or numbers
optionsobjectOptions used to describe table

Options:

NameTypeDefaultDescription
pagenumberUser provided starting page for pagination
countnumberUser provided override for total number of rows
serverSidebooleanfalseEnable remote data source
filterListarrayUser provided filter list
rowsSelectedarrayUser provided selected rows
filterTypestring'dropdown'Choice of filtering view. Options are "checkbox", "dropdown", or "multiselect"
textLabelsobjectUser provided labels to localize text
paginationbooleantrueEnable/disable pagination
selectableRowsbooleantrueEnable/disable row selection
resizableColumnsbooleanfalseEnable/disable resizable columns
customToolbarfunctionRender a custom toolbar
customToolbarSelectfunctionRender a custom selected rows toolbar. function(selectedRows, displayData, setSelectedRows) => void
customFooterfunctionRender a custom table footer. function(count, page, rowsPerPage, changeRowsPerPage, changePage) => string|React Component
customSortfunctionOverride default sorting with custom function. function(data: array, colIndex: number, order: string) => array
caseSensitivebooleanfalseEnable/disable case sensitivity for search
responsivestring'stacked'Enable/disable responsive table views. Options: 'stacked', 'scroll'
rowsPerPagenumber10Number of rows allowed per page
rowsPerPageOptionsarray10,15,20Options to provide in pagination for number of rows a user can select
rowHoverbooleantrueEnable/disable hover style over rows
fixedHeaderbooleantrueEnable/disable fixed header columns
sortFilterListbooleantrueEnable/disable alphanumeric sorting of filter lists
sortbooleantrueEnable/disable sort on all columns
filterbooleantrueShow/hide filter icon from toolbar
searchbooleantrueShow/hide search icon from toolbar
printbooleantrueShow/hide print icon from toolbar
downloadbooleantrueShow/hide download icon from toolbar
downloadOptionsobjectOptions to change the output of the CSV file. Default options: {filename: 'tableDownload.csv', separator: ','}
viewColumnsbooleantrueShow/hide viewColumns icon from toolbar
onRowsSelectfunctionCallback function that triggers when row(s) are selected. function(currentRowsSelected: array, allRowsSelected: array) => void
onRowsDeletefunctionCallback function that triggers when row(s) are deleted. function(rowsDeleted: array) => void
onRowClickfunctionCallback function that triggers when a row is clicked. function(rowData: string[], rowMeta: { dataIndex: number, rowIndex: number }) => void
onCellClickfunctionCallback function that triggers when a cell is clicked. function(colData: any, cellMeta: { colIndex: number, rowIndex: number }) => void
onChangePagefunctionCallback function that triggers when a page has changed. function(currentPage: number) => void
onChangeRowsPerPagefunctionCallback function that triggers when the number of rows per page has changed. function(numberOfRows: number) => void
onSearchChangefunctionCallback function that triggers when the search text value has changed. function(searchText: string) => void
onSearchOpenfunctionCallback function that triggers when the searchbox opens. function() => void
onFilterChangefunctionCallback function that triggers when filters have changed. `function(changedColumn: string, filterList: array) =>

void|**onColumnSortChange**|function||Callback function that triggers when a column has been sorted.function(changedColumn: string, direction: string) => void|**onColumnViewChange**|function||Callback function that triggers when a column view has been changed.function(changedColumn: string, action: string) => void|**onTableChange**|function||Callback function that triggers when table state has changed.function(action: string, tableState: object) => void`

Customize Columns

On each column object, you have the ability to customize columns to your liking with the 'options' property. Example:

const columns = [
 {
  name: "Name",
  options: {
   filter: true,
   sort: false
  }
 },
 ...
];

Column:

NameTypeDescription
NamestringName of column (This field is required)
optionsobjectOptions for customizing column

Column Options:

NameTypeDefaultDescription
displaystring'true'Display column in table. enum('true', 'false', 'excluded')
filterbooleantrueDisplay column in filter list
sortbooleantrueEnable/disable sorting on column
downloadbooleantrueDisplay column in CSV download file
customHeadRenderfunctionFunction that returns a string or React component. Used as display for column header. function(value, tableMeta, updateValue) => string|`
customBodyRenderfunctionFunction that returns a string or React component. Used as display data within all table cells of a given column. function(value, tableMeta, updateValue) => string|React Component Example

customHeadRender is called with these arguments:

function(columnMeta: {
  display: enum('true', 'false', 'excluded'),
  filter: bool,
  sort: bool,
  sortDirection: bool,
}, updateDirection: function)

customBodyRender is called with these arguments:

function(value: any, tableMeta: {
  rowIndex: number,
  columnIndex: number,
  columnData: array, // Columns Options object
  rowData: array, // Full row data
  tableData: array, Full table data
  tableState: {
    announceText: null|string,
    page: number,
    rowsPerPage: number,
    filterList: array,
    selectedRows: {
      data: array,
      lookup: object,
    },
    showResponsive: boolean,
    searchText: null|string,
  },
}, updateValue: function)

Customize Styling

Using Material-UI theme overrides will allow you to customize styling to your liking. First, determine which component you would want to target and then lookup the override classname. Let's start with a simple example where we will change the background color of a body cell to be red:

import React from "react";
import MUIDataTable from "mui-datatables";
import { createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';

class BodyCellExample extends React.Component {

  getMuiTheme = () => createMuiTheme({
    overrides: {
      MUIDataTableBodyCell: {
        root: {
          backgroundColor: "#FF0000"
        }
      }
    }
  })

  render() {

    return (
      <MuiThemeProvider theme={this.getMuiTheme()}>
        <MUIDataTable title={"ACME Employee list"} data={data} columns={columns} options={options} />
      </MuiThemeProvider>
    );

  }
}

Remote Data

If you are looking to work with remote data sets or handle pagination, filtering, and sorting on a remote server you can do that with the following options:

const options = {
  serverSide: true,
  onTableChange: (action, tableState) => {
    this.xhrRequest('my.api.com/tableData', result => {
      this.setState({ data: result });
    });
  }
};

To see an example Click Here

Localization

This package decided that the cost of bringing in another library to perform localizations would be too expensive. Instead the ability to override all text labels (which aren't many) is offered through the options property textLabels. The available strings:

const options = {
  ...
  textLabels: {
    body: {
      noMatch: "Sorry, no matching records found",
      toolTip: "Sort",
    },
    pagination: {
      next: "Next Page",
      previous: "Previous Page",
      rowsPerPage: "Rows per page:",
      displayRows: "of",
    },
    toolbar: {
      search: "Search",
      downloadCsv: "Download CSV",
      print: "Print",
      viewColumns: "View Columns",
      filterTable: "Filter Table",
    },
    filter: {
      all: "All",
      title: "FILTERS",
      reset: "RESET",
    },
    viewColumns: {
      title: "Show Columns",
      titleAria: "Show/Hide Table Columns",
    },
    selectedRows: {
      text: "rows(s) selected",
      delete: "Delete",
      deleteAria: "Delete Selected Rows",
    },
  }
  ...
}

License

The files included in this repository are licensed under the MIT license.

Thanks

Thank you to BrowserStack for providing the infrastructure that allows us to test in real browsers.