0

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"
                    }
                ]
            }
        ]
    }
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
techlead
  • 779
  • 7
  • 24
  • 44

1 Answers1

3

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 '';
    };
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • **Why the value of "cells" property in the JSON input is object and not an array?** - I tried it as an array but data is not displayed using the colModel above. Please see updated post under the heading _"The value of cells property in the JSON input as an array"_ – techlead Nov 16 '11 at 15:10
  • **How you imagine to place more as one column in the way?** - please see my updated post under the heading _"To place more than one column and why the suggestion of adding a filter"_. Yes, I'm unable to have more than 1 column and have data displayed as per the colModel – techlead Nov 16 '11 at 15:18
  • 1
    @SK11: If you use `"cells":{}` the `cells` will be *object* with properties. All properties must have **different names**. Your current JSON is buggy. If you use `"cells":[]` the `cells` will be *array* and you can has items with the same properties, but the usage of `{"cell":{ "value":"Mark", "colname": "fname"}}` as items will be syntax error the following is possible: `{"cells": [ {"value": "Mark", "colname": "fname"}, {"value": "Strong", "colname": "lname"}, {"value": "Hourly", "colname": "emptype"}]}`. – Oleg Nov 16 '11 at 15:31
  • @SK11: On the other side you can place the same information in more compact way as for example `{"columns": ["fname", "lname", "emptype"], "values": ["Mark", "Strong", "Hourly"]}`. – Oleg Nov 16 '11 at 15:32
  • I changed JSON structure to `{"cells": [ {"value": "Mark", "colname": "fname"}, {"value": "Strong", "colname": "lname"}, {"value": "Hourly", "colname": "emptype"}]}`. **jsonMap:cells.value** not working. – techlead Nov 16 '11 at 15:42
  • @SK11: It should not working now because if you change format of input JSON data you have to change the `jsonmap`. `cells.0.value`, `cells.1.value` and `cells.2.value` will get you the values. Nevertheless I still can absolutely not understand why you do all above changes. Two standard representation `{"cells": {"fname": "Mark", "lname": "Strong", "emptype": "Hourly"}}` or {"cells": ["Mark", "Strong", "Hourly"]} are compacter and has the same information. – Oleg Nov 16 '11 at 15:58
  • You told me that my JSON is buggy and to have it as an array. So, I went ahead and made the change as per your suggestion. Now you're telling me why I made the change. What's the problem? – techlead Nov 16 '11 at 16:01
  • I cannot have the JSON structured as per the **Two standard representation** `{"cells": {"fname": "Mark", "lname": "Strong", "emptype": "Hourly"}} or {"cells": ["Mark", "Strong", "Hourly"]}` as there is much more to JSON data than I can share here. – techlead Nov 16 '11 at 16:05
  • @SK11: I just tried to follow your suggestion, but you must to make changes in JSON to have no syntax errors. I recommend you always validate JSON in http://jsonlint.com/ before you use it as a proposal. If you need to provide more information in JSON you should write JSON examples having the additional data. You should better *specify the information* and not just write "there is much more to JSON data than I can share here". – Oleg Nov 16 '11 at 16:15
  • One last thing, Oleg. I'm trying to wrap `rows' with another element such that **{"wrapper":{"rows":[{"cells": [ {"value": "Mark", "colname": "fname"}, {"value": "Strong", "colname": "lname"}, {"value": "Hourly", "colname": "emptype"}]}]}}**. Now, no data is getting displayed. – techlead Nov 16 '11 at 16:24
  • @SK11: OK I wrote [one more demo](http://www.ok-soft-gmbh.com/jqGrid/SK11-name-value1.htm) for you which can read the last version of data which you posted. – Oleg Nov 16 '11 at 16:47
  • So that community members can benefit from our discussion on this thread as well as from your demo, should I be clicking on the tick mark on your very first answer where you say `You can use jsonmap as a function`? – techlead Nov 16 '11 at 17:06
  • @SK11: You can include the last version of JSON data in the text of your question and I'll add the code fragments from the last demo and the link in the **"UPDATED"** part of my answer. – Oleg Nov 16 '11 at 17:10
  • @SK11: I posted the last changes in my answer and in your question. You should modify the text over the **"UPDATED"** part of yoou question like you as want. – Oleg Nov 16 '11 at 17:17
  • I've added the last version of JSON data in the text of my question. BTW, how are you able to write "@SK11:"? I've been trying to write "@Oleg" in my comments but it just gets deleted. – techlead Nov 16 '11 at 17:17
  • @SK11: If you write "@Oleg" in the comment to *my* answer it will be removed because the comments will be addressed to my by default. If I would be write "@SK11" in the comment to *your* question it will be also removed to save the size of comment which is restricted. – Oleg Nov 16 '11 at 17:21