0

In above code attrSetting is called. If I change it to {"name":"A", "index":"0", "cellattr":attrSetting}. It runs fine. So what should I do? The cellattr treats it as string not as a function.

var gridData = {"list":[{"A":"abc", "B":"def", "C":"IRIS", "D":"Testing","E":"17-12-                 2012","F":"Test", "attr":{"A":{"rowspan": 3}}},{"A":"abc", "B":"def", "C":"IRIS", "D":"Testing","E":"17-12-2012","F":"Test", "attr":{"A":{"display":"none"}}},{"A":"abc", "B":"def", "C":"IRIS", "D":"Testing","E":"17-12-2012","F":"Test", "attr":{"A":{"display":"none"}}}]};
$(document).ready(function(){
prepareGrid();
});
prepareGrid = function(colModel) {
$("#grid").jqGrid({
    datatype    :   'local',
    contentType :   'application/json',
    data        :   gridData.list,
    loadtext    :   "Loading...",
    colNames    :   ['TB Element','GL Element', 'Company Name', 'Status', 'Date', 'User'],
    colModel    :   [
                     {"name":"A", "index":"0", "cellattr":"attrSetting" },
                     {name:"B", index:1 },
                     {name:"C", index:2},
                     {name:"D", index:3},
                     {name:"E", index:4},
                     {name:"F", index:5}
                    ],
    width       :   '500px',
    height      :   '200px',
    rownumWidth :   30,
    scrollrows  :   true,
    shrinkToFit :   false,
    rownumbers  :   true,
    viewrecords :   true,
});
};
function attrSetting(rowId, val, rawObject, cm) {
   var attr = rawObject.attr[cm.name], result;
   if (attr.rowspan) {
      result = ' rowspan=' + '"' + attr.rowspan + '"';
   } else if (attr.display) {
      result = ' style="display:' + attr.display + '"';
   }
   return result;
};
Mitesh
  • 477
  • 1
  • 6
  • 22

1 Answers1

0

You should just change

"cellattr":"attrSetting"

to

"cellattr": attrSetting

Additionally you should be carefully in usage of jqGrid options. You current code contains many bugs. Just some example:

  • If you use datatype: "local" you should remove index properties from colModel or use that values which are exact the same as the value of name property. If you don't follow the rule you will be unable to sort the grid columns and the searching/filtering of local data will not work too.
  • You don't use pager or toppager options of jqGrid. In the case I strictly recommend you to specify rowNum option with some large enough value like rowNum: 10000. The defauls value of rowNum is 20 (see "Default" column in the table with the options). So jqGrid will display just the first 20 rows from the array gridData.list which you specify.
  • The values of width and height should be numbers like 500 or 200 instead of strings like '500px' and '200px'. The value of height can be the string "auto" or "%100".
  • There are no contentType option.
  • I recommend you to use gridview: true and autoencode: true options.
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Tanks for your suggestions. But my colModel comes from java which is dynamically made from database. There is no single function like `attrSetting`, I've 5 different functions. When I prepare the json for this call model, It takes string it self automatically. **I'm Making this json using jackson** – Mitesh Jun 26 '13 at 19:25
  • @Mitesh: You are welcome! Look at [the answer](http://stackoverflow.com/a/5175127/315935) which describes a workaround which you can use. – Oleg Jun 26 '13 at 20:53