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

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

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>

คอลัมน์เงินที่กำลังเรียง

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";

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

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

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