我编写了一个代码,用于将数据从RESTFUL服务访问到dataView,以填充到Slick Grid表中。
我收到一个错误,每个数据元素必须实现唯一ID属性!
我在将数据设置为数据视图时将唯一ID传递给数据视图,但没有任何事情发生。
var dataView;
var grid;
var data;
var options = {
forceFitColumns: true,
enableColumnReorder: false,
autoExpandColumns: true
};
var columns = [{
id: "userId",
name: "User Id",
field: "userId"
}, {
id: "id",
name: "Id",
field: "id"
}, {
id: "title",
name: "Title",
field: "title"
}, {
id: "completed",
name: "Completed",
field: "completed"
}];
$.ajax({
url: 'http://jsonplaceholder.typicode.com/todos/',
async: false,
success: function (response) {
data = JSON.stringify(response);
console.log("received data " + JSON.stringify(response));
},
error: function () {
console.log("error while getting data");
}
});
dataView = new Slick.Data.DataView();
dataView.beginUpdate();
dataView.setItems(data, "id");
dataView.endUpdate();
grid = new Slick.Grid('#slickGrid', dataView, columns, options);
// Make the grid respond to DataView change events.
dataView.onRowCountChanged.subscribe(function (e, args) {
alert("1");
grid.updateRowCount();
grid.render();
});
dataView.onRowsChanged.subscribe(function (e, args) {
alert("2");
grid.invalidateRows(args.rows);
grid.render();
});
JSFIDDLE HERE
答案 0 :(得分:0)
使用JSON.parse
然后将其传递给设置项目
data=JSON.parse(data);
dataView.beginUpdate();
dataView.setItems(data, "id");