我正在使用angular-datatables
v0.5.x,我想加载一个本地JSON对象。根据我的研究,我知道我必须使用promise。从我的console.log
我可以看到deferred.promise
填充了json数据。所以我认为它已正确返回DTOptionsBuilder.fromFnPromise()
。但是,数据表中没有显示任何数据。任何帮助将非常感谢!
HTML:
<table datatable="ng" dt-options="dtOptions" dt-columns="dtColumns" dt-instance="dtInstance" class="row-border stripe compact hover">
</table>
AngularJS控制器:
var getTableData = function(jsonObj) {
console.log('get table data function');
var deferred = $q.defer();
deferred.resolve(jsonObj);
console.log(deferred.promise);
return deferred.promise;
};
$scope.dtOptions = DTOptionsBuilder.fromFnPromise(getTableData(jsonObj))
.withPaginationType('full_numbers');
$scope.dtColumns = [
DTColumnBuilder.newColumn('col1').withTitle('Column 1'),
DTColumnBuilder.newColumn('col2').withTitle('Column 2'),
DTColumnBuilder.newColumn('col3').withTitle('Column 3'),
DTColumnBuilder.newColumn('col4').withTitle('Column 4')
];
$scope.dtInstance = {};
json对象:
[{
"col1": "Column 1",
"col2": "Column 2",
"col3": "Column 3",
"col4": "Column 4"
}]
答案 0 :(得分:-1)
您的表是否希望将列提供为对象或对象数组?
我看到你传递给表的JSON对象是一个对象数组!
尝试传递
{
"col1": "Column 1",
"col2": "Column 2",
"col3": "Column 3",
"col4": "Column 4"
}
或者改变
$scope.dtOptions = DTOptionsBuilder.fromFnPromise(getTableData(jsonObj))
.withPaginationType('full_numbers');
到
$scope.dtOptions = DTOptionsBuilder.fromFnPromise(getTableData(jsonObj[0]))
.withPaginationType('full_numbers');