如何使用带有角度数据表的本地 JSON 对象 [英] How to use a local JSON object with angular-datatables
问题描述
我想这样做:
testdata = [{"id":"58",...}]; // local object
$('#test').dataTable({
"aaData": testdata,
"aoColumns": [
{ "mDataProp": "id" }
] });
使用 angular-datatables 模块.我试过这个:
with the angular-datatables module. I have tried this :
控制器
$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>
但它根本不起作用,我收到此错误消息:
But it doesn't work at all, and I get this error message :
DataTables 警告:table id=DataTables_Table_0 - Ajax 错误.有关此错误的详细信息,请参阅 http://datatables.net/tn/7
DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7
此外,我无法使用任何 Ajax 选项从 URL 获取 json 数据,因为我的 angularjs 项目使用 Express 和 Rest API,因此在尝试使用 GET/POST 获取数据时出现 401 not authorized 错误URL 像/api/data/getJsonData".
Also, I can't use any Ajax options to get json data from an URL, because my angularjs project uses Express and the Rest API, so I get 401 not authorized error while trying to get my data with a GET/POST URL like "/api/data/getJsonData".
有什么想法吗?谢谢.
推荐答案
您不能使用 .fromSource,因为它总是会执行 ajaxUrl 请求.但是您可以改用 .fromFnPromise() .将您的 JSON 放入一个返回 deferred.promise 的函数中.
You can't use .fromSource as it always will do an ajaxUrl request. But you can use .fromFnPromise() instead. Put your JSON into a function which returns an deferred.promise.
这篇关于如何使用带有角度数据表的本地 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!