ui
ธีม รูปแบบ CarbonKanok ภาษา English
ภาพรวมติดตั้งการย้ายไป 1.0ธีมและ layerดีไซน์ Kanokปรับแต่งหน้าตาไอคอนฟอร์มและการตรวจค่าใช้กับ frameworkใช้กับ AIButtonText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderModalSide panelTearsheetNotificationToastTooltipDefinition tooltipPopoverToggletipMenuData tableStructured listPaginationContained listTree viewTabsBreadcrumbLinkPagination navContent switcherAccordionProgress indicatorUI shellTagTileCardProduct cardCode snippetCopy buttonUser avatarTruncated textListLoadingInline loadingProgress barSkeletonBadge indicatorStatus indicatorGridHeadingLayerAction setPage headerBig numberEdit in placeแนะนำText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderTabsPagination navContent switcherTooltipDefinition tooltipPopoverMenuAccordionProgress indicatorUI shellModalSide panelTearsheetNotificationToastData tableStructured listPaginationTree viewCode snippetCopy buttonTruncated textLoadingTagCardProduct cardEdit in place

การย้ายไป 1.0

1.0 เปลี่ยน web component แบบขับเคลื่อนด้วยข้อมูล <ui-data-table> เป็นชุด element ที่ทำตาม Carbon web components (cds-table): <ui-table>, <ui-table-head>, <ui-table-header-row>, <ui-table-header-cell>, <ui-table-body>, <ui-table-row>, <ui-table-cell> และ element ของ toolbar, batch action และ skeleton รอบ ๆ หน้านี้รวมสิ่งที่ต้องเปลี่ยน

อะไรเปลี่ยนและทำไม

  • เขียนเป็น markup แทนข้อมูล <ui-data-table> รับ headers และ rows เป็น property แล้ววาดตารางเอง ส่วน <ui-table> เขียนเป็น element แบบ cds-table ของ Carbon: หัวคอลัมน์ แถว และเซลล์แต่ละตัวเป็น element ที่คุณเขียนหรือ render เอง เซลล์แสดงอะไรก็ได้ที่ใส่ไว้ renderCell จึงไม่มีแล้ว
  • ชื่อตาม Carbon web components attribute, event, slot และ part ตอนนี้ตาม cds-table: is-sortable, is-selectable, use-zebra-styles, ui-table-header-cell-sort, ui-table-row-selected เป็นต้น ความสามารถที่ Carbon ไม่มี (แบ่งหน้า ตั้งค่าคอลัมน์ ตรึงและปรับความกว้างคอลัมน์ CSV แก้ไขในตาราง สถานะกำลังโหลดและว่าง) ยังทำงานเหมือนเดิม และได้ชื่อในแบบเดียวกัน
  • toolbar ก็เป็น element ช่องค้นหา batch action ปุ่ม Columns และปุ่ม Export CSV คือ <ui-table-toolbar-search>, <ui-table-batch-actions>, <ui-table-column-settings> และ <ui-table-csv-export> ใน <ui-table-toolbar> แทนการเปิดสวิตช์บนตาราง
  • entry point ใหม่ import @purpurabog/ui/table แทน @purpurabog/ui/data-table

ไม่เปลี่ยน: DataTable ของ React + Tailwind (tw-data-table ขับเคลื่อนด้วย headers และ rows รวมถึง DataTableSkeleton) และ hook headless useDataTable กับ part ของมัน โค้ดที่ใช้สองอย่างนี้ไม่ต้องแก้

ก่อนและหลัง

