Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jquery - jQgrid: multiselect true - make each row checked by default on page load

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 () {
            $("#cb_grid").click(); 
        },

            rowattr: function (item) {
                    if (parseInt(item.ID) == 1) {
                        return {"class": "ui-state-disabled ui-jqgrid-disablePointerEvents"};
                    }
            },

            //prevent selection of disabled rows
            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
            }

})

Above code does this

Does this

Required is

This is required

Using jqGrid with multiselect:true how to set each row checked by default on page load ? jQgrid Version - 4.6

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you use jqGrid 4.6 with datatype: "json" and no loadonce: true option then you have not so much possibilities. You have to enumerate all multiselect checkboxes of the grid and select there. So you can do

$("#cb_grid").click(); // "grid" is the grid id

inside of loadComplete callback for example. If you would think about all cases you will probably add more additional criteria, because loadComplete will be called on sorting, paging and so on.

UPDATED: You code have $('.cbox').attr('checked', true); line inside of rowattr. In the way you don't change the checkbox of the row, which is not yet exist. Instead of that you set checked attribute with wrong value true instead of "checked" on the checkbox of the column header. You should remove the line to be able to use the code which I suggested you:

loadComplete: function () {
    $("#cb_" + this.id).click();
},
rowattr: function (item, rd, rowid) {
    //$('.cbox').attr('checked', true);
    if (parseInt(rowid) === 10) {
        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
}

see the demo: http://jsfiddle.net/OlegKi/aagxejj5/4/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...