Slickgrids with dynamic json data

2019-09-09 09:03发布

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

1条回答
迷人小祖宗
2楼-- · 2019-09-09 09:58

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.

查看更多
登录 后发表回答