Is it possible to modify the colModel after data has loaded with jqgrid?

前端 未结 1 1571
忘掉有多难
忘掉有多难 2020-11-30 13:29

I\'ve got a jqGrid where I need to change the model after the data is loaded, but before it\'s parsed into the grid. In otherwords, I think I want to do it in the loadComple

1条回答
  •  情书的邮戳
    2020-11-30 13:58

    I found one way which seems work OK.

    The idea of my solution is following. You use colModel having many hidden columns with the dummy names like 'cm0', 'cm1', 'cm2', ... All the columns has the same data like you need in your case. To fill the data more easy I use column templates existing since jqGrid 3.8.2:

    var mygrid=jQuery("#list"),
        cmIntTemplate = {
            width:50,
            sorttype:"int",
            formatter:"integer",
            align:"right",
            hidden:true
        },
        cm = [
            // here we define the first columns which we always has
            // the list can be empty or has some columns with
            // the properties other as the rest (without cmIntTemplate)
            {name:"date",label:"Date",key:true,width:100, fixed:true,
             formatter:'date',formatoptions:{srcformat:"m-d",newformat:"m/d"}}
        ], maxCol = 30, dummyColumnNamePrefix = "cm";
    
    // Add dummy hidden columns. All the columns has the same template
    for (i=0;i

    After that I create jqGrid in the standard way, but with the jsonReader which use the page as function:

    jsonReader: {
        repeatitems: false,
        page: function (obj) {
            // ------------------------
            // here I add the main code
            // ------------------------
            return obj.page;
        }
    }
    

    The function from the jsonReader.page return the same value like as do default jsonReader, but I use the way with function because the function will be called directly before the reading of the main contain of JSON data. Inside of the code I get the first row of the data and use it's property names to fill jsonmap property of the corresponding column and set the column name. Additionally I make some dummy columns needed to display all the JSON data visible and the rest dummy column hidden. The last thing which should be done is correction of the grid width which was calculated previously. So the grid will look like this:

    enter image description here

    or like this

    enter image description here

    depend on the JSON input data.

    The code of the page function is following:

    page: function (obj) {
        var rows = obj.rows, colModel = mygrid[0].p.colModel,
            cmi, iFirstDummy, firstRow, prop,
            orgShrinkToFit, isFound,
            showColNames = [], hideColNames = [];
    
        if (typeof(rows) === "undefined" || !$.isArray(rows) || rows.length === 0) {
            // something wrong need return
            return obj.page;
        }
    
        // find the index of the first dummy column
        // in the colModel. If we use rownumbers:true,
        // multiselect:true or subGrid:true additional
        // columns will be inserted at the begining
        // of the colModel
        iFirstDummy = -1;
        for(i=0;i

    Inside of the page function I use small helper functions

    var clearShrinkToFit = function() {
            // save the original value of shrinkToFit
            var orgShrinkToFit = mygrid.jqGrid('getGridParam','shrinkToFit');
            // set shrinkToFit:false to prevent shrinking
            // the grid columns after its showing or hiding
            mygrid.jqGrid('setGridParam',{shrinkToFit:false});
            return orgShrinkToFit;
        },
        setGridWidthAndRestoreShrinkToFit = function(orgShrinkToFit) {
            // calculate the new grid width
            var width=0, i=0, headers=mygrid[0].grid.headers, l=headers.length;
            for (;i

    The working demo you can see here.

    UPDATED: Another answer with the demo shows how to create the grid which has another format of input data: [[], [], ...] (array of arrays) - matrix.

    0 讨论(0)
提交回复
热议问题