如何在 AngularJS 中实现服务器端过滤和分页

How To Implement Server Side Filtering And Pagination In AngularJS

提问人:Riky Fahri Hasibuan 提问时间:4/18/2017 最后编辑:Riky Fahri Hasibuan 更新时间:4/18/2017 访问量:4272

问:

我正在使用 AngularJS 来显示来自数据库的数据,我已经在其上实现了客户端分页和过滤,但是当它加载大量数据时,我的应用程序崩溃了。

所以,我想通过将数据限制为 10 个项目/页面来将其更改为服务器端分页,它已经完成了,但是当我过滤数据时,它只会搜索页面上显示的项目。这是我到目前为止的代码

var app = angular.module('peradmin', ['ngRoute', 'ngAnimate']);
var baseUrl = 'http://localhost/peradmin/';
var currentUrl = window.location.pathname.split('/');

app.filter('startFrom', function() {
  return function(input, start) {
    if (input) {
        start = +start;
        return input.slice(start);
    }
    return [];
  }
});

app.controller('CustomerController', function($scope, $http, filterFilter) {
$scope.customer = [];
$scope.filtered = [];

$scope.pageSize = 10;
$scope.currentPage = 0;

$scope.getCustomerData = function(currentPage) {
    $http({
        method: 'POST',
        url: baseUrl + 'customer/getcustomer',
        data: { limit: $scope.pageSize, offset: currentPage },
        headers: { 'Content-Type': 'application/json' }
    }).then(function(response) {
        $scope.customer = response.data.customer;
        $scope.totalData = response.data.total;
        $scope.pageNumber = Math.ceil($scope.totalData / $scope.pageSize);
    })
}

$scope.filterData = function() {
    $scope.currentPage = 0;
    $scope.pageNumber = Math.ceil($scope.filtered.length / $scope.pageSize);
};

$scope.$watchCollection('customer', function() {
    if ($scope.results == undefined) return;
    $scope.currentPage = 0;
    $scope.pageNumber = Math.ceil($scope.totalData / $scope.pageSize);
})

// Next & Previous Button
$scope.paging = function(type) {
    if (type == 0 && $scope.currentPage > 0) {
        --$scope.currentPage;
    } else if (type == 1 && $scope.currentPage < $scope.pageNumber - 1) {
        ++$scope.currentPage;
    }
    $scope.getCustomerData($scope.pageSize * $scope.currentPage);
}

// Back to First Page
$scope.firstPage = function() {
    $scope.currentPage = 0;
    $scope.getCustomerData($scope.pageSize * $scope.currentPage);
}

// Go to Last Page
$scope.lastPage = function() {
    $scope.currentPage = $scope.pageNumber - 1;
    $scope.getCustomerData($scope.pageSize * $scope.currentPage + 1);
}

// call data when page is loaded
$scope.getCustomerData($scope.currentPage);

});

这是我的观点:

<div class="well" id="formsearch">
    <form id="search-form" class="form-inline float-lg-right" action="" method="POST">
        <input type="text" class="form-control" placeholder="Name" name="name" id="name" ng-model="filter.NAME" ng-change="filterData()">
        <input type="text" class="form-control" placeholder="Email" name="email" id="email"  ng-model="filter.EMAIL" ng-change="filterData()">
    </form>
</div>
<div class="box">
    <div class="box-body table-responsive" ng-controller="CustomerController">  
       <label>Page {{ currentPage + 1 }} from {{ pageNumber }} | Total: {{ totalData }} Data</label>
        <br><br>
        <table class="table table-hover table-bordered table-striped" width="100%">
            <thead>
                <tr>
                    <th style="text-align:center;width:20%;">Email</th>
                    <th style="text-align:center;width:25%;">Name</th>
                    <th style="text-align:center;width:10%;">Type</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="val in $parent.filtered = (customer | filter:{NAMA:filter.NAME,EMAIL:filter.EMAIL})">
                    <td>{{ val.EMAIL }}</td>
                    <td>{{ val.NAME }}</td>
                    <td style="text-align:center;">{{ val.CUST_TYPE == "B" ? "Business": "Personal"}}</td>
                </tr>
            </tbody>
        </table>

        <ul class="pagination" ng-if="totalData > 0">
            <li><a href="#" ng-click="firstPage()">First Page</a></li>
            <li><a href="#" ng-click="paging(0)">Previous</a></li>
            <li><a href="#" ng-click="paging(1)">Next</a></li>
            <li><a href="#" ng-click="lastPage()">Last Pager</a></li>
        </ul>

        <div class="alert alert-danger" ng-if="totalData == 0"><center>Data Is Not Found</center></div>
    </div>
</div>

如何使其过滤到数据库上的全部数据?

谢谢

JavaScript AngularJS 分页 过滤 服务器端

评论

0赞 jediz 4/18/2017
你应该用谷歌搜索如何用你的服务器或数据库的语言实现分页,而不是 Angular。后端不应该关心什么是UI技术。
0赞 Riky Fahri Hasibuan 4/18/2017
我正在使用 Codeigniter 运行 AngularJS,因为我是 AngularJS 的新手,所以它不完全是 Javascript。

答:

2赞 Leguest 4/18/2017 #1

我认为应该是:

$http({
    method: 'POST',
    url: baseUrl + 'customer/getcustomer',
    data: { 
            limit: $scope.pageSize, 
            offset: currentPage,
            filter: {
               name: $scope.filter.NAME,
               email: $scope.filter.EMAIL
            }
          },
    headers: { 'Content-Type': 'application/json' }
})

然后在服务器端,使用这 2 个筛选器属性,可以执行数据库端过滤

此外,还需要按钮在更改处理程序上应用过滤器或限制:

$scope.filterData = function() {
        $scope.currentPage = 0;
        $scope.pageNumber = Math.ceil($scope.filtered.length / $scope.pageSize);


        $scope.getCustomerData($scope.currentPage);

};

要执行油门,您可以尝试:ng-model-options='{ debounce: 1000 }'

   <input type="text" ng-model-options='{ debounce: 1000 }' class="form-control" placeholder="Name" name="name" id="name" ng-model="filter.NAME" ng-change="filterData()">
   <input type="text" ng-model-options='{ debounce: 1000 }' class="form-control" placeholder="Email" name="email" id="email"  ng-model="filter.EMAIL" ng-change="filterData()">

评论

0赞 Riky Fahri Hasibuan 4/18/2017
我尝试这个,并显示此错误ReferenceError: filter is not defined at b.$scope.getCustomerData (CustomerController.js:17)
0赞 Leguest 4/18/2017
对不起,是的,用$scope试试,比如$scope.filter.NAME
0赞 Riky Fahri Hasibuan 4/18/2017
仍然错误我已经更新了上面的 javascript 代码,也许你可以找出原因......TypeError: Cannot read property 'NAME' of undefined at b.$scope.getCustomerData
0赞 Leguest 4/18/2017
这是因为您需要声明 $scope.filter。试试这个: .把它放在你的声明之后。$scope.filter = {NAME: "", EMAIL: ""}$scope.currentPage = 0;
1赞 Riky Fahri Hasibuan 4/18/2017
对不起,回复晚了,我已经仔细尝试了您的代码,现在它正在工作......谢谢伙计,你是我的救世主:D