หัวคอลัมน์เรียงลำดับ
SortHeader
A column heading that sorts the table. aria-sort is set only on the column in use.
ใช้เป็นหัวคอลัมน์ที่กดเพื่อเรียงลำดับในตารางข้อมูล หน้าจอเก็บคอลัมน์และทิศทางที่เรียงไว้ใน state แล้วเรียงแถวเอง (หรือส่งให้ core API เรียง) คอลัมน์ที่เรียงไม่ได้ เช่น ช่องเลือกหรือปุ่มจัดการ ใช้ th ธรรมดา ตอนนี้หน้าจอยังต้องเขียนตาราง data-table เอง ส่วนประกอบ DataTable ที่รับข้อมูลอย่างเดียวจะตามมา
import { SortHeader } from "@bsru/ui";ตัวอย่าง
ตารางที่เรียงได้หลายคอลัมน์
กดหัวคอลัมน์เดิมเพื่อสลับทิศทาง กดคอลัมน์อื่นเพื่อเรียงตามคอลัมน์นั้นจากน้อยไปมาก มีเพียงคอลัมน์เดียวที่ active
| PR-2570-0001 | ครุภัณฑ์คอมพิวเตอร์โน้ตบุ๊ก 5 เครื่อง | 150,000.00 |
| PR-2570-0002 | คำขอที่เกินวงเงินคงเหลือ | 600,000.00 |
| PR-2570-0003 | วัสดุสำนักงาน | 19,500.00 |
| PR-2570-0004 | จ้างซ่อมเครื่องปรับอากาศ | 43,500.00 |
const columns = [
{ key: "no", label: "เลขที่" },
{ key: "item", label: "รายการ" },
{ key: "amount", label: "วงเงิน (บาท)", className: "money" },
];
const [sort, setSort] = useState("no");
const [dir, setDir] = useState("asc");
function onSort(key) {
if (key === sort) setDir(dir === "asc" ? "desc" : "asc");
else {
setSort(key);
setDir("asc");
}
}
// Today the screen still writes the table. A data-driven DataTable is coming next.
<table className="data-table">
<thead>
<tr>
{columns.map((c) => (
<SortHeader
key={c.key}
label={c.label}
className={c.className}
active={sort === c.key}
dir={dir}
onSort={() => onSort(c.key)}
/>
))}
</tr>
</thead>
<tbody>{/* rows, sorted by sort and dir */}</tbody>
</table>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| columns | รายการ 3 รายการ[
{ key: "no", label: "เลขที่" },
{ key: "item", label: "รายการ" },
{ key: "amount", label: "วงเงิน (บาท)", className: "money" },
] | array | — |
| sort | "no" | string | — |
| dir* จำเป็น | "asc" | SortDir ค่าที่ใช้ได้: "asc""desc" | The table's sort direction. The arrow shows it only on the active column. |
| rows | รายการ 4 รายการ[
{
no: "PR-2570-0001",
item: "ครุภัณฑ์คอมพิวเตอร์โน้ตบุ๊ก 5 เครื่อง",
amount: 150000,
},
{
no: "PR-2570-0002",
item: "คำขอที่เกินวงเงินคงเหลือ",
amount: 600000,
},
{ no: "PR-2570-0003", item: "วัสดุสำนักงาน", amount: 19500 },
{
no: "PR-2570-0004",
item: "จ้างซ่อมเครื่องปรับอากาศ",
amount: 43500,
},
] | array | — |
| onSort* จำเป็น | () => onSort(c.key) | () => void | Called on click. The table decides whether to flip the direction or sort by this column. |
คอลัมน์เงินที่กำลังเรียง
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}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "วงเงิน (บาท)" | ReactNode | The column name, already in the screen language. |
| className | "money" | string | Classes for the heading cell, such as money for a right-aligned money column. |
| active* จำเป็น | true | boolean | True when the table is sorted by this column. Only one column is active at a time. |
| dir* จำเป็น | dir | SortDir ค่าที่ใช้ได้: "asc""desc" | The table's sort direction. The arrow shows it only on the active column. |
| onSort* จำเป็น | toggleDir | () => void | Called on click. The table decides whether to flip the direction or sort by this column. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ 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. |
| className | string | — | 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";ข้อควรปฏิบัติ
- SortHeader เป็น th ที่มีปุ่มอยู่ข้างใน กด Tab ไปที่ปุ่ม แล้ว Enter หรือ Space เพื่อเรียง
- ตั้ง active เป็น true เพียงคอลัมน์เดียว aria-sort จึงอยู่ที่คอลัมน์นั้นเท่านั้น
- เมื่อเปลี่ยนการเรียง ให้กลับไปหน้า 1 และเก็บแถวที่เลือกไว้
- คอลัมน์เงินส่ง className money ทั้งหัวคอลัมน์และเซลล์ ให้ตัวเลขชิดขวา
ซอร์สโค้ด: ui/src/data-table.tsx