AngularJS:分页不适用于ng-table

时间:2016-12-01 14:50:44

标签: javascript angularjs pagination coffeescript ngtable

所以,我正在编写一个Web应用程序,它可以获得大量的学生作业簿并将它们放在一张大桌子上。我为此使用了ng-table。这个列表很大,所以我试图在桌面上使用分页,否则页面只需要永远加载。但是现在分页不起作用,控件和页码显示在表格的底部,但所有结果都在第一页上。我在这里错过了什么? 相关代码(用coffeescript编写):

$scope.loadWorkbooks = ->
  $scope.workbooks = []
  $scope.filters = {}
  #...some filtering stuff...
  $http.get "/api/students?active=true"
    .then (response) ->
      total = 0
      _.each response.data, (value) ->
        total += value.Workbooks.length

      $scope.workbooksTable = new NgTableParams
        sorting:
          id: 'desc'
        page: 1
        count: 20
        ,
          total: total
          # counts:[]
          getData: ($defer, params) ->
            filteredData = if params.filter() then $filter('filter')($scope.workbooks, params.filter()) else $scope.workbooks
            orderedData = if params.sorting() then $filter('orderBy')(filteredData, params.orderBy()) else filteredData
            $defer.resolve orderedData

      $scope.loadAllWorkbooks()


$scope.loadAllWorkbooks = () ->
  $http.get "/api/v1/students/workbook"
  .then (response) ->
    workbooks = response.data

    if workbooks.length > 0
      $scope.workbooks = $scope.workbooks.concat workbooks
      Materialize.toast "Loading workbooks", 2000
      $scope.workbooksTable.reload()

表格:

<table show-filter="true" class="striped highlight bordered" ng-table="workbooksTable">
      <tr ng-repeat="workbook in $data" ng-show="workbook.state !== 'dead'">
        <td data-title="'ID'" sortable="'id'" filter="{id: 'text'}">
          {{ workbook.id }}
        </td>
        <td data-title="'Student'" sortable="'Student.name'">
          <p>{{ workbook.Student.firstName }} {{ workbook.Student.lastName }}</p>
        </td>
        <!--... more columns...-->
      </tr>
    </table>

1 个答案:

答案 0 :(得分:0)

想出来,我需要在我的get getData函数中添加一行:

getData: ($defer, params) ->
  filteredData = if params.filter() then $filter('filter')($scope.workbooks, params.filter()) else $scope.workbooks
  orderedData = if params.sorting() then $filter('orderBy')(filteredData, params.orderBy()) else filteredData
  #line I needed to add below
  slicedData = orderedData.slice (params.page() - 1) * params.count(), params.page() * params.count()
  #and then resolve slicedData instead of orderedData
  $defer.resolve slicedData