javascript 如何知道 JqGrid 多选“全选”复选框被选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20295850/
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-10-27 18:10:08  来源:igfitidea点击:

how to know JqGrid multiselect 'select all' checkbox is checked

javascriptjquerycheckboxjqgrid

提问by Baby

Im trying to display the selected row in JQgrid. Now, i have this code:

我试图在 JQgrid 中显示选定的行。现在,我有这个代码:

onSelectRow:function(rowid){
    var selectedRow = $('#mygrid').jqGrid('getGridParam', 'selarrrow');
    $("#totalSelected").val(selectedRow .length); 
}

Its work fine, but then when the 'select all' checkbox is checked, it didn't trigger this code, eventhough in visual we can see that all row has been selected.

它的工作正常,但是当检查“选择所有”复选框时,它没有触发此代码,Visual中的EventHough我们可以看到已选择所有行。

so i'm thinking that if i know that the 'select all' checkbox is checked, i can set the total selected value like this:

所以我在想,如果我知道“全选”复选框被选中,我可以像这样设置总选定值:

  if(//selectall checkbox is checked) 
      $("#totalSelected").val($("#mygrid tr").length-1);

I've tried this code by refering to this answer, but im pretty sure .checked() is not recognized:

我已经通过参考这个答案尝试了这段代码,但我很确定 .checked() 不被识别:

  if($("#cb_mygrid"[0].id).checked()) 
      $("#totalSelected").val($("#mygrid tr").length-1);

Any idea is much appreciated

任何想法都非常感谢

回答by Brian Swift

onSelectRow does not fire when the header checkbox is used to select all items. There is a separate event handler to capture the Select All event. From the documentation:

当标题复选框用于选择所有项目时,onSelectRow 不会触发。有一个单独的事件处理程序来捕获 Select All 事件。从文档:

onSelectAll

全选

Sends you the following parameters: aRowids, status

向您发送以下参数:aRowids、状态

This event fires when multiselect option is true and you click on the header checkbox. aRowids is an array of the selected rows (rowid's). status is a boolean variable determining the status of the header check box - true if checked, false if not checked. Note that the aRowids array will always contain the ids whether the header checkbox is checked or unchecked.

当多选选项为 true 并且您单击标题复选框时会触发此事件。aRowids 是选定行(rowid's)的数组。status 是一个布尔变量,用于确定标题复选框的状态 - 如果选中则为 true,如果未选中则为 false。请注意,无论是否选中标题复选框,aRowids 数组都将始终包含 id。

When this event fires, in your case you'll want to do the following to show the correct count of selected rows:

当此事件触发时,在您的情况下,您需要执行以下操作以显示所选行的正确计数:

onSelectAll: function(aRowids, status) {
    //Use a ternary operator to choose zero if the header has just been unchecked, or the total items if it has just been checked.
    $("#totalSelected").val(status === false ? 0 : aRowids.length);
}