ก่อนหลัง<ui-data-table id="contracts" heading="สัญญา" sortable selectable searchable csv-export> <ui-button slot="batch-actions">เก็บเข้าคลัง</ui-button> </ui-data-table> <script type="module"> import '@purpurabog/ui/data-table'; const table = document.getElementById('contracts'); table.headers = [ { key: 'name', header: 'สัญญา' }, { key: 'amount', header: 'จำนวนเงิน (บาท)' }, ]; table.rows = [ { id: '1', name: 'สัญญาบริการ', amount: 1250000 }, { id: '2', name: 'สัญญารักษาความลับ', amount: 0 }, ]; table.renderCell = (row, header) => (header.key === 'amount' ? row.amount.toLocaleString('th-TH') : row[header.key]); table.addEventListener('ui-select', (event) => console.log(event.detail.selected)); </script> <ui-table id="contracts" aria-label="สัญญา" is-sortable is-selectable> <ui-table-header-title slot="title">สัญญา</ui-table-header-title> <ui-table-toolbar slot="toolbar"> <ui-table-batch-actions> <ui-button>เก็บเข้าคลัง</ui-button> </ui-table-batch-actions> <ui-table-toolbar-content> <ui-table-toolbar-search></ui-table-toolbar-search> <ui-table-csv-export></ui-table-csv-export> </ui-table-toolbar-content> </ui-table-toolbar> <ui-table-head> <ui-table-header-row> <ui-table-header-cell column="name">สัญญา</ui-table-header-cell> <ui-table-header-cell column="amount">จำนวนเงิน (บาท)</ui-table-header-cell> </ui-table-header-row> </ui-table-head> <ui-table-body> <ui-table-row data-id="1"> <ui-table-cell>สัญญาบริการ</ui-table-cell> <ui-table-cell sort-value="1250000" csv-value="1250000">1,250,000</ui-table-cell> </ui-table-row> <ui-table-row data-id="2"> <ui-table-cell>สัญญารักษาความลับ</ui-table-cell> <ui-table-cell sort-value="0" csv-value="0">0</ui-table-cell> </ui-table-row> </ui-table-body> </ui-table> <script type="module"> import '@purpurabog/ui/table'; const table = document.getElementById('contracts'); table.addEventListener('ui-table-row-selected', (event) => console.log(event.detail.selectedRows.map((row) => row.dataset.id))); </script>

Attribute และ property

<ui-data-table> 1.0
headers ({ key, header }[]) <ui-table-header-cell> ใน <ui-table-head> › <ui-table-header-row>; key → column, header → เนื้อหาของเซลล์
rows ({ id, … }[]) <ui-table-row> ที่มี <ui-table-cell> ใน <ui-table-body>; เก็บ id เอง (data-id, key ของ React)
renderCell(row, header) เขียนเนื้อหาลงใน <ui-table-cell>
size size (เหมือนเดิม)
zebra use-zebra-styles
static-width use-static-width
sortable is-sortable บนตาราง (ทุกหัวคอลัมน์) หรือบนหัวคอลัมน์บางตัว
sort-key + sort-direction sort-direction บน <ui-table-header-cell> ตัวนั้น
manual-sorting preventDefault() ใน ui-table-header-cell-sort แล้วเรียงข้อมูลเองและตั้ง sort-direction
sortRow(a, b, context) sort-value บนเซลล์; customSortRow(lhs, rhs, collator) บนตารางเปรียบเทียบค่าของสองเซลล์
heading <ui-table-header-title slot="title">
description <ui-table-header-description slot="description">
label aria-label บน <ui-table>
selectable is-selectable
radio radio (เหมือนเดิม)
selectedIds selected บน <ui-table-row>; selectedRows (element ของแถว) บนตาราง
searchable <ui-table-toolbar-search> ใน <ui-table-toolbar-content>
search-persistent persistent บน <ui-table-toolbar-search>
search-label, search-placeholder label, placeholder บน <ui-table-toolbar-search>
filter-text ไม่มี attribute: ตั้ง value ของช่องค้นหาแล้วยิง event input จากช่องค้นหา
filterRows(row, text, headers) (true คือเก็บแถว) filterRows(rowText, searchString) บนตาราง — ตอนนี้ true คือซ่อนแถว แบบ Carbon
manual-filtering filterRows = () => false แล้วฟัง input ของช่องค้นหา
renderExpandedRow(row) expandable บนตาราง และ <ui-table-expanded-row> ต่อจากแถวทันที
expandedIds expanded บน <ui-table-row>
expand-all batch-expansion (คู่กับ expandable)
sticky-header sticky-header (เหมือนเดิม)
resizable resizable บนตาราง (ทุกหัวคอลัมน์) หรือบนหัวคอลัมน์บางตัว
columnWidths width บนหัวคอลัมน์แต่ละตัว; columnWidths ของตารางตอนนี้อ่านได้อย่างเดียว
loading, loading-rows เหมือนเดิม
page-size, page, page-sizes เหมือนเดิม
total-selected selected-rows-count บน <ui-table-batch-actions> (ตั้งใน listener ของ ui-table-row-selected)
total-count total-rows-count บน <ui-table-batch-actions>
column-settings <ui-table-column-settings> ใน <ui-table-toolbar-content>
hiddenColumns column-hidden บนหัวคอลัมน์, setColumnHidden(column, hidden) และ resetColumns(); hiddenColumns ของตารางตอนนี้อ่านได้อย่างเดียว
csv-export <ui-table-csv-export> ใน <ui-table-toolbar-content>
csv-file-name file-name บน <ui-table-csv-export>
data-has-selection บน host selected บน <ui-table-header-row> ขณะมีแถวใดถูกเลือก (ui-table:has(ui-table-header-row[selected]))

