JqGrid - is it possible to filter jsonmap value in colModel

I am playing with jqGrid and wondering if the jsonmap value in colModel jqGrid can have a 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 the "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 accommodate more than one column and why it is suggested to add a filter , I have a problem displaying json with jsonmap with the following structure. That's 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 . JSON data may 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" } ] } ] } } 
0
source share
1 answer

You can use jsonmap as a function

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

Additionally, the jsonReader: { repeatitems: false } option is required for jqGrid.

You can read the JSON input you posted (see here ), but I still don't understand your suggestion. Why is the value of the "cells" property in the JSON input an object, not an array? Why is the "cells" property necessary at all? How do you imagine to place more as one column? As a rule, you see that in the jsonmap function you have access to the entire element from the "rows" array, so you can implement any algorithm for reading fields from data.

UPDATED : I wrote the following demo in which the latest version of the JSON data that you posted was read. The implementation idea remains the same - using jsonmap as a function.

The column model may be as follows

 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 getVaueByName method 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 ''; }; 
+3
source

Source: https://habr.com/ru/post/971228/


All Articles