简体   繁体   English

jqgrid中的多个复选框列。 处理onchange事件

[英]Multiple checkbox columns in jqgrid. Handle onchange event

I am creating a jqgrid with editable fields. 我正在创建一个具有可编辑字段的jqgrid。 I have 2 checkbox columns in jqgrid, one is coming from multiselect: true (to get unique rowId), other column is created inside column model. 我在jqgrid中有2个复选框列,一个来自multiselect:true(获取唯一的rowId),另一列在列模型内部创建。

I want to handle the onchange(checked/unchecked) event of the checkbox in my column model, independent of other checkbox column(multiselect:true). 我想处理我的列模型中复选框的onchange(选中/未选中)事件,独立于其他复选框列(multiselect:true)。 Any help appreciated. 任何帮助表示赞赏。 Below is the code snippet. 下面是代码片段。

[{name : "userRole", label: 'OV', width: 40, editable:true, edittype:'checkbox',formatter: 'checkbox', editoptions: {value:"True:False"},
 formatoptions: { disabled: false},frozen:true}]
multiselect: true,
 onSelectRow: function(rowid){ 
             jQuery(this).editRow(rowid, true);
            }

You can use beforeSelectRow callback. 您可以使用beforeSelectRow回调。 The demo demonstrate the approach. 该演示演示了该方法。 It uses the following code 它使用以下代码

beforeSelectRow: function (rowid, e) {
    var $target = $(e.target), $td = $target.closest("td"),
        iCol = $.jgrid.getCellIndex($td[0]),
        colModel = $(this).jqGrid("getGridParam", "colModel");
    if (iCol >= 0 && $target.is(":checkbox")) {
        alert("checkbox is " +
              ($target.is(":checked")? "checked" : "unchecked") +
              " in the column \"" + colModel[iCol].name +
              "\" in the row with rowid=\"" + rowid + "\"");
    }
    return true;
}

Define your own formatter function like this in your colmodel, 在colmodel中定义自己的格式化程序功能,

[{name : "userRole", label: 'OV', width: 40, 
 editable:true, edittype:'checkbox',formatter: checkboxFormatter, 
  editoptions: {value:"True:False"},

And your formatted checkbox like, 和格式化的复选框,例如

function checkboxFormatter(cellvalue, options, rowObject) {
return "<input type='checkbox' name='checkboxIsCC' 
              onchange='your_own_function();'>";
}

Hope this helps you. 希望这对您有所帮助。

I had an issue were not only I had more than 1 check box but also I had to update same column check box values based on the selection of the check box as well as modifying the same row columns. 我有一个问题,不仅我有多个复选框,而且还必须根据对复选框的选择以及修改相同的行列来更新同一列的复选框值。

In regards to the modification of the other checkboxes, when jqgrid modifies the data either by 'setCell' or 'setRowData' operations, it removes the click event. 关于其他复选框的修改,当jqgrid通过“ setCell”或“ setRowData”操作修改数据时,它将删除click事件。 Also there is the problem that for checkboxes none of the edit functions are useful. 还有一个问题是,对于复选框,没有任何编辑功能有用。

I manage to get bits from other people's solution and came to use the delegate jquery functions, that allows the binding of the click to be done each time an object that matches the selector is created. 我设法从其他人的解决方案中获取一些信息,并开始使用委托jquery函数,该函数允许每次创建与选择器匹配的对象时完成单击的绑定。 Also in this case only 1 checkbox of only 1 column could be checked at a time. 同样在这种情况下,一次只能选中1列的1个复选框。

$(document).delegate("#alarmDownloadListView td[aria-describedby*='stopArm'] input", 'click', function () { 
// Function that modifies all the other checkboxes of the same column 
    deselectOthersStopArmAlarms(this, j);
    // Set the Pre and Pos Alarm values to default
    var fileIndex = $(this).closest('tr').index();
// Modification of the same row cells 
    if($(this).is(':checked')){
        alarmsGrid.jqGrid('setCell',fileIndex,'preAlarm', defaultPrePosStopArmAlarmValue);
    }else{
        alarmsGrid.jqGrid('setCell',fileIndex,'preAlarm', null);
    }
});

Do not mind the exact operations of the code, what is important is the operations that the binded function does. 不要介意代码的确切操作,重要的是绑定函数执行的操作。 The CSS selector binds this function to all checkboxes whose name in my colmodel is stopArm. CSS选择器将此功能绑定到在我的colmodel中名称为stopArm的所有复选框。

I hope this answer is useful for some people. 我希望这个答案对某些人有用。 I found the delegate to be very useful! 我发现代表非常有用! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM