2016-08-16 54 views
4

我想用ngTable在html上顯示數據。ngTable不排序?

在html中,我使所有列都「可排序」。

<table ng-table="fm.tableParams" class="table" show-filter="false"> 
     <tr ng-repeat="report in $data"> 
      <td title="'ReportId'" sortable="'reportId'" class="text-center"> 
       {{report.reportId}}</td> 
      <td title="'SampleId'" sortable="'sampleId'" class="text-center"> 
       {{report.sampleId}}</td> 
      <td title="'MRN'" sortable="'mrn'" class="text-center"> 
       {{report.mrn}}</td> 
      <td title="'Diagnosis'" sortable="'diagnosis'" class="text-center"> 
       {{report.diagnosis}}</td> 
     </tr> 
    </table> 

從服務器檢索數據。 controller.js

ristoreApp.controller("fmCtrl", 
    ['$scope', 'fmFactory', 'NgTableParams', function($scope, fmFactory, NgTableParams) { 
     var self = this; 
     $scope.selection = '0'; 
     $scope.fmSearch = function() { 
      if ($scope.selection == '0') { 
       self.tableParams = new NgTableParams({ 
        page: 1,   // show first page 
        count: 10,   // count per page 
       }, { 
        getData: function (params) { 
         return fmFactory.getAll().then(function(response) { 
          var reports = response.data; 
          params.total(reports.length); 
          console.log(reports.length); 
          return reports; 
         }); 

        } 
       }); 
       self.tableParams.reload(); 
      } 
     } 
    }] 
) 

它確實顯示頁面中的記錄。但是排序不適用於任何列。我需要做些什麼才能使它工作?

編輯: 據ngtable.com,「你需要從NgTableParams.sorting()申請的值要在表格中顯示的數據,這通常情況下,當數據被取出來自服務器。「但它沒有說如何將這種方法應用於數據。看起來ngtable 1.0.0的記錄很差,缺乏示例。任何人都可以告訴我如何在客戶端排序?

回答

1

自動排序只採用了全dataset屬性使用時(因爲它可以在簡單的例子here看到)工作。

當您實施getData()方法時,輪到您(或在大多數情況下服務器的任務)進行排序。在params屬性中,您會發現通常需要傳遞給服務器的sorting,以便服務器可以進行排序。特別是在使用分頁視圖(並且因此一次不返回所有項目)時,服務器需要在選擇頁面的相關項目之前進行分類。

所以,如果你沒有很多的項目,不需要分頁,你可以在返回數組之前對數組進行排序。如果您不想預先加載所有項目,則需要將排序信息傳遞給服務器,並讓服務器執行排序和分頁。

+1

因此,每次點擊網頁上每列的小箭頭,我都會向服務器發送一個請求並獲取具有特定順序的json列表?我所做的從服務器獲取數據是調用rest api。如何通過http請求中的不同列指定不同的順序? – ddd

+1

這很大程度上取決於您定位的其他api。如果api不支持排序,解決此問題的唯一方法是加載所有項目並在客戶端排序項目。 –

+0

這正是我打算做的 - 在客戶端對它們進行分類。我該怎麼做? – ddd

0

將ngTable添加到您聲明應用程序模塊的位置。 喜歡這個問題: angular.module(「myApp」,[「ngTable」]);

然後使用NgTableParams.sorting() http://ng-table.com/#/sorting/demo-sorting-basic

+0

如何應用排序()數據?該網站似乎沒有這樣的例子。 – ddd

+1

@ddd,你使用orderBy。 https://docs.angularjs.org/api/ng/filter/orderBy https://scotch.io/tutorials/sort-and-filter-a-table-using-angular 的http:// www.w3schools.com/angular/ng_filter_orderby.asp – taiwoorogbangba

1

好吧,在我的情況下,我使用getData來調用一個只有30行數據返回一個頁面的rest api服務,所以我需要在服務的參數中指定當前頁面,頁面大小,字段排序和順序( 'ASC' 或 '降序'),如:

http://localhost:3000/api/my_service/?current_page=1&page_size=30&field_sort=date&order_sort=desc 

因此,要做到這一點我有我的getData:

getData: function(params){ 
    var sorter  = params.sorting() 
    vm.sortField = Object.keys(sorter)[0] 
    vm.sortOrder = sorter[Object.keys(sorter)[0]] 
    vm.currentPage = vm.sortField.length == 0 ? params.page() : 1 
    vm.pageSize = params.count() 
    return $http.get(get_service()).then(
    function(e){ 
     vm.posts = e.data.posts 
     params.total(vm.totalPosts) 
     return vm.posts 
    }, 
    function(e){ 
     console.error(e) 
    } 
) 
}