jqGrid复选框列

Lor*_*nzo 3 jquery jquery-ui jquery-plugins jqgrid jqgrid-asp.net

我有一个相当复杂的网格,其中两列格式化为复选框.这些列定义如下:

{ name: 'Alert_A', index: 'Alert_A', width: 22, align: 'center', sortable: false,
    formatter: CheckBoxFormatter, editable: true, edittype: 'checkbox', editoptions: {value: "True:False"}, 
    formatoptions: {disabled: false}, classes: "Alert_A" },
{ name: 'Alert_B', index: 'Alert_B', width: 22, align: 'center', sortable: false,
    formatter: CheckBoxFormatter, editable: true, edittype: 'checkbox', editoptions: { value: "True:False" },
    formatoptions: {disabled: false}, classes: "Alert_B" }
Run Code Online (Sandbox Code Playgroud)

CheckBoxFormatter需要自定义格式化程序,因为我需要根据一些自定义规则设置每个复选框的disabled属性,因此我借用了原生的"复选框"格式化程序并添加了我的自定义规则.

我还有一个外部的html按钮元素,当我点击它时,我需要执行一些代码,具体取决于复选框选择的组合.我的代码看起来像这样:

$('#btnAlert').button().click(function (event) {
    event.preventDefault();
    var dashboardID = '#<%=dashboard.ClientID %>';
    doWork(dashboardID);
});
Run Code Online (Sandbox Code Playgroud)

以及后来的doWork功能

var keys = $(dashboardID).getDataIDs();
for (var i = 0; i < keys.length; i++) {
    var rowData = $(dashboardID).getRowData(keys[i]);
    ...
    var reminderA = $(rowData.Alert_A).is(":checked");
    var reminderB = $(rowData.Alert_B).is(":checked");
    ...
    ... other application logic here
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我遇到的事实是,reminderAreminderB变量的值并不反映复选框的确切状态,而是始终反映其初始值的状态(例如,当它们被jqgrid插件加载时).换句话说,当用户单击网格中的复选框时,这些值不会更新.

这是实现我的结果的正确方法还是我必须使用不同的代码?有帮助吗?

非常感谢!

Ole*_*leg 5

您可以解决的问题非常简单.您使用enabled复选框(formatoptions:{disabled: false}),因此用户可以更改复选框的状态.问题是您使用自定义CheckBoxFormatter而不是原始的"复选框"格式化程序.getRowData您使用的方法尝试调用unformatter未定义的方法.因此,复选框的值将被使用$(cellval).text()(请参阅unformatter的源代码)并且将始终为空.

因此,如果您定义自定义格式化程序并使用类似于getRowData必须定义unformatter的方法.

在您的情况下,您根本不需要使用自定义格式化程序.您需要的只是disabled="disabled"为某些复选框定义属性取决于某些自定义规则.因此,您只需要为属性定义格式化程序.该cellattr(见这里这里使用的例子,我原来的功能要求)使用起来非常简单,它正是你需要的.例如,它可能如下所示

cellattr: function (rowId, value, rawObject) {
    if (rawObject.deliveryStatus !== "sent") {
        return '';
    } else {
        return ' disabled="disabled"';
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您可以使用原始复选框格式化器和unformatter,所有这些都将正常工作.