field ของ header:

DataTableHeader <ui-table-header-cell>
key column
header เนื้อหาของเซลล์
sortable: false ไม่ใส่ is-sortable บนตาราง แล้วใส่บนหัวคอลัมน์ที่เรียงได้
align ไม่มี attribute: แต่ง style เซลล์เอง เช่น style="text-align: end" บน <ui-table-cell>
editable, options editable, options (property หรือ JSON ใน attribute)
validate(value, row) validate(value, cell) (property)
resizable: false ไม่ใส่ resizable บนตาราง แล้วใส่บนหัวคอลัมน์ที่ปรับได้
width, minWidth, maxWidth width, min-width, max-width
hideable: false hideable="false"
hidden: true column-hidden
pinned pinned

field ของแถว: id เก็บเอง (data-id); disabled: true → disabled บน <ui-table-row>

Method

<ui-data-table> 1.0
toCsv({ scope }), downloadCsv({ scope, fileName }) เหมือนเดิมบน <ui-table>
focus() เหมือนเดิม
selectedRows selectedRows คืน element <ui-table-row>
sortBy(key, direction) ไม่มี: กดหัวคอลัมน์ หรือเรียงข้อมูลเองแล้วตั้ง sort-direction
filteredRows, visibleRows, shownRows ไม่มี: table.querySelectorAll('ui-table-row:not([filtered])') แถวที่อยู่หน้าอื่นมี paged-out ด้วย
pageCount ไม่มี: Math.ceil(rows / pageSize)
visibleHeaders table.columns.filter((header) => !header.columnHidden)

Event

<ui-data-table> 1.0
ui-sort { key, direction } ui-table-header-cell-sort { oldSortDirection, sortDirection } จากหัวคอลัมน์ ยกเลิกได้ ก่อนย้ายแถว; ui-table-sorted { sortedHeader } หลังย้าย
ui-select { selected, rows } ui-table-row-selected { selectedRow, selectedRows }; ui-table-row-all-selected { selectedRows } หลังเลือกทั้งหมดหรือ Cancel
ui-filter { value } ui-table-filtered { unfilteredRows }; input ของ <ui-table-toolbar-search> สำหรับข้อความ
ui-cell-edit { rowId, key, value, previous, row } หลังบันทึก ui-table-cell-edit { row, cell, column, value, previous } จากเซลล์ ยกเลิกได้ ก่อนเขียนค่า
ui-page-change ui-table-page-change (detail เหมือนเดิม)
ui-select-all { totalCount } ui-table-batch-actions-select-all-clicked จาก <ui-table-batch-actions>
ui-column-resize { key, width, widths } ui-table-column-resize { column, width } จากหัวคอลัมน์; อ่าน columnWidths ของตารางสำหรับทุกคอลัมน์
ui-expand { expandedIds, expanded, id } ui-table-row-expando-toggled { expanded } จากแถว (ก่อนหน้านั้นมี ui-table-row-expando-beingtoggled ที่ยกเลิกได้)
ui-csv-export { csv, rows, fileName } ui-table-csv-export { csv, rows, fileName } จาก <ui-table-csv-export> ยกเลิกได้; rows ตอนนี้เป็น element ของแถว
ui-columns-change { hiddenColumns, key, hidden } ui-table-columns-change { hiddenColumns, column, hidden }

Slot

<ui-data-table> 1.0
toolbar ตัวควบคุมของคุณอยู่ใน <ui-table-toolbar-content> ภายใน <ui-table-toolbar slot="toolbar">
batch-actions ลูกของ <ui-table-batch-actions>
empty, no-results เหมือนเดิม
— ใหม่: title, description

CSS part และ custom property

