priya priya - 16 days ago 7
jQuery Question

jqGrid v4 - dynamic column not working

Controller Code:

public JsonResult GetGridColumn()
{
var jsonData = new
{
colNames = "['actname']",
colModel = "[{ name: 'actionTaken'}]",
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
}


View Code :

$(document).ready(function () {
$.ajax({
type: "POST",
url: "/GetForm/GetGridColumn",
data: "",
datatype: "json",
success: function (data) {
colN = data.colNames;
colM = data.colModel;
$("#taskGrid").jqGrid({
url: "someurl",
datatype: 'jsonstring',
mtype: 'POST',
colNames: colN,
colModel: colM,
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30, 40],
height: '100%',
viewrecords: true,
width: 1250,
jsonReader: {
cell: "",
id: "0"
}
})
}
});
});


I get colN as ['actname'] and colM as [{ name: 'actionTaken'}]. When I run the above script I get this error Length of colNames<> colModel!

Answer

try something like

var jsonData = new {
        colNames = new [] {"actname"},
        colModel = new [] { new { name = "actionTaken" } }
    };

In any way jsonData.colNames and jsonData.colModel should be arrays and not strings. jqGrid verify that both arrays has the same number of elements, but you use colNames and colModel as strings and the test if (p.colNames.length !== p.colModel.length) { compares the length of the strings in your case. It's the reason of misunderstandable error message.

Comments