jqGrid - is it possible to filter the value of jso

2019-02-20 21:17发布

问题:

I'm playing around with jqGrid and am wondering if the value of jsonmap in the colModel of jqGrid can have filtered value.

ColModel

colModel:[
   {name:'fname',index:'action',jsonmap:'cells.cell.colname["fname"].value', width:50, align:"center", resizable:false}
]

JSON

{   
    "rows":[
        {

            "cells":
            {               
                "cell":{
                    "value":"Mark",
                    "colname": "fname"
                }
            }
        }
   ]
}

The value of "cells" property in the JSON input as an array

{   
    "rows":[
        {

            "cells":[
            {               
                "cell":{
                    "value":"Mark",
                    "colname": "fname"
                }
            }]
        }
   ]
}

with the following colModel does not work

colModel:[
   {name:'fname',index:'action',jsonmap:'cells.cell.value', width:50, align:"center", resizable:false}
]

To place more than one column and why the suggestion of adding a filter - I have a problem mapping json with jsonmap with the following structure. That is why I asked if we could add a filter.

{   
    "rows":[
        {

            "cells":
            {               
                "cell":{
                    "value":"Mark",
                    "colname": "fname"
                },
                "cell":{
                    "value":"Strong",
                    "colname": "lname"
                },
                "cell":{
                    "value":"Hourly",
                    "colname": "emptype"
                }
            }
        }
   ]
}

UPDATED: The JSON data can be

{
    "wrapper": {
        "rows": [
            {
                "cells": [
                    {
                        "value": "Mark",
                        "colname": "fname"
                    },
                    {
                        "value": "Strong",
                        "colname": "lname"
                    },
                    {
                        "value": "Hourly",
                        "colname": "emptype"
                    }
                ]
            },
            {
                "cells": [
                    {
                        "value": "Mark2",
                        "colname": "fname"
                    },
                    {
                        "value": "Strong2",
                        "colname": "lname"
                    },
                    {
                        "value": "Hourly2",
                        "colname": "emptype"
                    }
                ]
            }
        ]
    }
}

回答1:

You can use jsonmap as a function

jsonmap: function (item) {
    // item.cells.cell.colname is "fname"
    return item.cells.cell.value;
}

The option jsonReader: { repeatitems: false } of jqGrid is additionally required.

One can read the JSON input which you posted (see here), but I still don't understand your suggestion. Why the value of "cells" property in the JSON input is object and not an array? Why the property "cells" are needed at all? How you imagine to place more as one column in the way? In general you see that in the jsonmap function you have access to the whole item from the "rows" array, so you can implement any algorithm of reading of fields from the data.

UPDATED: I wrote the next demo which read the last version of JSON data which you posted. The idea of implementation stay the same - the usage of jsonmap as a function.

The column model can be the following

colModel: [
    {name: 'fname', jsonmap: function (obj) { return getVaueByName(obj.cells, "fname"); }},
    {name: 'lname', jsonmap: function (obj) { return getVaueByName(obj.cells, "lname"); }},
    {name: 'emptype', jsonmap: function (obj) { return getVaueByName(obj.cells, "emptype"); }}
],
cmTemplate: {width: 70, align: "center", resizable: false},
gridview: true,
height: 'auto',
jsonReader: {
    root: "wrapper.rows",
    page: function () { return 1; },
    total: function () { return 1; },
    repeatitems: false
}

where the method getVaueByName will be defined as

var getVaueByName = function (cells, colName) {
        var i, count = cells.length, item;
        for (i = 0; i < count; i += 1) {
            item = cells[i];
            if (item.colname === colName) {
                return item.value;
            }
        }
        return '';
    };


标签: json jqgrid