<ui-data-table> 1.0
base ไม่มี: แต่ง ui-table
header, content, table, pagination, empty เหมือนเดิมบน ui-table
title, description แต่ง ui-table-header-title, ui-table-header-description
toolbar, toolbar-content, search แต่ง ui-table-toolbar, ui-table-toolbar-content, ui-table-toolbar-search
batch-actions แต่ง ui-table-batch-actions
batch-summary, cancel-button, select-all-button ui-table-batch-actions::part(…) ชื่อเดิม
sort-button, resizer ui-table-header-cell::part(sort-button), ::part(resizer)
columns-button, columns-panel ui-table-column-settings::part(button), ::part(panel)
export-button ui-table-csv-export::part(button)
expand-button ui-table-row::part(expand-button) (ui-table-header-row สำหรับขยายทั้งหมด)
expanded-row ui-table-expanded-row::part(cell)
--ui-data-table-max-height --ui-table-max-height

DataTable ของ React + Tailwind ยังอ่าน --ui-data-table-max-height

Skeleton

<ui-data-table-skeleton> (UiDataTableSkeleton) เปลี่ยนเป็น <ui-table-skeleton> (UiTableSkeleton) แบบ cds-table-skeleton ของ Carbon: headers เป็นรายการข้อความแทน object { header }, hide-header เป็น show-header="false" และ hide-toolbar เป็น show-toolbar="false" ส่วน row-count, column-count และ zebra เหมือนเดิม และไม่มี size

ข้อความสำหรับ screen reader

<ui-data-table> บอก "เรียงตามคอลัมน์ สัญญา จากน้อยไปมาก" หลังกดเรียง และ "แสดง 3 จาก 12 แถว" เมื่อหยุดพิมพ์ค้นหา ส่วน <ui-table> บอกแค่ "กำลังโหลดข้อมูล" ขณะ loading เพราะตารางของ Carbon ไม่บอกอะไร จำนวนในแถบ batch ยังเป็น status อยู่ DataTable ของ React + Tailwind ยังบอกทั้งสองอย่าง

React

UiDataTable และ UiDataTableSkeleton ไม่มีใน @purpurabog/ui/react แล้ว เลือกอย่างใดอย่างหนึ่ง:

  • wrapper UiTable* (UiTable, UiTableHead, UiTableHeaderRow, UiTableHeaderCell, UiTableBody, UiTableRow, UiTableCell, UiTableExpandedRow, UiTableHeaderTitle, UiTableHeaderDescription, UiTableToolbar, UiTableToolbarContent, UiTableToolbarSearch, UiTableBatchActions, UiTableColumnSettings, UiTableCsvExport, UiTableSkeleton): map ข้อมูลของคุณเข้าไป event เป็น prop onUiTable… เช่น onUiTableRowSelected
  • DataTable ของ React + Tailwind: ยังขับเคลื่อนด้วย headers และ rows ใกล้กับ prop ของ UiDataTable เดิมที่สุด

ตารางเรียงลำดับด้วยการย้ายแถวใน DOM และบันทึกการแก้ไขด้วยการเขียนลงเซลล์ แถวที่ React render เป็นของ React จึงต้องเรียงและบันทึกผ่าน state ของคุณ:

<UiTable onUiTableCellEdit={(e) => { e.preventDefault(); // React เขียนค่าเอง const { row, column, value } = e.detail; setRows((all) => all.map((r) => (r.id === row.dataset.id ? { ...r, [column]: value } : r))); }} > <UiTableHead> <UiTableHeaderRow> {headers.map((h) => ( <UiTableHeaderCell key={h.key} column={h.key} editable={h.editable} sortDirection={sort.column === h.key ? sort.direction : 'none'} onUiTableHeaderCellSort={(e) => { e.preventDefault(); // React จัดลำดับแถวเอง setSort({ column: h.key, direction: e.detail.sortDirection }); }} > {h.header} </UiTableHeaderCell> ))} </UiTableHeaderRow> </UiTableHead> <UiTableBody> {sortRows(rows, sort).map((row) => ( <UiTableRow key={row.id} data-id={row.id}> {headers.map((h) => <UiTableCell key={h.key}>{row[h.key]}</UiTableCell>)} </UiTableRow> ))} </UiTableBody> </UiTable>

Vue, Angular และ framework อื่นที่ render แถวเองก็ทำแบบเดียวกัน ดูทุกความสามารถทั้งแบบ HTML และ React ได้ที่ Data table

Type

type DataTable… ของ web component (DataTableHeader, DataTableRow, DataTableSortDetail …) ไม่มีใน @purpurabog/ui แล้ว type ใหม่คือ UiTable, UiTableRow, UiTableHeaderCell …, TableSortDirection และ detail ของ event (TableSortedDetail, TableRowSelectedDetail, TableCellEditDetail …) ส่วน type ของ React + Tailwind (DataTableSort …) ไม่เปลี่ยน