สถานะของตาราง
TableState
One row across all columns for a table that can't show rows: `no-results` when the search or filters match nothing, `error` when the rows failed to load. Say what happened, what to do next, and give the action. Keep the filter bar and chips on screen, and hide the pager and the total.
แถวเดียวเต็มความกว้างในเนื้อ DataTable เมื่อตารางแสดงแถวไม่ได้: ไม่พบรายการที่ตรงกับคำค้นหาหรือตัวกรอง หรือโหลดไม่สำเร็จ หน้าจอไม่ใช้ TableState เอง DataTable วาดแถวไม่พบรายการเมื่อคำค้นหาหรือตัวกรองไม่ตรงกับแถวใด และแถวโหลดไม่สำเร็จเมื่อส่ง status error กับ error ถ้ายังไม่มีข้อมูลเลยตั้งแต่แรก ส่ง empty ระหว่างโหลดส่ง status loading
import { TableState } from "@bsru/ui";ตัวอย่าง
DataTable ที่ค้นหาไม่พบ
เมื่อคำค้นหาหรือตัวกรองไม่ตรงกับแถวใด ตารางวาดแถวไม่พบรายการ พร้อมปุ่มล้างการค้นหาและเงื่อนไข ลองกดปุ่มนั้น แถวทั้งหมดจะกลับมา
พบ 0 คำขอ
ไม่พบรายการที่ตรงกับ "PR-9999" ลองใช้คำค้นอื่น หรือล้างการค้นหาและเงื่อนไข | ||||
const columns: DataColumn<Request>[] = [
{ key: "no", label: "เลขที่" },
{ key: "item", label: "รายการ" },
{ key: "unit", label: "หน่วยงาน", hideOnPhone: true },
{ key: "amount", label: "วงเงิน (บาท)", kind: "money" },
{
key: "status",
label: "สถานะ",
kind: "badge",
badges: {
draft: { tone: "draft", label: "ร่าง", icon: Pencil },
pending: { tone: "pending", label: "รออนุมัติ", icon: Clock },
approved: { tone: "approved", label: "อนุมัติแล้ว", icon: Check },
rejected: { tone: "rejected", label: "ไม่อนุมัติ", icon: X },
},
},
];
const [q, setQ] = useState("PR-9999");
<DataTable
caption="คำขอซื้อขอจ้าง"
columns={columns}
rows={rows}
rowKey="no"
search={{ placeholder: "ค้นหาเลขที่หรือรายการ", value: q, onChange: setQ }}
noun="คำขอ"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| caption* จำเป็น | "คำขอซื้อขอจ้าง" | string | Names the table for screen readers, such as คำขอซื้อขอจ้าง. |
| columns* จำเป็น | รายการ 5 รายการ[
{ key: "no", label: "เลขที่" },
{ key: "item", label: "รายการ" },
{ key: "unit", label: "หน่วยงาน", hideOnPhone: true },
{ key: "amount", label: "วงเงิน (บาท)", kind: "money" },
{
key: "status",
label: "สถานะ",
kind: "badge",
badges: {
draft: { tone: "draft", label: "ร่าง", icon: Pencil },
pending: { tone: "pending", label: "รออนุมัติ", icon: Clock },
approved: { tone: "approved", label: "อนุมัติแล้ว", icon: Check },
rejected: { tone: "rejected", label: "ไม่อนุมัติ", icon: X },
},
},
] | readonly DataColumn<R>[] | — |
| rows* จำเป็น | รายการ 5 รายการ[
{
no: "PR-2570-0001",
item: "ครุภัณฑ์คอมพิวเตอร์โน้ตบุ๊ก 5 เครื่อง",
unit: "หน่วยงาน ก",
amount: 150000,
status: "pending",
},
{
no: "PR-2570-0002",
item: "จ้างซ่อมเครื่องปรับอากาศ",
unit: "หน่วยงาน ข",
amount: 43500,
status: "approved",
},
{
no: "PR-2570-0003",
item: "วัสดุสำนักงาน",
unit: "หน่วยงาน ค",
amount: 19500,
status: "approved",
},
{
no: "PR-2570-0004",
item: "โต๊ะและเก้าอี้ห้องเรียน",
unit: "หน่วยงาน ก",
amount: 88000,
status: "draft",
},
{
no: "PR-2570-0005",
item: "ค่าเช่าซอฟต์แวร์",
unit: "หน่วยงาน ข",
amount: 64000,
status: "rejected",
},
] | readonly R[] | All rows. With total set, only the rows of the current page. |
| rowKey* จำเป็น | "no" | keyof R & string | The field that identifies a row, such as no. Selection and onOpen use its value. |
| search | ออบเจกต์ 3 ฟิลด์{
placeholder: "ค้นหาเลขที่หรือรายการ",
value: "PR-9999",
onChange: () => {},
} | TableSearch | false | The search box. Without total, the table searches the searchable columns itself, and shows the box even when this is left out, with ค้นหา (Search) as its placeholder. With total the core API pages the list, so pass a search with onChange to get a box. false hides it. A table with paged false shows one only when this is passed. |
| noun | "คำขอ" | string | The record name in the result count, such as รายการ or คำขอ. |
| onSortChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: TableSort) => void | Hears every sort change. Go back to page 1. |
| onPageChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: number) => void | — |
| onPageSizeChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: number) => void | — |
| onOpen | ไม่ได้ส่งในตัวอย่างนี้ | (row: R) => void | Opens a row, from the value in an opens column and a quick-view button. Use for a drawer. |
| openHref | ไม่ได้ส่งในตัวอย่างนี้ | (row: R) => string | The URL of a row's record page. The value in an opens column becomes a link. |
| openLabel | ไม่ได้ส่งในตัวอย่างนี้ | (row: R) => string | The quick-view button's accessible name for a row. ดูรายละเอียด and the row key by default. |
DataTable ที่โหลดไม่สำเร็จ
ส่ง status error และ error ที่บอกสิ่งที่เกิดขึ้น สิ่งที่ทำต่อได้ และรหัสอ้างอิง (Correlation ID) จาก core API ตารางวาดแถวนี้เต็มความกว้าง onRetry เพิ่มปุ่มลองอีกครั้ง
โหลดรายการไม่สำเร็จ
โหลดรายการไม่สำเร็จ ระบบไม่ตอบสนองในขณะนี้ ลองอีกครั้ง หากยังไม่ได้ แจ้งผู้ดูแลระบบพร้อมรหัสอ้างอิง รหัสอ้างอิง | ||||
const columns: DataColumn<Request>[] = [
{ key: "no", label: "เลขที่" },
{ key: "item", label: "รายการ" },
{ key: "unit", label: "หน่วยงาน", hideOnPhone: true },
{ key: "amount", label: "วงเงิน (บาท)", kind: "money" },
{
key: "status",
label: "สถานะ",
kind: "badge",
badges: {
draft: { tone: "draft", label: "ร่าง", icon: Pencil },
pending: { tone: "pending", label: "รออนุมัติ", icon: Clock },
approved: { tone: "approved", label: "อนุมัติแล้ว", icon: Check },
rejected: { tone: "rejected", label: "ไม่อนุมัติ", icon: X },
},
},
];
<DataTable
caption="คำขอซื้อขอจ้าง"
columns={columns}
rows={rows}
rowKey="no"
search={{ placeholder: "ค้นหาเลขที่หรือรายการ" }}
status="error"
error={{
title: "โหลดรายการไม่สำเร็จ",
body: "ระบบไม่ตอบสนองในขณะนี้ ลองอีกครั้ง หากยังไม่ได้ แจ้งผู้ดูแลระบบพร้อมรหัสอ้างอิง",
correlationId: "0190b1c2-7d3e-7c41-9a55-3f0e8a11d201",
onRetry: reload,
}}
noun="คำขอ"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| caption* จำเป็น | "คำขอซื้อขอจ้าง" | string | Names the table for screen readers, such as คำขอซื้อขอจ้าง. |
| columns* จำเป็น | รายการ 5 รายการ[
{ key: "no", label: "เลขที่" },
{ key: "item", label: "รายการ" },
{ key: "unit", label: "หน่วยงาน", hideOnPhone: true },
{ key: "amount", label: "วงเงิน (บาท)", kind: "money" },
{
key: "status",
label: "สถานะ",
kind: "badge",
badges: {
draft: { tone: "draft", label: "ร่าง", icon: Pencil },
pending: { tone: "pending", label: "รออนุมัติ", icon: Clock },
approved: { tone: "approved", label: "อนุมัติแล้ว", icon: Check },
rejected: { tone: "rejected", label: "ไม่อนุมัติ", icon: X },
},
},
] | readonly DataColumn<R>[] | — |
| rows* จำเป็น | [] | readonly R[] | All rows. With total set, only the rows of the current page. |
| rowKey* จำเป็น | "no" | keyof R & string | The field that identifies a row, such as no. Selection and onOpen use its value. |
| search | { placeholder: "ค้นหาเลขที่หรือรายการ" } | TableSearch | false | The search box. Without total, the table searches the searchable columns itself, and shows the box even when this is left out, with ค้นหา (Search) as its placeholder. With total the core API pages the list, so pass a search with onChange to get a box. false hides it. A table with paged false shows one only when this is passed. |
| status | "error" | "ready" | "loading" | "error" ค่าที่ใช้ได้: "ready""loading""error" | loading shows placeholder rows, error shows the error. ready by default. |
| error | ออบเจกต์ 4 ฟิลด์{
title: "โหลดรายการไม่สำเร็จ",
body: "ระบบไม่ตอบสนองในขณะนี้ ลองอีกครั้ง หากยังไม่ได้ แจ้งผู้ดูแลระบบพร้อมรหัสอ้างอิง",
correlationId: "0190b1c2-7d3e-7c41-9a55-3f0e8a11d201",
onRetry: () => {},
} | TableError | What to say when status is error. |
| noun | "คำขอ" | string | The record name in the result count, such as รายการ or คำขอ. |
| onSortChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: TableSort) => void | Hears every sort change. Go back to page 1. |
| onPageChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: number) => void | — |
| onPageSizeChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: number) => void | — |
| onOpen | ไม่ได้ส่งในตัวอย่างนี้ | (row: R) => void | Opens a row, from the value in an opens column and a quick-view button. Use for a drawer. |
| openHref | ไม่ได้ส่งในตัวอย่างนี้ | (row: R) => string | The URL of a row's record page. The value in an opens column becomes a link. |
| openLabel | ไม่ได้ส่งในตัวอย่างนี้ | (row: R) => string | The quick-view button's accessible name for a row. ดูรายละเอียด and the row key by default. |
แถวไม่พบรายการที่ DataTable วาด
TableState ตัวเดียว ตามที่ DataTable วาดเมื่อค้นหาไม่พบ บอกคำที่ค้นหาถ้ามี และให้ปุ่มล้างคำค้นหาและตัวกรองทั้งหมด colSpan เท่ากับจำนวนคอลัมน์
| เลขที่ | รายการ | หน่วยงาน | วงเงิน (บาท) | สถานะ |
|---|---|---|---|---|
ไม่พบรายการที่ตรงกับเงื่อนไข ไม่พบ "PR-9999" ลองคำอื่นหรือล้างเงื่อนไขบางข้อ | ||||
<TableState
kind="no-results"
colSpan={5}
title="ไม่พบรายการที่ตรงกับเงื่อนไข"
body={"ไม่พบ \"PR-9999\" ลองคำอื่นหรือล้างเงื่อนไขบางข้อ"}
action={<Button label="ล้างเงื่อนไขทั้งหมด" onClick={handleClick} />}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "no-results" | "no-results" | "error" ค่าที่ใช้ได้: "no-results""error" | no-results when nothing matches the search or filters, error when the rows failed to load. |
| colSpan* จำเป็น | 5 | number | The number of columns in the table, so the row spans all of them. |
| title* จำเป็น | "ไม่พบรายการที่ตรงกับเงื่อนไข" | string | What happened, in a short sentence. |
| body* จำเป็น | "ไม่พบ \"PR-9999\" ลองคำอื่นหรือล้างเงื่อนไขบางข้อ" | string | What to do next. |
| action | <Button label="ล้างเงื่อนไขทั้งหมด" onClick={handleClick} /> | ReactNode | The button, such as Clear all filters or Try again. |
แถวโหลดไม่สำเร็จที่ DataTable วาด
TableState ตัวเดียว ตามที่ DataTable วาดจาก error ข้อผิดพลาดที่ไม่คาดคิดต้องแสดงรหัสอ้างอิง (Correlation ID) จาก core API เพื่อให้ผู้ดูแลระบบตามได้ และมีปุ่มลองอีกครั้ง
| เลขที่ | รายการ | หน่วยงาน | วงเงิน (บาท) | สถานะ |
|---|---|---|---|---|
โหลดรายการไม่สำเร็จ ระบบไม่ตอบสนองในขณะนี้ ลองอีกครั้ง หากยังไม่ได้ แจ้งผู้ดูแลระบบพร้อมรหัสอ้างอิง รหัสอ้างอิง | ||||
<TableState
kind="error"
colSpan={5}
title="โหลดรายการไม่สำเร็จ"
body="ระบบไม่ตอบสนองในขณะนี้ ลองอีกครั้ง หากยังไม่ได้ แจ้งผู้ดูแลระบบพร้อมรหัสอ้างอิง"
correlationId="0190b1c2-7d3e-7c41-9a55-3f0e8a11d201"
action={<Button label="ลองอีกครั้ง" icon={RotateCw} onClick={handleClick} />}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "error" | "no-results" | "error" ค่าที่ใช้ได้: "no-results""error" | no-results when nothing matches the search or filters, error when the rows failed to load. |
| colSpan* จำเป็น | 5 | number | The number of columns in the table, so the row spans all of them. |
| title* จำเป็น | "โหลดรายการไม่สำเร็จ" | string | What happened, in a short sentence. |
| body* จำเป็น | "ระบบไม่ตอบสนองในขณะนี้ ลองอีกครั้ง หากยังไม่ได้ แจ้งผู้ดูแลระบบพร้อมรหัสอ้างอิง" | string | What to do next. |
| correlationId | "0190b1c2-7d3e-7c41-9a55-3f0e8a11d201" | string | For an unexpected error: the Correlation ID, so support can trace it. The frontend standard requires it. |
| action | <Button label="ลองอีกครั้ง" icon={RotateCw} onClick={handleClick} /> | ReactNode | The button, such as Clear all filters or Try again. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "no-results" | "error" | — | no-results when nothing matches the search or filters, error when the rows failed to load. |
| colSpan* จำเป็น | number | — | The number of columns in the table, so the row spans all of them. |
| title* จำเป็น | string | — | What happened, in a short sentence. |
| body* จำเป็น | string | — | What to do next. |
| correlationId | string | — | For an unexpected error: the Correlation ID, so support can trace it. The frontend standard requires it. |
| action | ReactNode | — | The button, such as Clear all filters or Try again. |
ชนิดข้อมูล
DataColumn
One column of a DataTable. The table reads the value from each row by key, and formats it by kind.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| key* จำเป็น | keyof R & string | — | The row field to show. It also names the column when sorting, and in a server query. |
| label* จำเป็น | string | — | The heading, already in the screen language. At phone width it labels the value in each row. |
| kind | ColumnKind ค่าที่ใช้ได้: "text""number""money""date""badge" | — | text by default. A text value may be a string or a Text pair. number and money take numbers, and money shows two decimals. date takes an ISO date such as 2026-11-02. badge looks the value up in badges. |
| sortable | boolean | — | People can sort by this column. In a table that has every row and pages them, every column sorts unless this is false. With total the core API sorts, so only columns with true sort, and the screen handles onSortChange. With paged false, a record's lines keep their order unless this is true. |
| searchable | boolean | — | The search box looks in this column. Text columns are searched unless this is false. |
| badges | Readonly<Record<string, BadgeStyle>> | — | For a badge column: how each value shows, such as { pending: { tone: "pending", label: "รออนุมัติ" } }. |
| sub | keyof R & string | — | Another row field, shown as a smaller second line, such as a vendor code under the vendor name. |
| avatar | boolean | — | Shows the value's initials in a round tile before it, such as for a person's or a unit's name. |
| opens | boolean | — | The value opens the row, through the table's onOpen or openHref. Usually the record number. |
| total | boolean | — | Adds the column up in the total row. For number and money columns. |
| unit | keyof R & string | — | Another row field, shown after a number or money value, such as เครื่อง after a quantity of 5. The value keeps its own formatting and alignment, and sorting and the total use the number alone. |
| hideOnPhone | boolean | — | Hides the column at phone width, where each row becomes a block. |
Text
A string in both languages. Thai comes first because it is the default.
type Text = readonly [th: string, en: string];ColumnKind
How a column shows its value.
type ColumnKind = "text" | "number" | "money" | "date" | "badge";BadgeStyle
How a badge column shows one value, such as pending.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| tone* จำเป็น | BadgeTone ค่าที่ใช้ได้: "draft""pending""approved""paid""request""rejected""line" | — | — |
| label* จำเป็น | string | — | The badge text, already in the screen language. |
| icon | LucideIcon | — | — |
BadgeTone
The state a badge reports. Each one has its own colour. line is for facts that aren't a state.
type BadgeTone = "draft" | "pending" | "approved" | "paid" | "request" | "rejected" | "line";TableSearch
The search box above the table.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| placeholder* จำเป็น | string | — | Says what can be searched, such as ค้นหาเลขที่หรือรายการ. It is also the box's accessible name. |
| value | string | — | The search text, when the screen keeps it. |
| onChange | (q: string) => void | — | Hears the search text. With total set, it comes after a short pause, ready for a query to the core API. |
TableFilters
The filters in the filter bar above the table, and a chip for each one in use.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| chips* จำเป็น | readonly FilterChip[] | — | The filters in use, one chip each. Removing one should go back to page 1. |
| onClearAll* จำเป็น | () => void | — | Called by Clear all. Remove every filter, including those without a chip, such as putting a Segmented back on its All choice, but keep the search text. |
| active | boolean | — | The filters on the bar are narrowing the list, even where a filter has no chip, such as a Segmented that isn't on its All choice. With no rows, the table then says nothing matches the filters, keeps the bar and offers Clear all, instead of showing empty as if nothing existed yet. True whenever chips has any. |
| panel | ReactNode | — | The filters, built from the library's fields, shown in one row on the left of the filter bar, with the search box on the right. Use compact fields: a Select, Combobox or DateInput with hideLabel and a placeholder that names the filter, and a Segmented with a count on each choice. |
FilterChip
One filter in use, shown as a chip.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| key* จำเป็น | string | — | Unique among the chips, such as status-pending. |
| label* จำเป็น | string | — | What the filter is, already in the screen language and formatted, such as a date range. |
| onRemove* จำเป็น | () => void | — | Called when the chip's remove button is pressed. Remove the filter and go back to page 1. |
TableSort
The column a table is sorted by, and which way.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| key* จำเป็น | string | — | — |
| dir* จำเป็น | SortDir ค่าที่ใช้ได้: "asc""desc" | — | — |
SortDir
The sort direction: asc for A to Z and smallest first, desc for the reverse.
type SortDir = "asc" | "desc";TableSelection
Row selection, with actions on the chosen rows.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| selected | readonly string[] | — | The chosen rows' keys, when the screen keeps them. |
| onChange | (keys: string[]) => void | — | Hears the chosen keys after every change. |
| actions* จำเป็น | readonly BulkAction[] | — | The actions on the chosen rows, such as Export. |
BulkAction
An action on the chosen rows, shown in the bar above the table while any row is chosen.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | string | — | The button text, already in the screen language. |
| icon | LucideIcon | — | — |
| tone | "primary" | "secondary" | "danger" | — | secondary by default. |
| onRun* จำเป็น | (keys: string[]) => void | — | Gets the keys of every chosen row, across all pages. |
TableError
What the table shows instead of rows when they failed to load.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| title* จำเป็น | string | — | What happened, in a short sentence. |
| body* จำเป็น | string | — | What to do next. |
| correlationId | string | — | The Correlation ID of the failed request, so support can trace it. |
| onRetry | () => void | — | Called by Try again. Leave out when trying again can't help. |
TableEmpty
What the table shows when nothing exists yet, before anyone has created a record.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| title* จำเป็น | string | — | Says there are no records yet, such as ยังไม่มีคำขอซื้อขอจ้าง. |
| body* จำเป็น | string | — | Says what will appear here, and how it gets here. |
| icon | LucideIcon | — | A Lucide icon for the kind of record. |
| action | TableAction | — | The button that creates the first record. Leave out for people who may not create one. |
TableAction
A button the table shows, such as Try again or Create request.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | string | — | The button text, already in the screen language. |
| icon | LucideIcon | — | — |
| tone | "primary" | "secondary" | "danger" | — | secondary by default. Use primary for the one main action, and danger for one that deletes. |
| onClick* จำเป็น | () => void | — | — |
ข้อควรปฏิบัติ
- ตารางคงแถบตัวกรองและชิปตัวกรองไว้ และซ่อนตัวแบ่งหน้าและยอดรวม
- บอกสิ่งที่เกิดขึ้นใน title และสิ่งที่ทำต่อได้ใน body ห้ามแสดงข้อความผิดพลาดทางเทคนิค
- ตารางบอกผลใน live region ของตัวเองด้วย เช่น พบ 0 คำขอ หรือ title ของ error
- หลังกดลองอีกครั้ง ให้ส่ง status loading จนกว่าข้อมูลจะมา
ซอร์สโค้ด: ui/src/data-table.tsx