Asp.net mvc 如何选择jquery数据表中的所有行
我有两个按钮在我的页面选择全部和取消选择全部。我需要在单击全选按钮并将id列表保存在变量中时选择所有行。单击取消选择ll按钮时,应取消选择所有行。我们将如何做到这一点Asp.net mvc 如何选择jquery数据表中的所有行,asp.net-mvc,jquery,datatables,Asp.net Mvc,Jquery,Datatables,我有两个按钮在我的页面选择全部和取消选择全部。我需要在单击全选按钮并将id列表保存在变量中时选择所有行。单击取消选择ll按钮时,应取消选择所有行。我们将如何做到这一点 [HttpPost] public ActionResult Action Name(pssing value) { DataTable dataTable = new DataTable(); // my code
[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);
}
单击“全选”复选框时,无法将值绑定到控制器 试试这个
$('table#tableId> tbody > tr').addClass('highlight');
$('table#tableId> tbody > tr').removeClass('highlight')addClass('noHeilight');
样本表
您应该为复选框设置id attr,并将行id放入id attr中,并使用以下代码选择和取消选择: $document.readyfunction{
$("#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);
});
});
}) 这是我跟踪数据表中所有选定行的方法 其思想是检查用户所在的当前页面,并选择/取消选择当前正在查看的行。您可以在页面的行中循环,并将所选行存储到一个数组中以供将来参考 使用这种方法可以限制选择/取消选择表中所有行所需的时间 仅根据用户看到的视图显示所需内容 没有挂断,因为表正忙于检查/取消选中每一行
//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
}
}
*/
}
})
这就是跟踪选中行的逻辑 粘贴你的代码,那会更有帮助我什么都没做。。。。。。。。什么我们无法为您生成。请查看我最近的帖子我知道这一点,但如何存储选定的行id?请不要回答我的问题我理解。如果您向代码显示如何设置该id,那么我们可以提供解决方案,只要我从控制器绑定表即可。我有列Id,test1,test2。。。。。。。。。。。。。等等。然后我需要使用id值将我的值传递给我的控制器。请查看我最近的帖子。但是我需要在我的控制器中传递所选行,那么如何获取所选行id呢?可怜的家伙,当你选择所有行时,你应该使用jquery选择器获取包含id的元素并将其保存在数组中。我知道。请查看我最近的帖子
<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");
}
});
});
$("#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);
});
});
//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
}
}
*/
}
})