2016-12-17 19 views
5

我开始用ngTable的现有例的修改有点用ngResource不工作。使用资源工厂它填充数据,但搜索和排序不起作用。

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

创建上方的笔。

var app = angular.module("myApp", ["ngTable", "ngResource", "ngTableDemoFakeBackend"]); 
 

 
app.factory('orderResource', function ($resource) { 
 
    return $resource('https://jsonplaceholder.typicode.com/posts'); 
 
}); 
 

 
(function() { 
 

 
    app.controller("demoController", demoController); 
 
    demoController.$inject = ["NgTableParams", "$resource", "orderResource"]; 
 
    function demoController(NgTableParams, $resource, orderResource) { 
 
     //debugger; 
 
     var ordersGet = orderResource.query({ }); 
 

 

 
     var Api = $resource("/data"); 
 
     this.tableParams = new NgTableParams({}, { 
 
      getData: function (params) { 
 

 
       // **** Section 1 *** sorting and filter does not work 
 
        return ordersGet.$promise.then(function (response) { 
 
        params.total(100); 
 
        return response; 
 
       // **** Section 1 *** 
 
       
 
       
 
       //****Section 2 *** this works fine 
 
        // return Api.get(params.url()).$promise.then(function(data) { 
 
        //   params.total(data.inlineCount); 
 
        //   return data.results; 
 
       //****Section 2 *** 
 
       
 
       
 
       }); 
 
      } 
 
     }); 
 
    } 
 
})();
<div ng-app="myApp"> 
 
    <div ng-controller="demoController as demo"> 
 

 

 

 
     <table ng-table="demo.tableParams" class="table table-bordered table-striped table-condensed"> 
 
      <tr ng-repeat="row in $data track by row.id"> 
 
       <td data-title="'iss'" filter="{id: 'number'}" sortable="'id'">{{row.id}}</td> 
 

 
      </tr> 
 
     </table> 
 
    </div> 
 
</div>

在代码中,如果你对此有何评论第1节和未注释部分2你可以观察它的工作。 我也试过简单的$ http它没有工作。

回答

0

您的线路

var ordersGet = orderResource.query({ }); 

是您ordersGet变量清理出的query命令。所以,当你做

回报ordersGet.$promise.then心不是一个真正的AJAX调用发生。没有什么是真的。 ngTable调用你的ordersGet,但由于query命令是什么都没有,它什么都不做。

此外,如果您的某个ordersGet工作正常,您永远不会通过params.url(),因此分页无法工作。

在我看来有这样

  1. 两种方式只需用

第2解决方案,并用它做

  • 覆盖资源上的get网址,并将其称为与此类似的网址ordersGet(params.url()).$promise.then();这是m比较复杂,但这里有几条链接沿着这条路线走下去。
  • Great SO question on how to override resource URL

    +0

    我想这一点,并没有奏效。 app.factory('orderResource',function($ resource){ return $ resource('https://jsonplaceholder.typicode.com/posts',{},{ get:{method:'GET'}, 更新:{方法:'POST'} }); }); –

    4

    嗯,我想我已经解决了它 - 也许不是最干净的一个,但仍然...

    var app = angular.module("myApp", ["ngTable", "ngResource", "ngTableDemoFakeBackend"]); 
    
    app.factory('orderResource', function ($resource) { 
    return $resource('https://jsonplaceholder.typicode.com/posts'); 
    }); 
    
    (function() { 
    
    app.controller("demoController", demoController); 
    demoController.$inject = ["NgTableParams", "$resource", "orderResource", '$filter']; 
    function demoController(NgTableParams, $resource, orderResource, $filter) { 
        //debugger; 
        var ordersGet = orderResource; 
    
    
        var Api = $resource("/data"); 
        this.tableParams = new NgTableParams({}, { 
         getData: function (params) { 
          // **** Section 1 *** sorting and filter does not work 
           return orderResource.query().$promise.then(function (response) { 
           params.total(100); 
    
           return $filter('orderBy')($filter('filter')(response, params.filter()), params.orderBy()) 
          // **** Section 1 ***  
    
          //****Section 2 *** this works fine 
           // return Api.get(params.url()).$promise.then(function(data) { 
           //   params.total(data.inlineCount); 
           //   return data.results; 
          //****Section 2 *** 
    
    
          }); 
         } 
        }); 
    } 
    })(); 
    

    我申请$过滤服务,我订购表通过该过滤器的结果(排序顺序我已经从组分具有)

    +0

    谢谢你的伴侣。我明白你的意思,但用这种方法搜索仍然无法正常工作。 –

    +0

    嘿,我已经解决了搜索 - 看看我的更新 – Melzar

    +0

    是的,这几乎与下面给出的答案相同。当过滤器被清除时搜索不会清除,因为它使得数字过滤器 {id:null} 因此有一个注入的代码来删除空值。 不是一个干净的方式,而是一种工作。 但如果你看到ng表的例子。他们只是在所有情况下工作,没有任何手动干扰。 –