Search code examples
jqueryjsonslickgrid

Slickgrids with dynamic json data


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"]
}

Solution

  • 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.