jQgrid:禁用复选框和行(有条件)

时间:2015-05-19 07:52:30

标签: jquery jqgrid

基于从PHP数据数组传递的flag_status,我需要禁用行和复选框。另外,在多列选择列的标题中禁止选择已禁用的行以支持select all复选框

jQuery("#grid").jqGrid({
    url:call_url,
        datatype: "json",
    height: 'auto',
    rowNum: 20,
    rowList: [20,30,40],
    colNames:[<?php echo $col;?>],
    colModel:[
                {name:'USER_ID',index:'USER_ID', align:'center',search:false,hidden:true,key:true},
        {name:'PROJECT_NAME',index:'PROJECT_NAME', align:'center',search:false,hidden: true},
        {name:'EMP_NAME',index:'EMP_NAME', sortable:true,summaryType:'count',summaryTpl : 'Total ({0}) Resource Hours' },
        <?php for($i=1;$i<=count($cal_arr);$i++) {?>
        {name:'<?php echo $i;?>',index:'<?php echo $i;?>',search:false,align:"center",sortable:false ,width:80 },
        <?php } ?>
    ],
    pager: "#page",
        multiselect: true,
    shrinkToFit :true,
    autowidth: true,
    viewrecords: true,
    grouping: true, 
    groupingView : { groupField : ['PROJECT_NAME'], 
                    groupColumnShow : [false], 
                    groupText : ['<b>{0}</b>'],
                    groupCollapse : false, 
                    groupOrder: ['asc'], 
                    groupSummary : [true], 
                    showSummaryOnHide: true,
                    groupDataSorted : true },
    sortname: 'EMP_NAME',
    caption: "Programatically block selection of some grid row',
    gridComplete: function () {
                    var recs = $("#grid").getGridParam("records");
                    $( ".mycontent" ).remove();
                    if (recs == 0 || recs == null) {
                        $('#grid').after("<div class='mycontent' style='color:red;text-align:center'>No Record Found</div>");
                        $("#btn_submit").hide();
                    }
            },


            loadComplete: function() {
                // we make all even rows "protected", so that will be not selectable
                var cbs = $("tr.jqgrow > td > input.cbox:even", jQuery("#grid")[0]);
                cbs.attr("disabled", "disabled");
            },
            beforeSelectRow: function(rowid, e) {
                var cbsdis = $("tr#"+rowid+".jqgrow > td > input.cbox:disabled", jQuery("#grid")[0]);
                if (cbsdis.length === 0) {
                    return true;    // allow select the row
                } else {
                    return false;   // not allow select the row
                }
            },
            onSelectAll: function(aRowids,status) {
                if (status) {
                    // uncheck "protected" rows
                    var cbs = $("tr.jqgrow > td > input.cbox:disabled", jQuery("#grid")[0]);
                    cbs.removeAttr("checked");

                    //modify the selarrrow parameter
                    jQuery("#grid")[0].p.selarrrow = jQuery("#grid").find("tr.jqgrow:has(td > input.cbox:checked)")
                        .map(function() { return this.id; }) // convert to set of ids
                        .get(); // convert to instance of Array
                }
            }

})

感谢@Oleg for small demo,但不完全符合以下要求。

key:true无法使用flag_status,因为{b}用于user_id

此处它会禁用所有偶数行,因此无法选择,但只有在flag_status = 1的值时才需要禁用。如何在jQgrid中传递该值并禁用行(添加类ui-state-disabled)并阻止选中复选框(也禁用它)?

HTML结构:

<tr class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" id="13234" role="row" aria-selected="false">
<td aria-describedby="grid_cb" style="text-align:center;width: 20px;" role="gridcell">
<input type="checkbox" name="jqg_grid_13234" class="cbox" id="jqg_grid_13234" role="checkbox" disabled="disabled">
</td>
</tr>

此处USER_ID设置在<tr><input>,而flag_status应设置在<tr>USER_ID <input>上。如果flag_status值为1,则应将ui-state-disabled类添加到<tr>,以禁用<input>

版本 - jqGrid 4.6.0

1 个答案:

答案 0 :(得分:4)

我引用的演示已经很老了。我将其修改为the following,这更符合您的要求。

我在演示中使用

rowattr: function (item) {
    if (parseInt(item.ID, 10) % 3 === 0) {
        return {"class": "ui-state-disabled ui-jqgrid-disablePointerEvents"};
    }
},
beforeSelectRow: function (rowid, e) {
    if ($(e.target).closest("tr.jqgrow").hasClass("ui-state-disabled")) {
        return false;   // not allow select the row
    }
    return true;    // allow select the row
}

ui-jqgrid-disablePointerEvents有以下CSS规则:

.ui-jqgrid-disablePointerEvents {
    pointer-events: none;
}

您可以轻松修改rowattr的代码,以便根据我在上述演示中使用的item.flag_status而非item.ID的值禁用行。