Slimshadddyyy
Slimshadddyyy

Reputation: 4073

jQgrid: Disable checkbox and row (conditional)

Based on flag_status passed from PHP data array, I need to disable the row and checkbox. Additionally prevent selection of disabled rows to support select all checkbox in the header of the multiselect column

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

})

Thanks to @Oleg for small demo, but does not completely meet the below requirements.

Can not use key:true for flag_status since its bieng used for user_id

Here it disables all even rows, so that will be not selectable, but need to disable only if value of flag_status = 1. How to pass that value in jQgrid and disable row (adding class ui-state-disabled) and prevent checkbox being selected (making it disabled too) ?

HTML structure:

<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>

Here USER_ID is set on <tr> and <input>, instead flag_status should set on <tr> and USER_ID on <input>. If flag_status value is 1, class ui-state-disabled should be added to that <tr> making the <input> disabled.

Version - jqGrid 4.6.0

Upvotes: 1

Views: 11018

Answers (1)

Oleg
Oleg

Reputation: 221997

My demo which you referenced is already really old. I modified it to the following which more corresponds your requirements.

I uses in the demo

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
}

where there are the following CSS rule for the class ui-jqgrid-disablePointerEvents:

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

You can easy modify the code of rowattr so that you disable the rows based on the value of item.flag_status instead of item.ID which I uses in the above demo.

Upvotes: 5

Related Questions