14.2.2 • Published 9 days ago

@rmwc/data-table v14.2.2

Weekly downloads
4,842
License
MIT
Repository
github
Last release
9 days ago

Data Tables

Data tables display sets of data.

  • Module @rmwc/data-table
  • Import styles:
    • Using CSS Loader
      • import '@rmwc/data-table/styles';
    • Or include stylesheets
      • '@material/data-table/dist/mdc.data-table.css'
      • '@rmwc/data-table/data-table.css'
      • '@rmwc/icon/icon.css'

Standard Table

The DataTable components are intended to be flexible, properly styled, Material compliant HTML tables. Because of the complexities of working with datasets (especially large ones), the DataTable component DOES NOT handle pagination, data fetching, sorting, or performance of long lists.

function Example() {
  const [sortDir, setSortDir] = React.useState(null);
  const items = [
    { item: 'Cookies', quantity: 25, price: '$2.90' },
    { item: 'Pizza', quantity: 50, price: '$1.25' },
    { item: 'Icecream', quantity: 10, price: '$2.35' }
  ];
  const sortedItems =
    sortDir === 1
      ? items.sort((a, b) => a.quantity - b.quantity)
      : items.sort((a, b) => b.quantity - a.quantity);
  return (
    <DataTable>
      <DataTableContent>
        <DataTableHead>
          <DataTableRow>
            <DataTableHeadCell>Item</DataTableHeadCell>
            <DataTableHeadCell
              alignEnd
              sort={sortDir}
              onSortChange={(sortDir) => {
                setSortDir(sortDir);
                console.log(sortDir);
              }}
            >
              Quantity (Click Me)
            </DataTableHeadCell>
            <DataTableHeadCell alignEnd>Unit price</DataTableHeadCell>
          </DataTableRow>
        </DataTableHead>
        <DataTableBody>
          {sortedItems.map((item) => (
            <DataTableRow key={item.item}>
              <DataTableCell>{item.item}</DataTableCell>
              <DataTableCell alignEnd>{item.quantity}</DataTableCell>
              <DataTableCell alignEnd>{item.price}</DataTableCell>
            </DataTableRow>
          ))}
        </DataTableBody>
      </DataTableContent>
    </DataTable>
  );
}

Scrollable / Sticky Rows and Columns

You can set a fixed sized for your table container to make it scrollable. Additionally, you can specify stickyRows or stickyColumns to affix rows or columns. Currently, only 1 row / column is supported but more may be supported in a future release.

function Example() {
  const [rows, setRows] = React.useState(0);
  const [cols, setCols] = React.useState(0);
  const sampleColumns = Array(7).fill(undefined);
  const sampleRows = Array(50).fill(undefined);

  return (
    <>
      <DataTable
        style={{ height: '300px', width: '375px' }}
        stickyRows={rows}
        stickyColumns={cols}
      >
        <DataTableContent>
          <DataTableHead>
            <DataTableRow>
              <DataTableHeadCell>Label</DataTableHeadCell>
              {sampleColumns.map((v, i) => (
                <DataTableHeadCell key={i}>Header</DataTableHeadCell>
              ))}
            </DataTableRow>
          </DataTableHead>
          <DataTableBody>
            {sampleRows.map((v, i) => (
              <DataTableRow key={i}>
                <DataTableCell>Label</DataTableCell>
                <DataTableCell>R{i} C1</DataTableCell>
                <DataTableCell>R{i} C2</DataTableCell>
                <DataTableCell>R{i} C3</DataTableCell>
                <DataTableCell>R{i} C4</DataTableCell>
                <DataTableCell>R{i} C5</DataTableCell>
                <DataTableCell>R{i} C6</DataTableCell>
                <DataTableCell>R{i} C7</DataTableCell>
              </DataTableRow>
            ))}
          </DataTableBody>
        </DataTableContent>
      </DataTable>

      <div className="doc-controls">
        <Select
          label="Sticky Rows"
          options={['0', '1']}
          value={String(rows)}
          onChange={(evt) => setRows(Number(evt.currentTarget.value))}
        />
        <Select
          label="Sticky Cols"
          options={['0', '1']}
          value={String(cols)}
          onChange={(evt) => setCols(Number(evt.currentTarget.value))}
        />
      </div>
    </>
  );
}

Form Controls

DataTables play nice with the rest of the RMWC form controls. You are responsible for scripting your own selection behavior.

