Table

General


Table
A table displays rows of data.

#Title 1Title 2Title 3
1JasonJason 2Jason 3
2PrayPray 2Pray 3
3NemeNeme 2
Show Code
import React, { useRef, useState } from "react";
import {
  Table,
  TableHead,
  TableRow,
  TableBody,
  TableCell,
} from "pocko-ui/Table";
 
<Table tableClassName="table table-hover table-bordered table-striped align-middle">
  <TableHead>
    <TableRow>
      <TableCell>#</TableCell>
      <TableCell>Title 1</TableCell>
      <TableCell>Title 2</TableCell>
      <TableCell>Title 3</TableCell>
    </TableRow>
  </TableHead>
 
  <TableBody>
    <TableRow>
      <TableCell>1</TableCell>
      <TableCell>Jason</TableCell>
      <TableCell>Jason 2</TableCell>
      <TableCell>Jason 3</TableCell>
    </TableRow>
 
    <TableRow>
      <TableCell>2</TableCell>
      <TableCell>Pray</TableCell>
      <TableCell>Pray 2</TableCell>
      <TableCell>Pray 3</TableCell>
    </TableRow>
 
    <TableRow>
      <TableCell>3</TableCell>
      <TableCell colSpan="2">Neme</TableCell>
      <TableCell>Neme 2</TableCell>
    </TableRow>
  </TableBody>
</Table>

Cell Auto Width

The table displays data rows with adaptive width based on content

#Title 1Title 2Title 3
1JasonJason 2Jason 3-Jason 3-Jason 3-Jason 3
2PrayPray 2Pray 3
3NemeNeme 2
Show Code
import React, { useRef, useState } from "react";
import {
  Table,
  TableHead,
  TableRow,
  TableBody,
  TableCell,
} from "pocko-ui/Table";
 
<Table cellAutoWidth tableClassName="table table-hover table-bordered table-striped align-middle">
  <TableHead>
    <TableRow>
      <TableCell>#</TableCell>
      <TableCell>Title 1</TableCell>
      <TableCell>Title 2</TableCell>
      <TableCell>Title 3</TableCell>
    </TableRow>
  </TableHead>
 
  <TableBody>
    <TableRow>
      <TableCell>1</TableCell>
      <TableCell>Jason</TableCell>
      <TableCell>Jason 2</TableCell>
      <TableCell>Jason 3</TableCell>
    </TableRow>
 
    <TableRow>
      <TableCell>1</TableCell>
      <TableCell>Jason</TableCell>
      <TableCell>Jason 2</TableCell>
      <TableCell>Jason 3-Jason 3-Jason 3-Jason 3</TableCell>
    </TableRow>
 
    <TableRow>
      <TableCell>3</TableCell>
      <TableCell colSpan="2">Neme</TableCell>
      <TableCell>Neme 2</TableCell>
    </TableRow>
  </TableBody>
</Table>

Sticky Table Headers

#Title 1Title 2Title 3
1JasonJason 2Jason 3-Jason 3-Jason 3-Jason 3
2PrayPray 2Pray 3
3NemeNeme 2
Show Code [Index.scss]
 
.table_className {
  table-layout: auto;
  width: max-content;
  min-width: 100%;
}
 
.to-table-responsive_iframe {
  overflow: auto;
  width: 100%;
  position: relative;
 
  &::-webkit-scrollbar {
    height: 15px;
    width: 10px;
    background-color: #e4e4e4;
  }
 
  &::-webkit-scrollbar-thumb {
    background-color: #ccc;
    border-radius: 10px;
 
    &:hover {
      background-color: #aaa;
    }
  }
}
 
 
Show Code [Index.tsx]
import React, { useRef, useState } from "react";
import {
  Table,
  TableHead,
  TableRow,
  TableBody,
  TableCell,
} from "pocko-ui/Table";
import './Index.scss'
 
<div className="mt-4" style={{height: '100px', overflow: 'scroll'}}>  
  <Table cellAutoWidth tableClassName="table table-hover table-bordered table-striped align-middle">
    <TableHead>
      <TableRow>
        <TableCell>#</TableCell>
        <TableCell>Title 1</TableCell>
        <TableCell>Title 2</TableCell>
        <TableCell>Title 3</TableCell>
      </TableRow>
    </TableHead>
 
    <TableBody>
      <TableRow>
        <TableCell>1</TableCell>
        <TableCell>Jason</TableCell>
        <TableCell>Jason 2</TableCell>
        <TableCell>Jason 3</TableCell>
      </TableRow>
 
      <TableRow>
        <TableCell>1</TableCell>
        <TableCell>Jason</TableCell>
        <TableCell>Jason 2</TableCell>
        <TableCell>Jason 3-Jason 3-Jason 3-Jason 3</TableCell>
      </TableRow>
 
      <TableRow>
        <TableCell>3</TableCell>
        <TableCell colSpan="2">Neme</TableCell>
        <TableCell>Neme 2</TableCell>
      </TableRow>
    </TableBody>
  </Table>
</div>

