Javascript 如何在jqGrid中调用重置选择并全选?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3571324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:15:03  来源:igfitidea点击:

How to invoke the reset selection and select all in jqGrid?

javascriptjqueryjqgrid

提问by Paul

How to reset the selected rows and select all rows on external button click? i am trying to resetSelection() but not working ...

如何重置选定的行并在外部按钮单击时选择所有行?我正在尝试 resetSelection() 但不起作用...

jQuery("selectAll").click(function(){ 
  jQuery('.cbox').trigger('click'); 
});

jQuery("clear").click(function(){ 
  var grid = $("#list10"); 
  grid.resetSelection(); 
  $('#cb_my_grid').click(); 

  var ids = grid.getDataIDs(); 
  for (var i=0, il=ids.length; i < il; i++ ) 
    grid.setSelection(ids[i], false); 
});

回答by Oleg

The main reason why your code is not work is some syntax errors or wrong usage of jQuery selectors.

您的代码不起作用的主要原因是一些语法错误或jQuery 选择器的错误使用。

You don't post your HTML code, so I suppose it look like following

你没有发布你的 HTML 代码,所以我想它看起来像下面这样

<input id="selectAll" type="button" value="Select All" />
<input id="clear" type="button" value="Clear Selection" />
<table id="list10"></table>
<div id="pager"></div>

The corresponding JavaSript code should be like following:

对应的 JavaSript 代码应该如下所示:

var grid = $("#list10");
$("#selectAll").click(function(){
    grid.jqGrid('resetSelection');
    var ids = grid.getDataIDs();
    for (var i=0, il=ids.length; i < il; i++) {
        grid.jqGrid('setSelection',ids[i], true);
    }
});

$("#clear").click(function(){
    grid.jqGrid('resetSelection');
});

A working example you can see under the Link.

您可以在Link下看到一个工作示例。

回答by jerjer

For those who are still encountering this here is a solution the works for me:

对于那些仍然遇到这个问题的人来说,这是一个对我有用的解决方案:

//call resetSelection here

$('#cb_grid_id')
    .attr('checked','checked')
    .trigger('click')
    .attr('checked','checked');