function Example() {
  const [checked, setChecked] = React.useState({});
  const sampleRows = new Array(5).fill(undefined);

  return (
    <DataTable>
      <DataTableContent>
        <DataTableHead>
          <DataTableRow>
            <DataTableHeadCell hasFormControl>
              <Checkbox />
            </DataTableHeadCell>
            <DataTableHeadCell>Label</DataTableHeadCell>
            <DataTableHeadCell>Header</DataTableHeadCell>
            <DataTableHeadCell>Header</DataTableHeadCell>
            <DataTableHeadCell>Toggle</DataTableHeadCell>
          </DataTableRow>
        </DataTableHead>
        <DataTableBody>
          {sampleRows.map((v, i) => (
            <DataTableRow key={i} selected={checked[i]}>
              <DataTableCell hasFormControl>
                <Checkbox
                  checked={checked[i]}
                  onChange={(evt) => {
                    checked[i] = evt.currentTarget.checked;
                    setChecked({ ...checked });
                  }}
                />
              </DataTableCell>
              <DataTableCell>Label</DataTableCell>
              <DataTableCell>
                <Select
                  placeholder="--Select--"
                  options={['Cookies', 'Pizza', 'Icecream']}
                />
              </DataTableCell>
              <DataTableCell>R{i} C3</DataTableCell>
              <DataTableCell>
                <Switch />
              </DataTableCell>
            </DataTableRow>
          ))}
        </DataTableBody>
      </DataTableContent>
    </DataTable>
  );
}

Simplified Usage

If you just need to throw a table on the screen, you can pass an array of data to SimpleDataTable.

<SimpleDataTable
  getRowProps={(row) => {
    return row[1] > 100 ? { activated: true } : {};
  }}
  getCellProps={(cell, index, isHead) => {
    const props = { isNumeric: index > 0, style: undefined };

    return !isHead &amp;&amp; index === 2 &amp;&amp; !cell.includes('$')
      ? { ...props, style: { color: 'red' } }
      : props;
  }}
  headers={[['Item', 'Quantity', 'Value']]}
  data={[
    ['Cookies', 25, '$12.40'],
    ['Pizza', 11, '$10.43'],
    ['Icecream', 3, '1.43'],
    ['Candy', 72, '$22.45'],
    ['Cakes', 101, '$215.05'],
    ['Muffins', 3, '$5.97']
  ]}
/>

DataTable

The DataTable Component.

Props

NameTypeDescription
stickyColumnsnumberThe number of columns to affix to the side of the table when scrolling.
stickyRowsnumberThe number of rows to affix to the top of the table when scrolling.

DataTableRow

A row for the data table.

Props

NameTypeDescription
activatedbooleanStyles the row in a bolder activated state.
selectedbooleanStyles the row in a selected state.

DataTableCell

A cell for the DataTable

Props

NameTypeDescription
alignEndbooleanAlign content to the end of the cell.
alignMiddlebooleanAlign content to the middle of the cell.
alignStartbooleanAlign content to the start of the cell.
hasFormControlbooleanOptionally Remove padding on the cell for checkboxes, radios, and switches.
isNumericbooleanChanges alignment for numeric columns

DataTableHead

A header for the data table.

DataTableBody

A body for the data table.

DataTableHeadCell

A header cell for the data table.

Props

NameTypeDescription
alignEndbooleanAlign content to the end of the cell.
alignMiddlebooleanAlign content to the middle of the cell.
alignStartbooleanAlign content to the start of the cell.
childrenReactNodeChildren to pass to the cell.
hasFormControlbooleanOptionally Remove padding on the cell for checkboxes, radios, and switches.
isNumericbooleanChanges alignment for numeric columns
onSortChange(dir: null \| number) => voidA callback for when the sorting method changes. Null for not sorted, 1 for ascending, and -1 for descending.
sortnull \| numberMake the column sortable. Null for not sorted, 1 for ascending, and -1 for descending.

SimpleDataTable

A simple data table to render matrices.

Props

NameTypeDescription
dataany[][]Data to render.
getCellProps(cell: any[], index: number, isHead: boolean) => ObjectA function that allows you to return custom props for a cell.
getRowProps(row: any[], index: number, isHead: boolean) => ObjectA function that allows you to return custom props for a row.
headersany[][]Table headers to render.
stickyColumnsnumberThe number of columns to affix to the side of the table when scrolling.
stickyRowsnumberThe number of rows to affix to the top of the table when scrolling.
14.2.2

9 days ago

