6

我正在使用Angular-datatable来显示数据,并且它与静态数据一起工作正常,但是当动态提供数据时它不起作用。它让我很难过。

我想要实现的是从ajaxin加载数据datatable

我用于初始化的以下代码及其工作正常Plunker

function MyChapterController($scope, $q, DTOptionsBuilder, DTColumnBuilder, chapter) {
    var vm = this;     

    vm.dtOptions = DTOptionsBuilder.newOptions()
        .withPaginationType('full_numbers')
        .withDisplayLength(2)
        .withDOM('pitrfl');
    vm.dtColumns = [
        DTColumnBuilder.newColumn('id').withTitle('ID'),
        DTColumnBuilder.newColumn('title').withTitle('First name'),
        DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
    ];

    vm.myChapters = [];
}

这是以下代码不起作用检查Plunker

function MyChapterController($scope, $q, $resource, DTOptionsBuilder, DTColumnBuilder, chapter) {
    var vm = this;     

    vm.dtOptions = DTOptionsBuilder.newOptions()
        .withPaginationType('full_numbers')
        .withDisplayLength(2)
        .withDOM('pitrfl');
    vm.dtColumns = [
        DTColumnBuilder.newColumn('id').withTitle('ID'),
        DTColumnBuilder.newColumn('title').withTitle('First name'),
        DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
    ];

    vm.myChapters = [{
          "id": 860,
          "firstName": "Superman",
          "lastName": "Yoda"
      }, {
          "id": 870,
          "firstName": "Foo",
          "lastName": "Whateveryournameis"
      }, {
          "id": 590,
          "firstName": "Toto",
          "lastName": "Titi"
      }, {
          "id": 803,
          "firstName": "Luke",
          "lastName": "Kyle"
      }];

} 

我也尝试过使用dataresource Plunker,但这里也很不幸。

它已经消耗了我很多时间。所以最后我决定听取大家的建议。任何帮助,将不胜感激。

4

1 回答 1

4

你错过了两点:

  1. 控制器没有被调用,因为它没有被添加到标记中。如果您稍后使用ui-router,那没关系,因为您可以在配置中添加控制器。
  2. 您的表格未填充,因为您错过了添加绑定,例如{{chapter.id}},并且由于您使用语法而myChapters无法使用。$scopecontrollerAs

请查看下面的演示或此更新的plunkr

在下面的演示中,我只将 更改为$http.get(...)$http.jsonp(...)mocky.io 获取 json数据

更新

要使过滤器成为分页工作,您必须将表格标记更改为此。

<table datatable="ng" dt-options="chapterCtrl.dtOptions" dt-column-defs="chapterCtrl.dtColumnDefs" id="cb-mychapter-table" class="row-border hover table table-bordered cb-data-table" cellspacing="0" width="100%">

// Code goes here
'use strict';

angular.module('myApp', ['datatables','ngResource']);

(function (angular) {
    'use strict';

    angular
            .module('myApp')
            .controller('myChapterController', MyChapterController)
            .factory('chapter', ChapterFactory);

    MyChapterController.$inject = ['$scope', '$q', '$resource', 'DTOptionsBuilder', 'DTColumnBuilder', 'chapter'];

    function MyChapterController($scope, $q, $resource, DTOptionsBuilder, DTColumnBuilder, chapter) {
        var vm = this;     
        
        vm.dtOptions = DTOptionsBuilder.newOptions()
            .withPaginationType('full_numbers')
            .withDisplayLength(2)
            .withDOM('pitrfl');
        vm.dtColumns = [
            DTColumnBuilder.newColumn('id').withTitle('ID'),
            DTColumnBuilder.newColumn('title').withTitle('First name'),
            DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
        ];
        
        /*vm.myChapters = [{
              "id": 860,
              "firstName": "Superman",
              "lastName": "Yoda"
          }, {
              "id": 870,
              "firstName": "Foo",
              "lastName": "Whateveryournameis"
          }, {
              "id": 590,
              "firstName": "Toto",
              "lastName": "Titi"
          }, {
              "id": 803,
              "firstName": "Luke",
              "lastName": "Kyle"
          }];*/
        
        chapter.getChapters().then(function(chapters) {
          vm.myChapters = chapters;
        });
    }
    
  ChapterFactory.$inject = ["$http"];
  
  function ChapterFactory($http) {
   return {
     getChapters: function() {
       return $http.jsonp('http://www.mocky.io/v2/55f2b4cb0938f5cd069cff10?callback=JSON_CALLBACK').then(function(response) {
         console.log(response);
         return response.data;
       });
     }
   };
  }
  
}(angular));
<!DOCTYPE html>
<html ng-app='myApp'>

  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <!--<link rel="stylesheet" href="style.css">-->
    
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
    <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
    <script src="https://cdn.rawgit.com/l-lin/angular-datatables/dev/dist/angular-datatables.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular-resource.min.js"></script>
    <!--<script src="script.js"></script>-->
  </head>

  <body ng-controller="myChapterController as chapterCtrl" >
    <table datatable="ng" dt-options="chapterCtrl.dtOptions" dt-column-defs="chapterCtrl.dtColumnDefs" id="cb-mychapter-table" class="row-border hover table table-bordered cb-data-table" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>ID</th>
                <th>FirstName</th>
                <th>LastName</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="chapter in chapterCtrl.myChapters">
                <td>{{chapter.id}}</td>
                <td>{{chapter.firstName}}</td>
                <td>{{chapter.lastName}}</td>
            </tr>
        </tbody>
    </table>
  </body>

</html>

于 2015-09-11T11:05:23.410 回答