1

我当前的代码是使用 JSON 提取数据。我可以在 ngTable 上看到我的数据正确加载。

如何添加一个搜索框,在单击按钮时启动搜索并在我的 ngTable 上重新加载数据?我曾尝试在 ngClick 中使用 tableParams.reload() 但这不会重新加载我的数据。

控制器

app.controller('ModelPagingCtrl', function ($scope, ngTableParams, $filter, 
                $http, DataService) {
$scope.doSearch = function () {
    DataService.getModels($scope.SearchText).then(function (data) {
        console.log("GetModels Worked");
        $scope.data = data.d;      
        $scope.tableParams = new ngTableParams({
            page: 1,
            count: 10,
            filter: {
                ModelNum: ''
            },
            sorting:
            {
                ModelNum: 'asc'
            }
            }, {
            total: $scope.data.length, 
            getData: function ($defer, params) {
                // use built-in angular filter
                var filteredData = params.filter() ?
                            $filter('filter')($scope.data, params.filter()) 
                            : $scope.data;
                var orderedData = params.sorting() ?
                            $filter('orderBy')(filteredData, params.orderBy()) 
                            : $scope.data;
                params.total(orderedData.length);
                $defer.resolve(orderedData.slice((params.page() - 1) 
                    * params.count(), params.page() * params.count()));
            } 
        }, function (error) {
            console.log('errror', error);
        });
    });

工厂/数据服务

app.factory('DataService', function ($http, $q) {
    return {
    getModels: function (Search) {
        return $http.post('AngularWithWebServices.aspx/FetchModels', 
                { "search":  Search }).then(function (response) {
                      if (typeof response.data === 'object') {
                           console.log('object found');  
                          return response.data;
                      } else {
                          // invalid response
                          console.log("Invalid Response");
                          return $q.reject(response.data);
                      }
                  }, function (response) {
                      // something went wrong
                      console.log("Error Response!");
                      return $q.reject(response.data);
                  });
    }
};
});

HTML

 Search: <input ng-model="SearchText"/>
          <button ng-click="doSearch()">Search</button>**<----What goes here?**           
        <table class="table" ng-table="tableParams" show-filter="true" >
          <tr ng-repeat="item in $data" >
            <td data-title="'ModelNum'" sortable="'ModelNum'" 
               filter="{ 'ModelNum': 'text' }">
               {{item.ModelNum}}               
            </td>
             <td>
              <strong>Year:</strong><p>{{item.Year}}</p>
              <strong>Price:</strong><p>{{item.Price}}</p>
              <p>{{item.Features}}</p>
             </td>
          </tr>
        </table>
4

1 回答 1

1

我没有测试这段代码,但它应该对你有用。

app.controller('ModelPagingCtrl', function ($scope, ngTableParams, $filter, $http, DataService) {

    $scope.doSearch = function() {
        $scope.tableParams.reload();
    }

    function search(params)
    {
        return DataService.getModels($scope.SearchText);   
    }

    $scope.tableParams = new ngTableParams({
        page: 1,
        count: 10,
        filter: {
            ModelNum: ''
        },
        sorting:
        {
            ModelNum: 'asc'
        }
        }, {
        getData: function ($defer, params) {
            //pass params.url() to your search function http://whatever.dev?count=10&page=1&sorting%5Bid%5D=desc
            search(params.url()).then(function(data)
            {
                //you have 2 ways from here

                // 1. if you use server side sorting, filtering
                params.total(data.length);
                $defer.resolve(data);

                // 2. if you use client side sorting/filtering    
                var filteredData = params.filter() ?
                            $filter('filter')(data, params.filter()) 
                            : data;
                var orderedData = params.sorting() ?
                            $filter('orderBy')(filteredData, params.orderBy()) 
                            : filteredData;
                params.total(orderedData.length);
                $defer.resolve(orderedData.slice((params.page() - 1) 
                    * params.count(), params.page() * params.count()));
            }
        } 
    }, function (error) {
        console.log('errror', error);
    });
});
于 2014-06-26T20:00:04.480 回答