我已将SlickGrid与我的Angular JS应用程序集成在一起。之前我用普通的硬编码javascript代码填充网格数据[见下文]。但是现在我需要从REST服务中获取网格数据,该服务在我的角度Js控制器中调用并保存在对象数组中。如何在SlickGrid javascript代码中访问角度js中定义的变量[array]。
我尝试用angular js代码中定义的数组替换下面的'data',但它不起作用。谁能在这里帮助我。
$(function () {
/* need to comment out this code once I can use the array defined in angular Js */
var data = [];
for (var i = 0; i < 500; i++) {
data[i] = {
title: "Task " + i,
duration: "5 days",
percentComplete: Math.round(Math.random() * 100),
start: "01/01/2009",
finish: "01/05/2009",
effortDriven: (i % 5 == 0)
};
}
grid = new Slick.Grid("#myGrid", data, columns, options);
})
更新:这是我的slickgrid代码。我正在调用REST服务来获取$ scope.data1中的数据,然后使用它来填充slickgrid,但我得到一个空的slickgrid。但是如果我在$ scope.data1中对数据进行硬编码,它就可以工作了。我错过了什么?谁能帮帮我吗。我花了一整天时间讨论这个问题。
$scope.populateGridData = function()
FetchPopulation.get({id:1} , function(response ) {
$scope.data1 = [];
for (var i = 0; i < response.PopulationList.population.length; i++)
$scope.data1[i] = {
firstName: response.PopulationList.population[i].firstName,
lastName: response.PopulationList.population[i].lastName,
designation: response.PopulationList.population[i].designation,
department: response.PopulationList.population[i].department,
salary: response.PopulationList.population[i].salary,
rating: response.PopulationList.population[i].rating,
joiningDate: response.PopulationList.population[i].joiningDate,
employeeId: response.PopulationList.population[i].employeeId,
employeeType: response.PopulationList.population[i].employeeType,
manager: response.PopulationList.population[i].manager,
permanent: (i % 5 == 0),
percentComplete: Math.round(Math.random() * 100)
};
/*
$scope.data1= [{employeeId:"12345", firstName: "aaa", lastName: "bbb" , designation:"Business Analyst" , department:"FSI" ,
salary:"120000",rating:"1" , joiningDate:"12/8/2013" , employeeType:"permanent" , manager:"aaaa" }];
*/
var grid = new Slick.Grid("#myGrid", $scope.data1, $scope.columns, $scope.options);
$scope.grid.setSelectionModel(new Slick.CellSelectionModel());
});
};
答案 0 :(得分:1)
你手动输入了注释线吗?因为它不是有效的JSON,所以必须转义[{employeeId:"12345"
应该是[{"employeeId":"12345",...
,即使不是这样,你的数据集似乎也是错误的。我没有使用没有DataView的SlickGrid,但是如果你采用基本的例子并复制这段代码(从SlickGrid example2拉出来:
$(function () {
for (var i = 0; i < 5; i++) {
var d = (data[i] = {});
d["title"] = "<a href='#' tabindex='0'>Task</a> " + i;
d["duration"] = "5 days";
d["percentComplete"] = Math.min(100, Math.round(Math.random() * 110));
d["start"] = "01/01/2009";
d["finish"] = "01/05/2009";
d["effortDriven"] = (i % 5 == 0);
}
grid = new Slick.Grid("#myGrid", data, columns, options);
})
它很可能会工作...在回到你的代码之前试试这个,但我强烈怀疑你的JSON结果可能没有效果,因为你认为它...现在在你尝试了基本样本之后回到你的代码,你可以尝试通过这里来验证你的JSON输出:JSONLint只需复制+粘贴你的JSON并点击验证。