2013-03-05 38 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 
     } 
    } 
    */ 
    } 
}) 

这将是您跟踪检查行的逻辑。

相关问题