0

我有一个有六列的 jqGrid,每列都有一个“复选框”格式。我需要根据列名获取所有复选框的选中和未选中值。可能吗?

第一列是提供一个选项来选择所有剩余的列。我无法onclickonselect定义colModel.

$("#Grid").jqGrid({
    url: '@Url.Action("Access", "Authorization")' + '?role=' + encodeURIComponent($('input#hIDRole').val()),
    datatype: 'json',
    colNames: ["IDAccess","Permission", "ALL", "Read", "Add", "Edit", "Copy", "Delete"],
    colModel: [
        { name: 'IDAccess', index: 'IDAccess', width: 10, resizable: false, editable: false, hidden: true },
        { name: 'Permission', index: 'Permission', width: 100, resizable: false, editable: false, hidden: false },
        { name: 'ALL', index: 'ALL', editable: true, edittype: 'checkbox', editoptions: { value: "True:False" }, formatter: "checkbox", width: 50, resizable: false, formatoptions: { disabled: false }, onselect: "checkBox(this.value())" },
        { name: 'IsRead_Allowed', index: 'IsRead_Allowed', editable: true, edittype: 'checkbox', formatter: "checkbox", editoptions: { value: "True:False" }, width: 50, resizable: false, formatoptions: { disabled: false }, onclick: "checkBox(checked,this.value)" },
        { name: 'IsCreate_Allowed', index: 'IsCreate_Allowed', editable: true, edittype: 'checkbox', editoptions: { value: "True:False" }, formatter: "checkbox", width: 50, resizable: false, editable: true, formatoptions: { disabled: false }, onclick:"checkBox(event)"  },
        { name: 'IsUpdateAllowed', index: 'IsUpdateAllowed', editable: true, edittype: 'checkbox', editoptions: { value: "True:False" }, formatter: "checkbox", width: 50, resizable: false, editable: true, formatoptions: { disabled: false }, },
        { name: 'IsCopy_Allowed', index: 'IsCopy_Allowed', editable: true, edittype: 'checkbox', editoptions: { value: "True:False" }, formatter: "checkbox", width: 50, resizable: false, editable: true, formatoptions: { disabled: false } },
        { name: 'IsDeleteAllowed', index: 'IsDeleteAllowed', editable: true, edittype: 'checkbox', editoptions: { value: "True:False" }, formatter: "checkbox", width: 50, resizable: false, editable: true, formatoptions: { disabled: false } },
    ],
    //rowNum: 10,
    //rowList: [10],
    pager: "#pager-json",            
    autowidth: true,            
    loadComplete: function () {
        var rowIDs = $("#Grid").jqGrid('getDataIDs');
        for (var i = 0; i < rowIDs.length ; i++) {
            var rowId = rowIDs[i];
            var rowData = jQuery('#Grid').jqGrid('getRowData', rowId);
            //below code to check the All column if the other columns have true in the db. But once checked attribute is added i am not able to uncheck
            if ((rowData['IsRead_Allowed'] == "True") && (rowData['IsCreate_Allowed'] == "True") && (rowData['IsUpdateAllowed'] == "True")
                && (rowData['IsCopy_Allowed'] == "True") && (rowData['IsDeleteAllowed'] == "True")) {
                var check = $("#" + rowId).find('input[type="checkbox"]');
                check.attr('checked', 'checked');
            }
        }
        for (var i = 0; i < rowIDs.length; i++) {
            var rowData = rowIDs[i];
            if (rowData['IsCopy_Allowed'] == null) {
                //alert("1");
                var checkbox = $("#Grid" + rowData.i);
                //checkbox.css("visibility", "hidden");
                checkbox.attr("disabled", true);
            }
        }
    }
});
4

1 回答 1

0

您可以使用以下选择器来获取所有输入元素:

jQuery(".jqgrow td input", "#my_grid").each(function(){
        jQuery(this).unbind('click');
        jQuery(this).click(function(){
            ...
        });
});

input元素实际上将包含在 a 中td

<td ... aria-describedby="my-column"><input type="checkbox" ...></td>

您可以使用元素aria-describedby上的属性td来确定是否添加您的点击处理程序。就像是:

var col = jQuery(this).parent().attr('aria-describedby');
if (col === "IDAccess") {
    // Add handler here, etc...
}

您需要通过类似的练习来查找特定行中的所有复选框,尽管使用 ID 您可能能够限制搜索,即:

jQuery(".jqgrow td input", "#" + my_id)

或者,您也可以使用classescolmodel 选项为每一列分配一个唯一的类。例如:classes:'col1'。这将简化您设置点击处理程序的代码,并可能允许您完全避免使用该aria属性。

于 2013-10-10T14:59:53.673 回答