我正在使用带有dirPagination插件的Angularjs来连接Web API。这似乎工作正常。我添加了一个搜索功能,进行服务器端搜索:
$scope.searchChanged = function () {
if ($scope.searchFor.length == 0) {
$scope.calculatedValue = 'e';
} else {
vm.getData(vm.pageno, vm.getSearch($scope.searchFor));
}
}
vm.getSearch = function (query) {
if (query == undefined) {
query = 'tech';
} else {
query = query;
}
return query;
}
有关完整代码,请参阅Plnkr 如果我开始搜索(例如销售),API返回结果并且分页正确,则get请求为:
/api/students/?category=sales&begin=1&pageSize=10
但是如果你想转到另一个页码,对服务器的get请求是:
/api/students/?category=tech&begin=2&pageSize=10
视图如何记住查询'sales',以便分页和结果正确?
答案 0 :(得分:1)
你在这里犯了一个常见的错误:如果你已经在使用范围变量,那么你不需要从视图中传入变量。
更改为这将更不容易出错
// change this to var getSearch or function getSearch if you don't need it on the view anymore
vm.getSearch = function () {
var query = vm.searchFor;
// you should only use vm, change ng-model to data.searchFor
if (query == undefined) {
query = 'tech';
}
return query;
}
vm.getData = function () {
vm.users = [];
$http.get("/api/students/?category=" + vm.getSearch() + "&begin=" + vm.pageno + "&pageSize=" + vm.itemsPerPage).success(function (response) {
vm.users = response.data;
vm.total_count = response.total_count;
});
};
答案 1 :(得分:0)
您的请求ID很好,您需要优化SQL查询,以便获得正确的结果。看起来应该是这样的:
@begin INT = 0,
@pageSize INT = 10
SELECT *
FROM [TableName]
ORDER BY id
OFFSET (@pageSize * @begin )
ROWS FETCH NEXT @pageSize ROWS ONLY;