小编典典

如何将本地JSON对象与angular-datatables一起使用

angularjs

我想这样做:

testdata = [{"id":"58",...}]; // local object

$('#test').dataTable({
    "aaData": testdata,
    "aoColumns": [
        { "mDataProp": "id" }
    ] });

与angular-datatables模块一起使用。我已经试过了:

控制者

$scope.dtOptions = DTOptionsBuilder.fromSource('[{"id": 1}]')
        .withDataProp('data')
        .withBootstrap()
        .withPaginationType('full_numbers');
$scope.dtColumns = [
        DTColumnBuilder.newColumn('id').withTitle('ID')
];

视图

<table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="table table-striped table-bordered"></table>

但这根本不起作用,并且我收到此错误消息:

DataTables警告:表格ID =
DataTables_Table_0-Ajax错误。有关此错误的更多信息,请参见http://datatables.net/tn/7

另外,我无法使用任何Ajax选项从URL获取json数据,因为我的angularjs项目使用Express和Rest API,因此在尝试使用GET /
POST URL(例如“ / api / data / getJsonData”。

有任何想法吗 ?谢谢。


阅读 340

收藏
2020-07-04

共1个答案

小编典典

您不能使用.fromSource,因为它始终会执行ajaxUrl请求。但是您可以改用.fromFnPromise()。将您的JSON放入返回deferred.promise的函数中。

2020-07-04