14.2.0

16 days ago

14.2.1

16 days ago

14.1.5

17 days ago

14.1.4

29 days ago

14.1.3

1 month ago

14.1.2

2 months ago

14.1.1

2 months ago

14.1.0

2 months ago

14.0.12

2 months ago

14.0.11

3 months ago

14.0.10

3 months ago

14.0.9

3 months ago

14.0.8

3 months ago

14.0.7

4 months ago

14.0.6

4 months ago

14.0.5

4 months ago

14.0.4

5 months ago

14.0.1-alpha.0

8 months ago

14.0.2-alpha.3

6 months ago

14.0.2-alpha.0

8 months ago

14.0.2-alpha.1

7 months ago

14.0.2-alpha.6

6 months ago

14.0.2-alpha.7

6 months ago

14.0.2-alpha.4

6 months ago

14.0.2-alpha.5

6 months ago

14.0.0

6 months ago

14.0.1

5 months ago

14.0.0-alpha.0

9 months ago

14.0.2

5 months ago

14.0.3

5 months ago

8.0.8

11 months ago

8.0.7

1 year ago

8.0.6

1 year ago

8.0.5

1 year ago

8.0.4

1 year ago

8.0.3

2 years ago

8.0.2

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

7.0.3

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

7.0.0

2 years ago

6.1.4

4 years ago

6.1.3

4 years ago

6.1.2

4 years ago

6.0.14

4 years ago

6.0.13

4 years ago

6.0.12

4 years ago

6.0.11

4 years ago

6.0.10

4 years ago

6.0.9

4 years ago

6.0.5

4 years ago

6.0.4

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

6.0.0-rc.4

4 years ago

6.0.0-rc.3

4 years ago

6.0.0-rc.2

4 years ago

6.0.0-rc.1

4 years ago

6.0.0-rc.0

4 years ago

6.0.0-alpha.16

4 years ago

6.0.0-alpha.14

4 years ago

6.0.0-alpha.15

4 years ago

6.0.0-alpha.13

4 years ago

6.0.0-alpha.12

4 years ago

6.0.0-alpha.11

4 years ago

6.0.0-alpha.7

4 years ago

5.7.2

4 years ago

6.0.0-alpha.5

4 years ago

6.0.0-alpha.6

4 years ago

6.0.0-alpha.3

4 years ago

6.0.0-alpha.1

4 years ago

5.7.0

5 years ago

5.6.0

5 years ago

5.5.2

5 years ago

5.5.1

5 years ago

5.5.0

5 years ago

5.4.3

5 years ago

5.4.2

5 years ago

5.4.1

5 years ago

5.4.0

5 years ago

5.3.1

5 years ago

5.3.0

5 years ago

5.2.2

5 years ago

5.2.1

5 years ago

5.2.0

5 years ago

5.2.0-alpha.0

5 years ago

5.1.8

5 years ago

5.1.7

5 years ago

5.1.6

5 years ago

5.1.5

5 years ago

5.1.4

5 years ago

5.1.3

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.30-rc.0

5 years ago

5.0.29-rc.0

5 years ago

5.0.28-rc.0

5 years ago

5.0.27-rc.0

5 years ago

5.0.26-rc.0

5 years ago

5.0.25-rc.0

5 years ago

5.0.24-rc.0

5 years ago

5.0.23-rc.0

5 years ago

5.0.23-alpha.0

5 years ago

5.0.22-alpha.0

5 years ago

5.0.21-alpha.0

5 years ago

5.0.20-alpha.0

5 years ago

5.0.19-alpha.0

5 years ago

5.0.18-alpha.0

5 years ago

5.0.17-alpha.0

5 years ago

5.0.16-alpha.0

5 years ago

5.0.15-alpha.0

5 years ago

5.0.14-alpha.0

5 years ago

5.0.13-alpha.0

5 years ago

5.0.12-alpha.0

5 years ago

5.0.8-alpha.0

5 years ago

5.0.7-alpha.0

5 years ago

5.0.6-alpha.0

5 years ago

5.0.5-alpha.0

5 years ago

5.0.4-alpha.0

5 years ago

5.0.3-alpha.0

5 years ago

5.0.2-alpha.0

5 years ago

5.0.1-alpha.0

5 years ago

5.0.0-alpha.0

5 years ago

4.0.6

5 years ago

4.0.5

5 years ago

4.0.4

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.11

5 years ago

3.0.10

5 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.0

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.0

6 years ago