ข้ามไปยังเนื้อหา

หัวคอลัมน์เรียงลำดับ

SortHeader

A column heading that sorts the table. aria-sort is set only on the column in use.

หัวคอลัมน์ที่กดเพื่อเรียงลำดับใน DataTable หน้าจอไม่ใช้ SortHeader เอง แต่ใส่ sortable: true ในคอลัมน์ของ DataTable แล้วตารางวาดหัวคอลัมน์นี้ เรียงแถว และกลับไปหน้า 1 ให้เอง ใช้ defaultSort กำหนดการเรียงตอนเริ่ม หรือส่ง sort และ onSortChange เมื่อ core API เป็นผู้เรียง

import { SortHeader } from "@bsru/ui";

ตัวอย่าง

คอลัมน์ที่เรียงได้ใน DataTable

ใส่ sortable: true ในคอลัมน์ที่เรียงได้ กดหัวคอลัมน์เดิมเพื่อสลับทิศทาง กดคอลัมน์อื่นเพื่อเรียงตามคอลัมน์นั้นจากน้อยไปมาก มีเพียงคอลัมน์เดียวที่ active คอลัมน์หน่วยงานไม่มี sortable จึงเป็นหัวคอลัมน์ธรรมดา

พบ 5 คำขอ

คำขอซื้อขอจ้าง
PR-2570-0001ครุภัณฑ์คอมพิวเตอร์โน้ตบุ๊ก 5 เครื่องหน่วยงาน ก150,000.00รออนุมัติ
PR-2570-0002จ้างซ่อมเครื่องปรับอากาศหน่วยงาน ข43,500.00อนุมัติแล้ว
PR-2570-0003วัสดุสำนักงานหน่วยงาน ค19,500.00อนุมัติแล้ว
PR-2570-0004โต๊ะและเก้าอี้ห้องเรียนหน่วยงาน ก88,000.00ร่าง
PR-2570-0005ค่าเช่าซอฟต์แวร์หน่วยงาน ข64,000.00ไม่อนุมัติ
แสดง 1-5 จาก 5 คำขอต่อหน้า
const columns: DataColumn<Request>[] = [
  { key: "no", label: "เลขที่", sortable: true },
  { key: "item", label: "รายการ", sortable: true },
  { key: "unit", label: "หน่วยงาน", hideOnPhone: true },
  {
    key: "amount",
    label: "วงเงิน (บาท)",
    kind: "money",
    sortable: true,
  },
  {
    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 },
    },
    sortable: true,
  },
];

<DataTable
  caption="คำขอซื้อขอจ้าง"
  columns={columns}
  rows={rows}
  rowKey="no"
  defaultSort={{ key: "no", dir: "asc" }}
  noun="คำขอ"
/>

หัวคอลัมน์ที่ DataTable วาด

SortHeader ตัวเดียว ตามที่ DataTable วาดให้คอลัมน์เงินที่กำลังเรียง ตารางส่ง className money ให้ชิดขวาตรงกับตัวเลข ลูกศรแสดงทิศทาง และ aria-sort บอกโปรแกรมอ่านหน้าจอ

const [dir, setDir] = useState("desc");
const toggleDir = () => setDir(dir === "asc" ? "desc" : "asc");

<SortHeader
  label="วงเงิน (บาท)"
  className="money"
  active
  dir={dir}
  onSort={toggleDir}
/>

props ทั้งหมด

ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

SortHeader props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
label* จำเป็นReactNode—The column name, already in the screen language.
active* จำเป็นboolean—True when the table is sorted by this column. Only one column is active at a time.
dir* จำเป็นSortDir
ค่าที่ใช้ได้:"asc""desc"
—The table's sort direction. The arrow shows it only on the active column.
onSort* จำเป็น() => void—Called on click. The table decides whether to flip the direction or sort by this column.
classNamestring—Classes for the heading cell, such as money for a right-aligned money column.

ชนิดข้อมูล

SortDir

The sort direction: asc for A to Z and smallest first, desc for the reverse.

type SortDir = "asc" | "desc";

DataColumn

One column of a DataTable. The table reads the value from each row by key, and formats it by kind.

DataColumn fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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.
kindColumnKind
ค่าที่ใช้ได้:"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.
sortableboolean—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.
searchableboolean—The search box looks in this column. Text columns are searched unless this is false.
badgesReadonly<Record<string, BadgeStyle>>—For a badge column: how each value shows, such as { pending: { tone: "pending", label: "รออนุมัติ" } }.
subkeyof R & string—Another row field, shown as a smaller second line, such as a vendor code under the vendor name.
avatarboolean—Shows the value's initials in a round tile before it, such as for a person's or a unit's name.
opensboolean—The value opens the row, through the table's onOpen or openHref. Usually the record number.
totalboolean—Adds the column up in the total row. For number and money columns.
unitkeyof 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.
hideOnPhoneboolean—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.

BadgeStyle fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
tone* จำเป็นBadgeTone
ค่าที่ใช้ได้:"draft""pending""approved""paid""request""rejected""line"
——
label* จำเป็นstring—The badge text, already in the screen language.
iconLucideIcon——

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.

TableSearch fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
placeholder* จำเป็นstring—Says what can be searched, such as ค้นหาเลขที่หรือรายการ. It is also the box's accessible name.
valuestring—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.

TableFilters fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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.
activeboolean—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.
panelReactNode—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.

FilterChip fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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.

TableSort fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
key* จำเป็นstring——
dir* จำเป็นSortDir
ค่าที่ใช้ได้:"asc""desc"
——

TableSelection

Row selection, with actions on the chosen rows.

TableSelection fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
selectedreadonly 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.

BulkAction fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
label* จำเป็นstring—The button text, already in the screen language.
iconLucideIcon——
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.

TableError fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
title* จำเป็นstring—What happened, in a short sentence.
body* จำเป็นstring—What to do next.
correlationIdstring—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.

TableEmpty fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
title* จำเป็นstring—Says there are no records yet, such as ยังไม่มีคำขอซื้อขอจ้าง.
body* จำเป็นstring—Says what will appear here, and how it gets here.
iconLucideIcon—A Lucide icon for the kind of record.
actionTableAction—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.

TableAction fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
label* จำเป็นstring—The button text, already in the screen language.
iconLucideIcon——
tone"primary" | "secondary" | "danger"—secondary by default. Use primary for the one main action, and danger for one that deletes.
onClick* จำเป็น() => void——

ข้อควรปฏิบัติ

  • SortHeader เป็น th ที่มีปุ่มอยู่ข้างใน กด Tab ไปที่ปุ่ม แล้ว Enter หรือ Space เพื่อเรียง
  • DataTable ตั้ง active ให้คอลัมน์ที่เรียงอยู่เพียงคอลัมน์เดียว aria-sort จึงอยู่ที่คอลัมน์นั้นเท่านั้น
  • เมื่อหน้าจอเก็บการเรียงเองผ่าน sort และ onSortChange ให้กลับไปหน้า 1 เมื่อการเรียงเปลี่ยน ตารางเก็บแถวที่เลือกไว้ให้
  • คอลัมน์ kind money และ number ชิดขวาทั้งหัวคอลัมน์และเซลล์ หน้าจอไม่ต้องส่งคลาสเอง

ซอร์สโค้ด: ui/src/data-table.tsx