Datatable for React using Material-UI
npm install material-datatable
npm install material-datatable --save
js
import MaterialDatatable from "material-datatable";
const columns = [
{
name: 'Name',
field: 'name',
options: {
width: 70,
},
},
{
name: 'Title',
field: 'title'
},
{
name: 'Location',
field: 'location',
options: {
width: 100,
},
},
{
name: 'Age',
field: 'age'
},
{
name: 'Salary',
field: 'salary'
},
{
name: 'Veeeeery loooooooooong naaaaaaaaaaame title',
field: 'salary',
options: {
headerNoWrap: true,
customBodyRender: (value, tableMeta, updateValue) => {
return ${value.name} (${value.title});
}
},
}
];
const data = [
{name: "Veeeeery loooooooooong naaaaaaaaaaame", title: "Title 1", location: "Location 1", age: 30, salary: 10},
{name: "Name 2", title: "Title 2", location: "Location 2", age: 31, salary: 11},
];
const options = {
filterType: 'checkbox',
};
title={"Employee List"}
data={data}
columns={columns}
options={options}
/>
`
Or customize columns:
`js
import MaterialDatatable from "material-datatable";
const columns = [
{
name: "Name",
field: "name",
options: {
filter: true,
sort: true,
}
},
{
name: "Company",
field: "company",
options: {
filter: true,
sort: false,
}
},
{
name: "City",
field: "city",
options: {
filter: true,
sort: false,
}
},
{
name: "State",
field: "state",
options: {
filter: true,
sort: false,
}
},
];
const data = [
{name: "Name 1", title: "Title 1", location: "Location 1", age: 30, salary: 10},
{name: "Name 2", title: "Title 2", location: "Location 2", age: 31, salary: 11},
];
const options = {
filterType: 'checkbox',
};
title={"Employee List"}
data={data}
columns={columns}
options={options}
/>
`
How to turn off Material UI Typography warnings
Warnings like:
`js
Warning: Material-UI: You are using the typography variant body2 which will be restyled in the next major release.
`
Can be turned off with follow code:
`js
const theme = createMuiTheme({
typography: {
useNextVariants: true
},
});
ReactDOM.hydrate(
,
document.getElementById('react-app')
`
https://github.com/mui-org/material-ui/issues/12741#issuecomment-428130592
Keep track and restore table settings (sorting, filtering, paging, search text)
Example
`js
let options = {
filter: true,
selectableRows: true,
usePaperPlaceholder: false,
filterType: 'multiselect',
responsive: 'stacked',
rowsPerPage: 10,
searchText: "22",
componentWillReceiveProps: true,
page: 0,
sortColumnIndex: 2,
sortColumnDirection: "desc",
filterList: [[], [], ["Location 2"], [], [], [], []],
onTableChange: (action, state) => this.onChange(state)
};
if (this.state.tableState !== undefined && this.state.tableState !== null) {
options.filterList = this.state.tableState.filterList;
options.searchText = this.state.tableState.searchText;
options.page = this.state.tableState.page;
options.rowsPerPage = this.state.tableState.rowsPerPage;
options.sortColumnDirection = this.state.tableState.sortColumnDirection;
options.sortColumnIndex = this.state.tableState.sortColumnIndex;
options.rowsSelected = this.state.tableState.rowsSelected;
}
return (
title={"ACME Employee list"}
data={data}
columns={columns}
options={options}
/>
);
`
API
#### <MaterialDatatable />
The component accepts the following props:
|Name|Type|Description
|:--:|:-----|:-----|
|title|array|Title used to caption table
|columns|array|Columns used to describe table. Must be either an array of simple strings or objects describing a column
|data|array|Data used to describe table. Must be an array of strings or numbers
|options|object|Options used to describe table
#### Options:
|Name|Type|Default|Description
|:--:|:-----|:--|:-----|
|page|number||User provided starting page for pagination
|count|number||User provided override for total number of rows
|serverSide|boolean|false|Enable remote data source
|usePaperPlaceholder|boolean|true|When false, component use React.Fragment as placeholder
|filterList|array||User provided filter list
|rowsSelected|array||User provided selected rows
|filterType |string|'dropdown'|Choice of filtering view. Options are "checkbox", "dropdown", or "multiselect"
|textLabels |object||User provided labels to localize text
|pagination|boolean|true|Enable/disable pagination
|componentWillReceiveProps|boolean|true|NOT RECOMMENDED. Enable/disable componentWillReceiveProps function. That option can be used if you subscribed on some event like 'onTableChange' and changing your state. It will prevent re-render of table component.
|selectableRows|boolean|true|Enable/disable row selection
|showSelectedRowsToolbar|boolean|true|When false hide extra toolbar for selected records
|rowCursorHand|boolean|false|When true show cursor pointer (hand) on table rows hover
|onlyOneRowCanBeSelected|boolean|false|When true only one row can be selected
|resizableColumns|boolean|false|Enable/disable resizable columns
|customToolbar|function||Render a custom toolbar
|customToolbarSelect|function||Render a custom selected rows toolbar
|customFooter|function||Render a custom table footer. function(count, page, rowsPerPage, changeRowsPerPage, changePage) => string| React Component
|caseSensitive |boolean|false|Enable/disable case sensitivity for search
|responsive|string|'stacked'|Enable/disable responsive table views. Options: 'stacked', 'scroll'
|rowsPerPage|number|10|Number of rows allowed per page
|rowsPerPageOptions|array|[10,15,20]|Options to provide in pagination for number of rows a user can select
|rowHover|boolean|true|Enable/disable hover style over rows
|sortFilterList|boolean|true|Enable/disable alphanumeric sorting of filter lists
|sort|boolean|true|Enable/disable sort on all columns
|sortColumnIndex|number|null| Set column number for initial sorting (0, n < columns.length). Should be used with sortColumnDirection.
|sortColumnDirection|string|null| Set directional for initial sorting ("asc", "desc"). Should be used with sortColumnIndex.
|filter|boolean|true|Show/hide filter icon from toolbar
|search|boolean|true|Show/hide search icon from toolbar
|searchText|string|""|Set search text
|print|boolean|true|Show/hide print icon from toolbar
|download|boolean|true|Show/hide download icon from toolbar
|downloadOptions|object||Options to change the output of the CSV file. Default options: {filename: 'tableDownload.csv', separator: ','}
|viewColumns|boolean|true|Show/hide viewColumns icon from toolbar
|onRowsSelect|function||Callback function that triggers when row(s) are selected. function(currentRowsSelected: array, allRowsSelected: array) => void
|onRowsDelete|function||Callback function that triggers when row(s) are deleted. function(rowsDeleted: array) => void
|onRowClick|function||Callback function that triggers when a row is clicked. function(rowObject: any, rowMeta: { dataIndex: number, rowIndex: number }) => void
|onCellClick|function||Callback function that triggers when a cell is clicked. function(colData: any, cellMeta: { colIndex: number, rowIndex: number }) => void
|onChangePage|function||Callback function that triggers when a page has changed. function(currentPage: number) => void
|onChangeRowsPerPage|function||Callback function that triggers when the number of rows per page has changed. function(numberOfRows: number) => void
|onSearchChange|function||Callback function that triggers when the search text value has changed. function(searchText: string) => void
|onFilterChange|function||Callback 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. If you going to change your component state and force re-render, you probably need turn off componentWillReceiveProps option.
#### Customize Columns
On each column object, you have the ability to customize columns to your liking with the 'options' property. Example:
`js
const columns = [
{
name: "Name",
field: "name",
options: {
width: 150,
headerNoWrap: true,
filter: true,
sort: false
}
},
...
];
`
#### Column:
|Name|Type|Description
|:--:|:-----|:-----|
|Name|string|Name of column (This field is required)
|Field|string|Field of object field (This field is required)
|options|object|Options for customizing column
#### Column Options:
|Name|Type|Default|Description
|:--:|:-----|:--|:-----|
|display|string|'true'|Display column in table. enum('true', 'false', 'excluded')
|filter|boolean|true|Display column in filter list
|sort|boolean|true|Enable/disable sorting on column
|width|number|null|Set width for column (header and data)
|headerNoWrap|boolean|false|No wrap for table header cell
|download|boolean|true|Display column in CSV download file
|customHeadRender|function||Function that returns a string or React component. Used as display for column header. function(rowDataObject, tableMeta, updateValue) => string|
customBodyRender|function||Function that returns a string or React component. Used as display data within all table cells of a given column. function(rowDataObject, tableMeta, updateValue) => string| React Component
customValue|function||Function that returns a string or number. Can be used just with customBodyRender. For example you added some React component like Link into the column, but still wants to use that column for filtering. function(rowDataObject) => string|number. Example https://codesandbox.io/s/lp8pvk1zk9
customSortValue|function||Function that returns a string or number. Return value for which will be used for sorting. For example you added some data like string "Fed 10, 2016" which provide create date into the column, but still wants to use that column for correct sorting by date. function(rowDataObject) => string|number Example https://codesandbox.io/s/lp8pvk1zk9
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:
`js
import React from "react";
import MaterialDatatable from "material-datatable";
import { createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
class BodyCellExample extends React.Component {
getMuiTheme = () => createMuiTheme({
overrides: {
MaterialDatatableBodyCell: {
root: {
backgroundColor: "#FF0000"
}
}
}
})
render() {
return (
);
}
}
`
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 });
});
}
};
`
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",
},
}
...
}
``