2012-08-03 70 views
0

當在數據表上啓用行選擇時,是否有辦法限制用戶可以選擇的行數?我只希望用戶能夠在數據表中選擇最多兩行和最少一行,但在Datatables API中沒有看到描述如何執行此操作的選項?數據表jquery插件 - 限制可選行數

我需要在用戶選擇行時觸發的某些回調中手動執行此操作嗎?如果可能,我想避免這種情況。任何幫助或洞察力,表示讚賞。

回答

0

確定了一個實現的工作,所以我落得這樣做的,而不是限制的行數,用戶可以選擇,我加了一些每當用戶點擊另一行時取消選擇任何先前選擇的行的邏輯。我將選擇順序視爲一個隊列,我剛剛使用一個數組實現,每當我將新行推入隊列時,任何以前的元素都會彈出。

隊列的大小決定了用戶可以選擇的行數,通過將隊列大小設置爲2,這允許最多選擇兩行,當選擇了附加行時,第一個選定的行被刪除從隊列中。我使用下面的fnGetInstance()和fnDeselect()函數取消選擇我的數據表實例中的行

//oldRow = first selected row from queue 

var oTT = TableTools.fnGetInstance('MyDataTable'); 
oTT.fnDeselect(oldRow); 
0

使用數據表例如:

$(document).ready(function() { 
    /* Add a click handler to the rows - this could be used as a callback */ 
    $("#example tbody tr").click(function(e) { 
     if ($(this).hasClass('row_selected')) { 
      $(this).removeClass('row_selected'); 
     } 
     // the following line do what you want 
     else if(oTable.$('tr.row_selected').length < 2) { 
      $(this).addClass('row_selected'); 
     } 
    }); 

    /* Add a click handler for the delete row */ 
    $('#delete').click(function() { 
     var anSelected = fnGetSelected(oTable); 
     if (anSelected.length !== 0) { 
      oTable.fnDeleteRow(anSelected[0]); 
     } 
    }); 

    /* Init the table */ 
    oTable = $('#example').dataTable(); 
}); 

參考:select single row