jqGrid复选框列 [英] jqGrid Checkbox column

查看:489
本文介绍了jqGrid复选框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a fairly complex grid with two columns formatted as a checkbox. Those columns are defined as follow:

{ 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" }

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

The custom formatter CheckBoxFormatter is needed because I need to setup the disabled attribute of each checkbox depending on some custom rules, so I borrowed the native 'checkbox' formatter and added my custom rules.

我也有一个外部html按钮元素,当我单击它时,我需要根据已选择复选框的组合来执行一些代码.我的代码如下:

I also have an external html button element and when I click on it I need to execute some code depending on which combination of checkbox selection have been made. My code looks like this:

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

,然后再使用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
}

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

Unfortunately I am experiencing the fact that the value of reminderA and reminderB variables does not reflect the exact state of the checkboxes but instead does always reflect the state of their initial values (e.g. when they have been loaded by the jqgrid plugin). In other words those values does'nt get updated when the user clicks on a checkbox in the grid.

这是获得结果的正确方法还是必须使用其他代码?有帮助吗?

Is this the right way to achieve my result or I have to use different code? Any help?

非常感谢!

推荐答案

可以很容易地解释您所遇到的问题.您使用启用的复选框(formatoptions:{disabled: false}),以便用户能够更改复选框的状态.问题是您使用自定义CheckBoxFormatter而不是原始的复选框"格式化程序.您使用的方法getRowData尝试调用未定义unformatter.因此,该复选框的值将使用$(cellval).text()获取(请参见

The problem which you have can be explained very easy. You use enabled checkboxes ( formatoptions:{disabled: false}), so the user are able to change state of the checkboxes. The problem is that you use your custom CheckBoxFormatter instead of original 'checkbox' formatter. The method getRowData which you use try to call unformatter which you not defined. So the value for the checkbox will be get using $(cellval).text() (see the source code of unformatter) and will be always empty.

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

So if you define your custom formatter and use methods like getRowData you have to define unformatter too.

就您而言,您根本不需要使用自定义格式化程序.您只需要为某些复选框定义disabled="disabled"属性,取决于某些自定义规则.因此,您只想为属性定义格式器. cellattr (请参见此处用法示例和

In your case you don't need to use custom formatter at all. What you need is just to define disabled="disabled" attribute for some checkboxes depend on some custom rules. So you want define only the formatter for the attributes. The cellattr (see here and here examples of the usage and my original feature request) is very simple to use and it's exactly what you need. For example it could be like the following

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

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

In the case you can use the original checkbox formater and unformatter and all will work correctly.

这篇关于jqGrid复选框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