Row selection (multiple rows)

namefriendcondition
OIE19010OIE19022PERFECT
OIE19022OIE19061PERFECT
OIE19061SWE19048PERFECT
SWE19048OIE19010PERFECT
Show Code
import React, { useRef, useState } from "react";
import {
  Table,
  TableHead,
  TableRow,
  TableBody,
  TableCell,
} from "pocko-ui/Table";
 
import Checkbox from "pocko-ui/Checkbox"; //other components
 
const testData: any = [
  { id: "1", name: "OIE19010", friend: "OIE19022", condition: "PERFECT" },
  { id: "2", name: "OIE19022", friend: "OIE19061", condition: "PERFECT" },
  { id: "3", name: "OIE19061", friend: "SWE19048", condition: "PERFECT" },
  { id: "4", name: "SWE19048", friend: "OIE19010", condition: "PERFECT" },
];
 
const [tableData, setTableData] = useState<any[]>([]);
 
// 多选
function handleSelectAll() {
  const isAllSelected = testData.every((item) =>
    tableData.some((selectedItem: any) => selectedItem.id == item.id)
  );
 
  if (isAllSelected) {
    setTableData([]);
  } else {
    const unselectedItems = testData.filter(
      (item: any) => !tableData.some((selectedItem) => selectedItem.id == item.id)
    );
 
    setTableData((prevChoose: any) => [...prevChoose, ...unselectedItems]);
  }
}
 
// 单选
function isChoose(e: any, val: any) {
  const index = tableData.findIndex((item: any) => item.id == val.id);
 
  if (index != -1) {
    const updatedChoose = [...tableData];
    updatedChoose.splice(index, 1);
 
    setTableData(updatedChoose);
  } else {
    setTableData((prevChoose: any) => [...prevChoose, val]);
  }
}
 
return (
  <>
    <div className="mt-4 to-table-responsive_iframe">
      <Table tableClassName="table table-hover table-bordered table-striped align-middle">
        <TableHead className="sticky-top bg-white">
          <TableRow>
            <TableCell>
              <Checkbox
                wrapperClassName="mb-0"
                onChange={handleSelectAll}
                checked={tableData.length == testData.length}
              />
            </TableCell>
            <TableCell>name</TableCell>
            <TableCell>friend</TableCell>
            <TableCell>condition</TableCell>
          </TableRow>
        </TableHead>
 
        <TableBody>
          {testData.map((v: any, i: any) => {
            return (
              <TableRow key={i}>
                <TableCell>
                  <Checkbox
                    value={v}
                    wrapperClassName="mb-0"
                    onChange={(e: any, val: any) => {
                      isChoose(e, val);
                    }}
                    checked={tableData.some(
                      (selectedItem: any) => selectedItem.id == v.id
                    )}
                  />
                </TableCell>
                <TableCell>{v.name}</TableCell>
                <TableCell>{v.friend}</TableCell>
                <TableCell>{v.condition}</TableCell>
              </TableRow>
            );
          })}
        </TableBody>
      </Table>
    </div>
  </>
);

API


You could specify all remaining properties defined and all synthetic events from React on all components listed below. such as tabIndex, style, id, data-xxx, onClick, onMouseEnter, onMouseLeave, and so on.

  • <Table />
  • <TableHead />
  • <TableBody />
  • <TableRow />
  • <TableCell />
Table
import { Table } from 'pocko-ui/Table';
PropertyTypeDefaultDescriptionRequired
tableWrapperClassNamestring-The style name of the outer div of the table.-
tableClassNamestring'table'The style name of the table.-
cellAutoWidthboolean-Whether the table adjusts its width automatically.-
hoverbooleantrueTo enable a hover state on table rows.-
borderedbooleantrueWhether the table has borders.-
childrenReact.ReactNode-The content inside the table.
TableHead
import { TableHead } from 'pocko-ui/Table';
PropertyTypeDefaultDescriptionRequired
classNamestring-The style name of the table head.-
refReact.ForwardedRef-It is the return element of this component.-
TableBody
import { TableBody } from 'pocko-ui/Table';
PropertyTypeDefaultDescriptionRequired
classNamestring-The style name of the table body.-
refReact.ForwardedRef-It is the return element of this component.-
TableRow
import { TableRow } from 'pocko-ui/Table';
PropertyTypeDefaultDescriptionRequired
classNamestring-The style name of the table body.-
refstring-It is the return element of this component.-
activeboolean-Whether this cell row is highlight.-
activeNamestring-The style name of the table row highlight.-
TableCell
import { TableCell } from 'pocko-ui/Table';
PropertyTypeDefaultDescriptionRequired
classNamestring-The style name of the table body.-
activeboolean-Whether this cell row is highlight.-
activeNamestring-The style name of the table row highlight.-
colSpannumber-The value represents the number of columns to span.-
scope"col" | "row" | "colgroup" | "rowgroup"-The scope attribute specifies whether a header cell is a header for a column, row, or group of columns or rows.-
onClickfunction-Click on the callback function of the cell.-