Search code examples
jqueryjqgrid

Customise(Pivot) display of a Jqgrid with a Nested JSON object


I have a JSON

{"empId":"4444","skill":{"rating":"1","review":"Great"},"productivity":{"rating":"2","review":"Good"}};

Here is my colModel of the jqgrid code as of now.

colModel: [
{name:'skill.rating',label:'Skill Rating', width:150,editable: true},
{name:'skill.review',label:'Skill Review', width:150,editable: true},
{name:'productivity.rating',label:'Productivity Rating',width:150,editable: true},
{name:'productivity.review',label:'Productivity Review', width:150,editable: true}]

And this is how my grid looks like now My JqGrid

But this grid design makes it hard for future scaling, in case there is a new category Reliability review and rating then the grid design has to be changed manually.

Instead i want to make it look in this format in the grid

Category      ||  Rating   || Review
----------------------------------
Skill         ||  1        || Great
Productivity  ||  2        || Good
Reliability   ||  3        || Average

Is there a way to do this with Jqgrid without having to change the structure of the JSON object(as I have no control on the object, i only fetch it through a service)??


Solution

  • It is better to manipulate the jsonReader (which can be a function) rather than to change the colModel. See the Guriddo docs for detailed explanation

    Try with the following code:

    colModel: [
        {name:'category',label:'Category', width:150,editable: true},
        {name:'rating',label:'Skill Rating', width:150,editable: true},
        {name:'review',label:'Skill Review', width:150,editable: true}
    ],
    jsonReader : {
        root : function (data) {
            var ret  = [];
            for( var key in data) {
                if($.isPlainObject(data[key])) {
                    var tmp = data[key];
                    tmp.category = key;
                    ret.push(tmp);
                }
            }
            return ret;
        }
    },
    

    UPDATE

    Below the working code

    HTML

    <table id="jqGrid"></table>
    <div id="jqGridPager"></div>
    

    JavaScript

    var mydata ={"empId" : "4444", "skill" : "rating":"1", "review":"Great"}, "productivity" : {"rating":"2","review":"Good"}, "reliability" : {"rating":"3","review":"Very Good"}};
    
    
            $("#jqGrid").jqGrid({
                datastr : mydata,
                datatype: "jsonstring",
                colModel: [
                    {name:'category',label:'Category', width:150,editable: true},
                    {name:'rating',label:'Skill Rating', width:150,editable: true},
                    {name:'review',label:'Skill Review', width:150,editable: true}
                ],
                jsonReader : {
                    root : function (data) {
                        var ret  = [];
                        for( var key in data) {
                            if($.isPlainObject(data[key])) {
                                var tmp = data[key];
                                tmp.category = key;
                                ret.push(tmp);
                            }
                        }
                        return ret;
                    }
                },
                viewrecords: true,
                width: 780,
                rowNum: 15,
                //cellEdit : true,
                rownumbers: true, // show row numbers
                rownumWidth: 25, // the width of the row numbers columns
                pager: "#jqGridPager"
            });
    

    Here the link to jsfiddle example datatype is jsonstring, but it it the same as json. This set is mainly for jsfiddle to work.