我目前的代码是使用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);
});
}
};
});
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>
答案 0 :(得分: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);
});
});