{hero}

選擇器修飾符

關於行、列和儲存格選擇器如何在行上操作的選項。

描述

當您在 rows()columns()cells() (及其單數形式) 中使用選擇器時,您會想要知道並能夠控制 DataTables 如何處理行的基本方面,例如它們被處理的順序,以及選擇器應該作用於哪一組行。此 selector-modifier 類型正好提供了這種能力,並且可以選擇性地用於任何使用選擇器的函數中。

伺服器端處理

關於伺服器端處理的特別說明:當在伺服器端處理模式下使用 DataTables (serverSide) 時,selector-modifier 對所選行的影響很小,因為所有處理(排序、搜尋等)都是在伺服器端執行的。因此,在用戶端存在的行只有在任何時間顯示在表格中的行,並且選擇器只能選擇目前頁面上顯示的行。

選項

基本上,selector-modifier 是一個具有三個內建屬性的物件,可以使用這些屬性,並且可以透過擴充功能(例如 Select)進行擴充。這些屬性及其預設值如下:

{
    // DataTables core
    order:  'current',  // 'current', 'applied', 'index', 'original', number
    page:   'all',      // 'all',     'current'
    search: 'none',     // 'none',    'applied', 'removed'

    // Extension - KeyTable (v2.1+) - cells only
    focused: undefined, // true, false, undefined

    // Extension - Select (v1.0+)
    selected: undefined // true, false, undefined
}

內建選項

order

order 修飾符提供控制行被處理順序的能力。這會影響鏈接函數的回傳值 - 例如,column().data() 可以按照表格目前顯示資料的順序或原始資料順序傳回該欄的資料。

  • current (預設) - 按照目前應用於表格的順序處理行。
  • index - 按照其資料索引順序處理行(資料最初讀入表格的順序)。
  • applied - current 的別名。
  • original - 為了向後相容而設計的 index 別名。
  • number - 應該將其排序應用於資料的欄索引(自 2.0 起)。請注意,此選項無法與 page: 'current' 選項一起使用。
取得應用順序中欄的資料

請注意,由於 selector-modifier 是可選的,且 applied 是欄的預設值,因此下面的範例與:table.column( 3 ).data(); 相同 - 它只是顯式設定了 order 參數。

var table = new DataTable('#myTable');
table.column( 3, {order:'current'} ).data();
取得資料索引順序中欄的資料
var table = new DataTable('#myTable');
table.column( 3, {order:'index'} ).data();

```

取得 DataTables 將資料排序的順序中欄的資料
var table = new DataTable('#myTable');
table.column( 3, {order:3} ).data();

page

page 修飾符可讓您控制選擇器是否應該考慮表格中的所有資料(無論分頁如何),或者是否只應使用目前顯示頁面中的行。

  • all (預設) - 使用所有頁面的行
  • current - 僅使用目前顯示頁面中的行。

重要:將 page 設定為 current 會隱式設定 order=currentsearch=applied。否則,current 選項就沒有意義!這些隱含的 ordersearch 值無法透過顯式設定來覆寫。

僅取得目前頁面上行的資料
var table = new DataTable('#myTable');
table.rows( {page:'current'} ).data();

search

search 修飾符提供使用應用於表格的搜尋選項來控制選擇器使用的行的能力。

  • none (預設) - 不考慮搜尋(即使用所有行)
  • applied - 僅使用與目前應用於表格的搜尋相符的行
  • removed - 僅使用已從應用搜尋中從表格中移除的行。

請注意,為了向後相容,search 詞也可以作為屬性 filter 提供。如果兩者都有提供,則優先使用 search 詞。

取得符合應用於表格的搜尋詞的行的 tr 元素,按照索引順序
var table = new DataTable('#myTable');
table.rows( {order:'index', search:'applied'} ).nodes();
取得已從搜尋中移除的行
var table = new DataTable('#myTable');
table.rows( {search:'removed'} ).nodes();

擴充功能

下列選項描述了可以透過使用 DataTables 的擴充功能新增到 DataTables 核心中的行為。這些擴充功能提供與 DataTables API 的緊密整合,並且這些選項可以與擴充功能一起使用,感覺像是 DataTables 的自然組成部分。

focused (僅限儲存格)

KeyTable 提供專注於 DataTable 中特定儲存格的能力,因此通常了解哪個儲存格具有焦點,以及哪個儲存格沒有焦點很有用。

請注意,此選項只能與 cells()cell() 方法一起使用。將其與行或列選擇器一起使用將無效。

此選項採用布林值,但也可能是 undefined,預設為此值

  • undefined (預設) - 不執行選擇修改
  • true - 只會選取具有焦點的儲存格
  • false - 只會選取沒有焦點的儲存格。
取得具有焦點儲存格的資料
var table = new DataTable('#myTable');
table.cell( {focused:true} ).data();

selected

DataTables 的 Select 擴充功能提供了選取表格中項目(行、列和儲存格)的能力,因此能夠檢索使用者選取的項目以便您對它們執行某些動作非常重要。

此選項採用布林值,但也可能是 undefined,預設為此值

  • undefined (預設) - 不執行選擇修改
  • true - 只會檢索已選取的項目
  • false - 只會檢索選取的項目。
取得已選取行的資料
var table = new DataTable('#myTable');
table.rows( {selected:true} ).data();

有關此屬性的其他資訊,請參閱Select 手冊