使用promise和json对象的angular-datatables不显示数据

时间:2017-06-22 12:55:01

标签: angularjs json angular-datatables

我正在使用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"
}]

1 个答案:

答案 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');