0

I ve data in the JSON format given below,need to populate the slick grid columns with the data from cols and rows from values.. Could u please help me with the loops required to do so ....

var response = { "cols" :  ["name", "Precentage", "Year", "Amount"],
"rows": [{
"flag": true,
"values": [" name1", "Precentage1", "year1", "Amount1"]
}
Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
Diya4ever
  • 102
  • 1
  • 5
  • 13

1 Answers1

0

There may be a better way to do this, but you could just loop through and build the data array by hand, something like this:

var colName;
var data = [];

for (var i = 0; response.cols.length; i++) {
  colName = response.cols[i];
  for (var j = 0; response.values.length; i++) {
    if (i === 0) data[j] = {};
    data[j][colName] =  response.values[i];
  }
}

You can then use grid.setData(data) to pass the data into the grid.

njr101
  • 9,499
  • 7
  • 39
  • 56
  • this worked with a slight change, the values have to be accessed loopwise like response.rows[].values.. – Diya4ever Mar 28 '12 at 10:42
  • Oh yeah, sorry, I missed the "rows" property. The json in the post doesn't close the rows item correctly so is not correctly formatted so I overlooked it. – njr101 Mar 28 '12 at 10:56
  • one more help needed, is there any way by which i can insert a image onto the first column wenever flag turns true ??? – Diya4ever Mar 29 '12 at 18:16
  • It's pretty simple. You just need to create a custom formatter and specify the formatter in the column definition. See here (http://mleibman.github.com/SlickGrid/examples/example2-formatters.html) or here (http://stackoverflow.com/questions/9840548/how-to-put-html-into-slickgrid-cell) for more info. – njr101 Mar 30 '12 at 06:51