2017-07-25 61 views
0

所以我努力使引導表搜索,並提出從GET calll引導表中獲取JSON數據

<table id="example" class="table table-bordered table-striped table-hover" data-search="true"> 
    <thead> 
     <tr class="text-white clickable-row" bgcolor="#578ebe" > 
     <th id="white-text"> # </th> 
     <th id="white-text"> Name </th> 
     <th id="white-text"> DBA </th> 
     <th id="white-text"> Facility ID </th> 
     <th id="white-text"> Active </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr ng-repeat="hospital in hospital_filtered = hospitals"> 
     <td> <a ng-click="click(hospital)"> {{ hospital.hospital_id }} </td> 
     <td> <a ng-click="click(hospital)"> {{ hospital.hospital_name }} </td> 
     <td> <a ng-click="click(hospital)"> {{ hospital.hospital_dba }} </td> 
     <td> <a ng-click="click(hospital)"> {{ hospital.facility_id }} </td> 
     <td> <a ng-click="click(hospital)"> {{ hospital.active_flag }} </td> 
     </tr> 
    </tbody> 
    </table> 

這裏的數據來排序是我讓從我的API GET調用。數據即將進入,但引導程序表排序和排序不起作用。這個調用如何填充引導表?

$http.get("/hospitals/all", { 
      params: {authtoken: $rootScope.auth_token}} 
     ) 
     .then(function(response) { 
      // Request completed successfully 
      //console.log(response); 
      $scope.hospitals=response.data 

     }, function(x) { 
      // Request error 
      console.log("Error"); 
     }); 
     }); 

回答

0

你正確地獲取數據,但是迭代是錯誤的。

HTML:

<input ng-model="searchKeyword" type="text"> 
<table id="example" class="table table-bordered table-striped table-hover" data-search="true"> 
<thead> 
    <tr class="text-white clickable-row" bgcolor="#578ebe" > 
    <th id="white-text" ng-click="changeSort('hospital_id')"> # </th> 
    <th id="white-text" ng-click="changeSort('hospital_name')"> Name </th> 
    <th id="white-text" ng-click="changeSort('hospital_dba')"> DBA </th> 
    <th id="white-text" ng-click="changeSort('facility_id')"> Facility ID </th> 
    <th id="white-text" ng-click="changeSort('active_flag')"> Active </th> 
    </tr> 
</thead> 
<tbody> 
    <tr ng-repeat="hospital in hospitals | filter: searchKeyword | orderBy: orderKeyword"> 
    <td> <a ng-click="click(hospital)"> {{ hospital.hospital_id }} </td> 
    <td> <a ng-click="click(hospital)"> {{ hospital.hospital_name }} </td> 
    <td> <a ng-click="click(hospital)"> {{ hospital.hospital_dba }} </td> 
    <td> <a ng-click="click(hospital)"> {{ hospital.facility_id }} </td> 
    <td> <a ng-click="click(hospital)"> {{ hospital.active_flag }} </td> 
    </tr> 
</tbody> 
</table> 

JavaScript部分:

$scope.orderKeyword = 'hospital_name'; 
$scope.changeSort = function(sortAttr) { 
    $scope.orderKeyword = sortAttr; 
}; 

filter: searchKeyword將使用鍵入到額外的輸入字段的關鍵字。這樣你就可以基本上「搜索」表格。被留下的結果再由(orderBy)這是內部存儲$scope.orderKeyword屬性名稱進行排序。我用hospital_name初始化它。

你可以增加怎麼樣嚴格的濾波和哪個方向排序應該是附加的邏輯。

下面是有問題的兩個過濾器的文檔:

https://docs.angularjs.org/api/ng/filter/orderBy

https://docs.angularjs.org/api/ng/filter/filter