MUI-Datatables

使用 Material-UI 的 React 数据表。「 Datatables for React using Material-UI - https://www.material-ui-datatables.com 」

Github stars Tracking Chart

MUI-Datatables - Datatables for Material-UI

MUI-Datatables 是在 Material-UI 上构建的数据表组件。 它具有过滤、可调整大小+查看/隐藏列、搜索、导出到CSV下载、打印、可选行、可扩展行、分页和排序等功能。 除了在大多数视图上自定义样式的能力之外,还有两种响应模式“堆叠”和“滚动”用于移动/平板电脑设备。

安装

npm install mui-datatables --save

演示

Edit react-to-print

使用

对于一个简单的表:

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}
/>

或自定义列

import MUIDataTable from "mui-datatables";

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

const data = [
 { name: "Joe James", company: "Test Corp", city: "Yonkers", state: "NY" },
 { name: "John Walsh", company: "Test Corp", city: "Hartford", state: "CT" },
 { name: "Bob Herm", company: "Test Corp", city: "Tampa", state: "FL" },
 { name: "James Houston", company: "Test Corp", city: "Dallas", state: "TX" },
];

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

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

API

(恕删略)

许可

此存储库中包含的文件根据MIT许可证进行许可。

Overview

Name With Ownergregnb/mui-datatables
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 2)
PlatformWeb browsers
License:MIT License
Release Count93
Last Release Name4.3.0 (Posted on )
First Release Namev1.0.3 (Posted on 2018-01-01 15:09:18)
Created At2017-11-04 19:49:44
Pushed At2024-03-08 05:54:35
Last Commit At2023-01-03 18:53:42
Stargazers Count2.7k
Watchers Count40
Fork Count0.9k
Commits Count1k
Has Issues Enabled
Issues Count1442
Issue Open Count617
Pull Requests Count417
Pull Requests Open Count23
Pull Requests Close Count151
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

MUI-Datatables - Datatables for Material-UI

Build Status
NPM Downloads
Coverage Status
dependencies Status
npm version

MUI-Datatables is a data tables component built on Material-UI. It comes with features like filtering, resizable + view/hide columns, search, export to CSV download, printing, selectable rows, expandable rows, pagination, and sorting. On top of the ability to customize styling on most views, there are three responsive modes "stacked", "scrollMaxHeight", and "scrollFullHeight" 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",
  label: "Name",
  options: {
   filter: true,
   sort: true,
  }
 },
 {
  name: "company",
  label: "Company",
  options: {
   filter: true,
   sort: false,
  }
 },
 {
  name: "city",
  label: "City",
  options: {
   filter: true,
   sort: false,
  }
 },
 {
  name: "state",
  label: "State",
  options: {
   filter: true,
   sort: false,
  }
 },
];

const data = [
 { name: "Joe James", company: "Test Corp", city: "Yonkers", state: "NY" },
 { name: "John Walsh", company: "Test Corp", city: "Hartford", state: "CT" },
 { name: "Bob Herm", company: "Test Corp", city: "Tampa", state: "FL" },
 { name: "James Houston", company: "Test Corp", city: "Dallas", state: "TX" },
];

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

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

API

<MUIDataTable />

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 either an array containing objects of key/value pairs with values that are strings or numbers, or arrays of strings or numbers (Ex: data: [{"Name": "Joe", "Job Title": "Plumber", "Age": 30}, {"Name": "Jane", "Job Title": "Electrician", "Age": 45}] or data: [["Joe", "Plumber", 30], ["Jane", "Electrician", 45]]) Use of arbitrary objects as data is not supported, and is deprecated. Consider using ids and mapping to external object data in custom renderers instead e.g. const data = [{"Name": "Joe", "ObjectData": 123}] --> const dataToMapInCustomRender = { 123: { foo: 'bar', baz: 'qux', ... } }, options, object, Options used to describe table

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:, Name, Type, Description, :--:, :-----, :-----, name, string, Name of column (This field is required), label, string, Column Header Name override, options, object, Options for customizing column

Column Options:, Name, Type, Default, Description, :--:, :-----, :--, :-----, display, string, 'true', Display column in table. enum('true', 'false', 'excluded'), empty, boolean, false, This denotes whether the column has data or not (for use with intentionally empty columns), viewColumns, boolean, true, Allow user to toggle column visibility through 'View Column' list, filterList, array, Filter value list Example, filterOptions, {names, logic, display}, (These options affect the filter display and functionality from the filter dialog. To modify the filter chips that display after selecting filters, see customFilterListOptions) With filter options, it's possible to use custom names for the filter fields Example, custom filter logic Example, and custom rendering Example, customFilterListRender DEPRECATED (use customFilterListOptions), function, Function that returns a string or array of strings used as the chip label(s). function(value) => string OR arrayOfStrings Example, customFilterListOptions, {render: function, update: function}, (These options only affect the filter chips that display after filters are selected. To modify the filters themselves, see filterOptions) render returns a string or array of strings used as the chip label(s). function(value) => string OR arrayOfStrings, update returns a filterList (see above) allowing for custom filter updates when removing the filter chip Example, filter, boolean, true, Display column in filter list, filterType , string, 'dropdown', Choice of filtering view. Takes priority over global filterType option.enum('checkbox', 'dropdown', 'multiselect', 'textField', 'custom') Use 'custom' if you are supplying your own rendering via filterOptions., sort, boolean, true, Enable/disable sorting on column, searchable, boolean, true, Exclude/include column from search results, sortDirection, string, Set default sort order enum('asc', 'desc', 'none') (null option has been deprecated, use 'none' instead), print, boolean, true, Display column when printing, download, boolean, true, Display column in CSV download file, hint, string, Display hint icon with string as tooltip on hover., customHeadRender, function, Function that returns a string or React component. Used as display for column header. function(columnMeta, handleToggleColumn) => string| React Component, customBodyRender, function, Function 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, setCellProps, function, Is called for each cell and allows to you return custom props for this cell based on its data. function(cellValue: string, rowIndex: number, columnIndex: number) => object Example, setCellHeaderProps, function, Is called for each header cell and allows you to return custom props for the header cell based on its data. function(columnMeta: object) => object Example

customHeadRender is called with these arguments:

function(columnMeta: {
  customHeadRender: func,
  display: enum('true', 'false', 'excluded'),
  filter: boolean,
  sort: boolean,
  sortDirection: boolean,
  download: boolean,
  empty: boolean,
  index: number,
  label: string,
  name: string,
  print: boolean,
  searchable: boolean,
  viewColumns: boolean
}, handleToggleColumn: function(columnIndex))

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",
      columnHeaderTooltip: column => `Sort for ${column.label}`
    },
    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: "row(s) selected",
      delete: "Delete",
      deleteAria: "Delete Selected Rows",
    },
  }
  ...
}

Contributing

Thanks for taking an interest in the library and the github community!

The following commands should get you started:

npm i
npm run dev

open http://localhost:5050/ in browser

After you make your changes locally, you can run the test suite with npm test.

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.

To the top