DataGrid 是 jQuery EasyUI 中非常常用的表格元件,主要用於展示大量結構化資料。它擴展自 panel,並可搭配 resizable、linkbutton、pagination 等元件使用。
使用時,通常先在頁面中建立一個 table 元素,再透過 datagrid 方法初始化,並設定資料來源與欄位定義。若資料來自遠端,可透過 url 指定 JSON 位址。
每個欄位可設定 title、field、width、align、sortable、resizable、hidden、checkbox、formatter、styler、sorter 與 editor 等屬性。透過 formatter 可以自訂儲存格顯示內容,透過 editor 則可支援行內編輯。
DataGrid 提供多種事件,例如 onLoadSuccess、onLoadError、onBeforeLoad、onClickRow、onDblClickRow、onClickCell、onDblClickCell、onSortColumn、onResizeColumn、onSelect、onUnselect、onSelectAll、onUnselectAll、onBeforeEdit、onAfterEdit、onCancelEdit、onHeaderContextMenu 與 onRowContextMenu。
常用方法包含 load、reload、loadData、getData、getRows、getSelected、getSelections、selectRow、unselectRow、beginEdit、endEdit、cancelEdit、updateRow、appendRow、insertRow、deleteRow、getChanges、acceptChanges、rejectChanges、mergeCells、showColumn 與 hideColumn。
DataGrid 的設計重點在於資料展示、排序、分頁、選擇與編輯。掌握欄位設定與常用事件,就能快速建立功能完整的表格介面。