Monday, August 21, 2017

create pagination with angular

==================== for pagination html  start======================
<div class="row site-pagination">
       <div class="col-xs-12">
                    <uib-pagination data-ng-if="totalItems > itemsPerPage" items-per-page="itemsPerPage"                      ng-change="pageChanged(currentPage)" total-items="totalItems" ng-                     model="currentPage" max-size="maxSize" class="pull-right" boundary-links="true" rotate="false" first-text="<i class='fa fa-chevron-left'></i><i class='fa fa-chevron-left'></i>" previous-text="<i class='fa fa-chevron-left'></i>" next-text="<i class='fa fa-chevron-right'></i>" last-text="<i class='fa fa-chevron-right'></i><i class='fa fa-chevron-right'></i>"></uib-pagination>
     </div>
</div>

===================for pagination html end========================
==================  Start function section ==========================
 /* this funtion will be setup on controller js
     Items per page is set default 10 in app.js if change needed active following line
 */
    $scope.itemsPerPage=2;
    $scope.actualPath = $location.path();
    $scope.pageChanged = function (currentPage) {
        if(currentPage > 1){        
            $location.path($scope.actualPath).search('page',currentPage);      
        }else{
            $location.path($scope.actualPath).search('');
        }
        $scope.currentPage = currentPage;
    };
   
    $scope.search = $location.search();
    if($scope.search.page !== undefined){
        $scope.currentPage = $scope.search.page;
        $rootScope.productFilterObj.pageNo = $scope.currentPage;
        _vm.getData($rootScope.productFilterObj);
    }else{
     if(Object.keys($rootScope.productFilterObj).length > 0){
      $rootScope.productFilterObj.pageNo = 1;
      _vm.getData($rootScope.productFilterObj);
     }else{
      _vm.getData();
     }
     
    }

====================  end function section =====================

====================  backend query start =====================
$data=request()->json('ProductFilter');
$pageNo=1;
if(!empty($data)){
$paginate_datas = json_decode($data);
if(isset($paginate_datas->pageNo)){
$pageNo = $paginate_datas->pageNo;
}
}

$questions = Question::where('blogorforum', 0)->where('close', 1)->orderBy('id', 'desc')->forPage($pageNo,$limit)->get();
====================  backend query end=====================