自訂篩選選項
此範例示範如何在 SearchPanes 中設定自訂比較函數。
姓名 | 職位 | 辦公室 | 年齡 | 薪資 | |
---|---|---|---|---|---|
Tiger Nixon | 系統架構師 | 愛丁堡 | 61 | $320,800 | |
Garrett Winters | 會計師 | 東京 | 63 | $170,750 | |
Ashton Cox | 初級技術作者 | 舊金山 | 66 | $86,000 | |
Cedric Kelly | 資深 Javascript 開發人員 | 愛丁堡 | 22 | $433,060 | |
Airi Satou | 會計師 | 東京 | 33 | $162,700 | |
Brielle Williamson | 整合專家 | 紐約 | 61 | $372,000 | |
Herrod Chandler | 銷售助理 | 舊金山 | 59 | $137,500 | |
Rhona Davidson | 整合專家 | 東京 | 55 | $327,900 | |
Colleen Hurst | Javascript 開發人員 | 舊金山 | 39 | $205,500 | |
Sonya Frost | 軟體工程師 | 愛丁堡 | 23 | $103,600 | |
Jena Gaines | 辦公室經理 | 倫敦 | 30 | $90,560 | |
Quinn Flynn | 支援主管 | 愛丁堡 | 22 | $342,000 | |
Charde Marshall | 區域主管 | 舊金山 | 36 | $470,600 | |
Haley Kennedy | 資深行銷設計師 | 倫敦 | 43 | $313,500 | |
Tatyana Fitzpatrick | 區域主管 | 倫敦 | 19 | $385,750 | |
Michael Silva | 行銷設計師 | 倫敦 | 66 | $198,500 | |
Paul Byrd | 財務長 (CFO) | 紐約 | 64 | $725,000 | |
Gloria Little | 系統管理員 | 紐約 | 59 | $237,500 | |
Bradley Greer | 軟體工程師 | 倫敦 | 41 | $132,000 | |
Dai Rios | 人事主管 | 愛丁堡 | 35 | $217,500 | |
Jenette Caldwell | 開發主管 | 紐約 | 30 | $345,000 | |
Yuri Berry | 行銷長 (CMO) | 紐約 | 40 | $675,000 | |
Caesar Vance | 售前支援 | 紐約 | 21 | $106,450 | |
Doris Wilder | 銷售助理 | 雪梨 | 23 | $85,600 | |
Angelica Ramos | 執行長 (CEO) | 倫敦 | 47 | $1,200,000 | |
Gavin Joyce | 開發人員 | 愛丁堡 | 42 | $92,575 | |
Jennifer Chang | 區域主管 | 新加坡 | 28 | $357,650 | |
Brenden Wagner | 軟體工程師 | 舊金山 | 28 | $206,850 | |
Fiona Green | 營運長 (COO) | 舊金山 | 48 | $850,000 | |
Shou Itou | 區域行銷 | 東京 | 20 | $163,000 | |
Michelle House | 整合專家 | 雪梨 | 37 | $95,400 | |
Suki Burks | 開發人員 | 倫敦 | 53 | $114,500 | |
Prescott Bartlett | 技術作者 | 倫敦 | 27 | $145,000 | |
Gavin Cortez | 團隊領導 | 舊金山 | 22 | $235,500 | |
Martena Mccray | 售後支援 | 愛丁堡 | 46 | $324,050 | |
Unity Butler | 行銷設計師 | 舊金山 | 47 | $85,675 | |
Howard Hatfield | 辦公室經理 | 舊金山 | 51 | $164,500 | |
Hope Fuentes | 秘書 | 舊金山 | 41 | $109,850 | |
Vivian Harrell | 財務主管 | 舊金山 | 62 | $452,500 | |
Timothy Mooney | 辦公室經理 | 倫敦 | 37 | $136,200 | |
Jackson Bradshaw | 總監 | 紐約 | 65 | $645,750 | |
Olivia Liang | 支援工程師 | 新加坡 | 64 | $234,500 | |
Bruno Nash | 軟體工程師 | 倫敦 | 38 | $163,500 | |
Sakura Yamamoto | 支援工程師 | 東京 | 37 | $139,575 | |
Thor Walton | 開發人員 | 紐約 | 61 | $98,540 | |
Finn Camacho | 支援工程師 | 舊金山 | 47 | $87,500 | |
Serge Baldwin | 資料協調員 | 新加坡 | 64 | $138,575 | |
Zenaida Frank | 軟體工程師 | 紐約 | 63 | $125,250 | |
Zorita Serrano | 軟體工程師 | 舊金山 | 56 | $115,000 | |
Jennifer Acosta | 初級 Javascript 開發人員 | 愛丁堡 | 43 | $75,650 | |
Cara Stevens | 銷售助理 | 紐約 | 46 | $145,600 | |
Hermione Butler | 區域主管 | 倫敦 | 47 | $356,250 | |
Lael Greer | 系統管理員 | 倫敦 | 21 | $103,500 | |
Jonas Alexander | 開發人員 | 舊金山 | 30 | $86,500 | |
Shad Decker | 區域主管 | 愛丁堡 | 51 | $183,000 | |
Michael Bruce | Javascript 開發人員 | 新加坡 | 29 | $183,000 | |
Donna Snider | 客戶支援 | 紐約 | 27 | $112,000 | |
姓名 | 職位 | 辦公室 | 年齡 | 薪資 |
- Javascript
- HTML
- CSS
- Ajax
- 伺服器端腳本
- 註解
以下顯示的 Javascript 用於初始化此範例中顯示的表格
var dt = $('#example').DataTable({ columnDefs: [ { orderable: false, render: DataTable.render.select(), searchPanes: { show: true, options: [ { label: '已勾選', value: function (rowData, rowIdx) { return this.row(rowIdx, { selected: true }).any(); } }, { label: '未勾選', value: function (rowData, rowIdx) { return this.row(rowIdx, { selected: true }).any() === false; } } ] }, targets: [0] }, { searchPanes: { options: [ { label: '小於 20 歲', value: function (rowData, rowIdx) { return rowData[4] < 20; } }, { label: '20 到 30 歲', value: function (rowData, rowIdx) { return rowData[4] <= 30 && rowData[4] >= 20; } }, { label: '30 到 40 歲', value: function (rowData, rowIdx) { return rowData[4] <= 40 && rowData[4] >= 30; } }, { label: '40 到 50 歲', value: function (rowData, rowIdx) { return rowData[4] <= 50 && rowData[4] >= 40; } }, { label: '50 到 60 歲', value: function (rowData, rowIdx) { return rowData[4] <= 60 && rowData[4] >= 50; } }, { label: '大於 60 歲', value: function (rowData, rowIdx) { return rowData[4] > 60; } } ] }, targets: [4] }, { searchPanes: { options: [ { label: '非愛丁堡', value: function (rowData, rowIdx) { return rowData[3] !== 'Edinburgh'; } }, { label: '非倫敦', value: function (rowData, rowIdx) { return rowData[3] !== 'London'; } } ], combiner: 'and' }, targets: [3] } ], layout: { top1: { searchPanes: { viewTotal: true, columns: [0, 3, 4] } } }, select: { style: 'os', selector: 'td:first-child' }, order: [[1, 'asc']] }); dt.on('select.dt', () => { dt.searchPanes.rebuildPane(0, true); }); dt.on('deselect.dt', () => { dt.searchPanes.rebuildPane(0, true); });
var dt = new DataTable('#example', { columnDefs: [ { orderable: false, render: DataTable.render.select(), searchPanes: { show: true, options: [ { label: '已勾選', value: function (rowData, rowIdx) { return this.row(rowIdx, { selected: true }).any(); } }, { label: '未勾選', value: function (rowData, rowIdx) { return this.row(rowIdx, { selected: true }).any() === false; } } ] }, targets: [0] }, { searchPanes: { options: [ { label: '小於 20 歲', value: function (rowData, rowIdx) { return rowData[4] < 20; } }, { label: '20 到 30 歲', value: function (rowData, rowIdx) { return rowData[4] <= 30 && rowData[4] >= 20; } }, { label: '30 到 40 歲', value: function (rowData, rowIdx) { return rowData[4] <= 40 && rowData[4] >= 30; } }, { label: '40 到 50 歲', value: function (rowData, rowIdx) { return rowData[4] <= 50 && rowData[4] >= 40; } }, { label: '50 到 60 歲', value: function (rowData, rowIdx) { return rowData[4] <= 60 && rowData[4] >= 50; } }, { label: '大於 60 歲', value: function (rowData, rowIdx) { return rowData[4] > 60; } } ] }, targets: [4] }, { searchPanes: { options: [ { label: '非愛丁堡', value: function (rowData, rowIdx) { return rowData[3] !== 'Edinburgh'; } }, { label: '非倫敦', value: function (rowData, rowIdx) { return rowData[3] !== 'London'; } } ], combiner: 'and' }, targets: [3] } ], layout: { top1: { searchPanes: { viewTotal: true, columns: [0, 3, 4] } } }, select: { style: 'os', selector: 'td:first-child' }, order: [[1, 'asc']] }); dt.on('select.dt', () => { dt.searchPanes.rebuildPane(0, true); }); dt.on('deselect.dt', () => { dt.searchPanes.rebuildPane(0, true); });
除了以上程式碼之外,還會載入以下 Javascript 函式庫檔案以供本範例使用
以下顯示的 HTML 是原始 HTML 表格元素,在經過 DataTables 增強之前
此範例使用了除函式庫檔案載入的 CSS 之外的一些額外 CSS(如下所示),以便正確顯示表格。以下顯示使用的額外 CSS
載入以下 CSS 函式庫檔案以供本範例使用,以提供表格的樣式
此表格透過 Ajax 載入資料。以下顯示已載入的最新資料。此資料將在載入任何額外資料時自動更新。
以下顯示用於執行此表格伺服器端處理的腳本。請注意,這只是一個使用 PHP 的範例腳本。伺服器端處理腳本可以使用任何語言編寫,方法是使用 DataTables 文件中描述的協定。