2013-03-05 42 views
0

我的頁面中有兩個按鈕「全選」和「取消全選」。我需要選擇所有的行,當我點擊全選按鈕並保存ID列表在我的變量。點擊取消選擇按鈕時,應取消選擇所有行。我們將如何做到這一點?如何選擇jQuery數據表中的所有行

 [HttpPost] 
      public ActionResult Action Name(pssing value) 
      { 
       DataTable dataTable = new DataTable(); 

      // my code 
       var MyModel = new List<List<string>>(); 

       foreach (var joblist in Jobs) 
       { 
        var sublist = new List<string>(); 
        sublist.Add(checked.false); 
        sublist.Add(values)); 
        sublist.Add(values)); 
        ....etc 

        baseModel.Add(sublist); 
       } 

       return new DataTableResult(return); 
      } 

HTML:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="AssignJobsTable" width="100%"> 
    <thead> 
     <tr> 
      <th><input type="checkbox" id="selectall"/></th> 
      <th>ID</th> 
      etc ...... 
     </tr> 
    </thead> 

腳本:

$(function(){ 

    // add multiple select/deselect functionality 
    $("#selectall").click(function() { 
      $('.case').attr('checked', this.checked); 
    }); 

    // if all checkbox are selected, check the selectall checkbox 
    // and viceversa 
    $(".case").click(function(){ 

     if($(".case").length == $(".case:checked").length) { 
      $("#selectall").attr("checked", "checked"); 
     } else { 
      $("#selectall").removeAttr("checked"); 
     } 

    }); 
}); 

當我點擊全選複選框它不能綁定值形成的控制器。

+0

粘貼你的代碼,那會更有幫助 – 2013-03-05 06:41:16

+0

我什麼也沒做。 – 2013-03-05 06:42:34

+0

'我什麼都沒做.' ........什麼?我們不能爲你生產。 – Jai 2013-03-05 07:03:07

回答

0

試試這個

$('table#tableId> tbody > tr').addClass('highlight'); 

$('table#tableId> tbody > tr').removeClass('highlight')addClass('noHeilight'); 
+0

我知道這個,但如何存儲選定的行ID? – 2013-03-05 06:43:20

+0

顯示你的代碼你如何設置行ID值 – PSR 2013-03-05 06:44:16

+0

請unsendta我的問題 – 2013-03-05 06:45:08

0

樣品臺

<table border="1"> 
<tr> 
    <th><input type="checkbox" id="selectall"/></th> 
    <th>Cell phone</th> 
    <th>Rating</th> 
</tr> 
<tr> 
    <td align="center"><input type="checkbox" class="case" name="case" value="1"/></td> 
    <td>BlackBerry Bold 9650</td> 
    <td>2/5</td> 
</tr> 
<tr> 
    <td align="center"><input type="checkbox" class="case" name="case" value="2"/></td> 
    <td>Samsung Galaxy</td> 
    <td>3.5/5</td> 
</tr> 

</table> 

示例腳本:

$(function(){ 

    // add multiple select/deselect functionality 
    $("#selectall").click(function() { 
      $('.case').attr('checked', this.checked); 
    }); 

    // if all checkbox are selected, check the selectall checkbox 
    // and viceversa 
    $(".case").click(function(){ 

     if($(".case").length == $(".case:checked").length) { 
      $("#selectall").attr("checked", "checked"); 
     } else { 
      $("#selectall").removeAttr("checked"); 
     } 

    }); 
}); 
+0

它有幫助。但ineed要通過我的控制器中選定的行,以便如何獲得選定的行ID? – 2013-03-05 06:46:06

+1

可憐的傢伙,當你選擇所有的行時,你應該使用jquery選擇器來獲取包含Id的元素並將其保存在一個數組中。 – OQJF 2013-03-05 06:54:04

+0

我知道。請檢查我最近的帖子 – 2013-03-05 07:06:06

0

您應該設置複選框ID ATTR,把行ID在id attr和使用用於選擇和取消選擇下面的代碼:

$(文件)。就緒(函數(){

$("#selectall").click(function() { 

     var isSelect; 
     if($(this).is(":checked")) { 
     isSelect=true; 
     }else{ 
     isSelect=false; 
     } 

     $("table tr td input[type=checkbox]").each(function(){ 

      if(isSelect) { 
      $(this).prop('checked', true); 

      SelectedItems+=$(this).val()+":"; 
      }else{ 
      $(this).prop('checked', false); 
      SelectedItems=""; 
      } 
      $("#Result").html(SelectedItems); 

     });   
}); 

});

0

這是我跟蹤數據表中所有選定行的方法。

http://www.datatables.net/release-datatables/examples/plug-ins/plugin_api.html

的想法是檢查當前頁面的用戶,選擇/取消當前正在觀看的行。您將遍歷頁面的行並將選定的行存儲到數組中以供將來參考。

通過這種方法,您可以限制選擇/取消選擇表中所有行所需的時間。

只顯示用戶正在看到的視圖需要什麼。

沒有掛斷,因爲表太忙檢查/取消選中每一行。

//stored checked box list 
var result = []; 
var selectAll = false; 

var table5 = $('#example5').dataTable({ 
    'fnDrawCallback':function(oSettings){ 

    var anNodes = this.oApi._fnGetTrNodes(oSettings); 
var anDisplay = $('tbody tr', oSettings.nTable); 

    //write your logic for pagination 
    //example 
    /* 
    $('tbody tr td:first-child input[type=checkbox]').on('click', function(){ 
     if($(this).is(':checked')){ 
     if($.inArray(....) != -1){push result} 
     }else{ 
     if($.inArray(....) != -1){splice result} 
     } 
    } 
    if(selectAll){ 
     for(var i = 0; i < anDisplay.length; i++){ 
      //check if it's in result array 
      //add to the array 
     } 
    }else{ 
     for(var i = 0; i < anDisplay.length; i++){ 
      //check if it's in result array 
      //delete from the array 
     } 
    } 
    */ 
    } 
}) 

這將是您跟蹤檢查行的邏輯。

相關問題