我当前的代码是使用 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>