mui-datatables

repository·master·Indexed 25 days ago

https://github.com/gregnb/mui-datatables

A responsive datatables component for React built for Material-UI (MUI). It provides advanced features including filtering, sorting, pagination, column resizing, and CSV exporting, with built-in responsive modes for mobile and tablet devices. Version 4.3.0 is compatible with MUI v5.

Tokens
8.7K
Snippets
9
Records
49
Agent score
82%

What's inside mui-datatables

  1. Install mui-datatables

    master

    Install the mui-datatables package using npm:

    npm install mui-datatables --save

    If your project does not already have Material UI (MUI) v5 and its icon pack installed, you must also install them:

    npm --save install @mui/material @emotion/react @emotion/styled @mui/icons-material

    npm install mui-datatables --save
    npm --save install @mui/material @emotion/react @emotion/styled @mui/icons-material
  2. Upgrade from mui-datatables v2 to v3

    master
    When upgrading to version 3.0.0, most existing options from version 2 remain functional but may trigger deprecation warnings in the console. To future-proof your implementation, you should migrate to the new API patterns described in the upgrade guide, specifically regarding responsive modes, sorting, fixed headers, and callback renaming.
  3. Manage expandable rows

    master

    Expandable rows allow users to view additional details for a specific row. Enable this via the options object.

    Key Options

    • expandableRows (boolean): Enables the feature.
    • expandableRowsHeader (boolean): Shows/hides the 'expand all/collapse all' header (default: true).
    • expandableRowsOnClick (boolean): If true, clicking the row itself triggers expansion. If false, only the expand icon works.
    • isRowExpandable (function): A custom function to determine if a specific row can be expanded. (dataIndex, expandedRows) => boolean.
    • renderExpandableRow (function): The component used to render the expanded content. (rowData, rowMeta) => React Component.
    • rowsExpanded (array): A user-provided array of dataIndex values representing currently expanded rows.
  4. Implement Remote Data (Server-side Pagination, Filtering, and Sorting)

    master

    To handle large datasets where pagination, filtering, and sorting are performed on a remote server, set serverSide: true in your options and use the onTableChange callback to trigger API requests.

    onTableChange provides the action and tableState to help you construct the correct request to your backend.

    const options = {
      serverSide: true,
      onTableChange: (action, tableState) => {
        this.xhrRequest('my.api.com/tableData', result => {
          this.setState({ data: result });
        });
      }
    };
  5. Configure responsive modes in mui-datatables

    master

    The responsive option no longer controls table height. Height is now managed via tableBodyHeight and tableBodyMaxHeight. The responsive option now accepts the following modes:

    • vertical: Replaces "stacked" mode. Handles long text strings and alignment correctly.
    • standard: Replaces "scrollMaxHeight" mode.
    • simple: A new design mode.

    Note: Legacy inputs like stacked or scrollMaxHeight still work but are being phased out.

  6. Configure selectToolbarPlacement

    master

    The selectToolbarPlacement option controls where the selection toolbar appears when rows are selected. Options include:

    • none: No select toolbar (equivalent to disableToolbarSelect: true).
    • replace (default): Replaces the normal toolbar.
    • above: Renders the select toolbar above the normal toolbar.
  7. Localize MUIDataTable text labels

    master

    You can override the default text labels used throughout the table by providing a textLabels object within your options. This allows for full localization of the component's UI text.

    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",
        },
      }
      ...
    }
  8. Configure CSV download options

    master

    The table includes a built-in download feature for CSV files. You can control its visibility and the output format via the options object.

    Visibility

    • download (boolean | string):
      • true: Button is visible and clickable.
      • false: Button is not visible.
      • 'disabled': Button is visible, but not clickable.

    Output Configuration

    • downloadOptions (object):
      • filename (string): Name of the exported file.
      • separator (string): Character used to separate values.
      • filterOptions (object):
        • useDisplayedColumnsOnly (boolean)
        • useDisplayedRowsOnly (boolean)
  9. Configure MUIDataTable options

    master

    The options prop is a comprehensive object used to customize the table's functionality. Key categories include:

    Data & Display

    • serverSide (boolean): Enable remote data source.
    • enableNestedDataAccess (string): Use a string (e.g., ".") to access nested data in columns (e.g., "phone.cell" accesses phone: { cell: '...' }).
    • expandableRows (boolean): Enable/disable expandable rows.
    • fixedHeader (boolean): Enable/disable a fixed header.
    • responsive (string): Choose between 'vertical' (default), 'standard', or 'simple' views.
    • tableBodyHeight / tableBodyMaxHeight (string): CSS strings for table height.

    Interaction & Events

    • onTableChange (function): Triggered when table state changes. (action: string, tableState: object) => void.
    • onRowClick (function): Triggered when a row is clicked. (rowData: string[], rowMeta: { dataIndex: number, rowIndex: number }) => void.
    • onFilterChange (function): Triggered when filters change.
    • onSearchChange (function): Triggered when search text changes.
    • onColumnSortChange (function): Triggered when a column is sorted.

    Custom Rendering

    • customRowRender (function): Override default row rendering. (data, dataIndex, rowIndex) => React Component.
    • customToolbar (function): Render a custom toolbar. ({displayData}) => React Component.
    • customFooter (function): Render a custom table footer.
  10. Enable nested data access

    master

    Nested data access (e.g., using dots in column names like phone.cell) is now disabled by default. To enable it, provide a non-empty string to the enableNestedDataAccess option.

    Example: enableNestedDataAccess: '.' allows a column named phone.cell to access data from { phone: { cell: "555-5555" } }.

  11. Use MUIDataTable for a simple table

    master

    To render a basic table, provide an array of strings for columns and a 2D array for data. You can pass an options object to configure features like filterType.

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