簡體   English   中英

如何在控制器中使用javascript數組加載angular-datatables表

[英]How to load an angular-datatables table with a javascript array in the controller

我想用JavaScript數組加載angular-datatable,但是如果我嘗試這樣做,則會出現此錯誤

DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7

看起來它正在嘗試訪問某種URL

我想從我的angularjs控制器加載它,這是我的html代碼

<div class="table-responsive">
        <table datatable="" 
               dt-options="ctrl.dtOptions" 
               dt-columns="ctrl.dtColumns" 
               class="table table-striped table-bordered table-hover"
               width="100%">
        </table>
    </div>

這是我的angularjs控制器中的代碼:

var data = [{
                    "name": the name,
                    "lastname": "xx",
                    "age": 2
                }, {
                    "name": the name two,
                    "lastname": "yy",
                    "age": 3
                }];




            vm.dtOptions = DTOptionsBuilder.fromSource(JSON.stringify(data))
                    .withOption('createdRow', createdRow)
                    .withOption('stateSave', true)
                    .withPaginationType('full_numbers')
                    // Add Bootstrap compatibility
                    .withBootstrap()
                    // Active Responsive plugin
                    .withOption('responsive', true);
            vm.dtColumns = [
                DTColumnBuilder.newColumn(null).withTitle('Actions').notSortable()
                        .renderWith(actionsHtmlEstatus),
                DTColumnBuilder.newColumn('name').withTitle('Name'),
                DTColumnBuilder.newColumn('lastname').withTitle('lastname'),
                DTColumnBuilder.newColumn('age').withTitle('age')
            ];

當您具有靜態JSON /對象文字時,只需使用data選項:

vm.dtOptions = $scope.dtOptions = DTOptionsBuilder.newOptions()
   .withOption('data', data) // <---- like this
   .withOption('createdRow', createdRow)
   .withOption('stateSave', true)
   ...

使用OP中的樣本數組進行演示-> http://plnkr.co/edit/s2RybDQ8WV27jPOf4VQA?p=preview

您不能使用.fromSource因為它始終會執行ajaxUrl請求。 相反,您可以使用.fromFnPromise() 您需要將JSON放入返回deferred.promise的函數中。

檢查下面的筆的工作示例:

http://codepen.io/anon/pen/jrLpZX

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM