【问题标题】:how to disable pagination if there is only one page to display using AngularJs如果使用AngularJs只有一页要显示,如何禁用分页
【发布时间】:2015-11-03 18:48:18
【问题描述】:

我有一个用户表,我可以根据定义的某些状态对显示进行排序。

HTML

<div data-ng-controller="adminController">
   <div class="row">
      <div class="col-md-9">
        <h3>&nbsp;&nbsp; &nbsp;<span style="color:blue">List of Applied Users</span></h3>
        </div>
        <div class="col-md-2">
        <br>
        <select class="form-control" id="sel1" data-ng-model="status" required>
             <option value="">Status</option>
             <option value="Open">Open</option>
             <option value="In-Progress">In-Progress</option>
             <option value="Rejected">Rejected</option>
             <option value="Verified">Verified</option>
             <option value="Closed">Closed</option>
        </select>
        </div>
    </div>
<br>
<div class="padding">
<table class="table table-hover" border="2" >
    <thead>
        <tr>
            <th width="30" bgcolor="#FF8566">Si.No</th>
            <th width="90" bgcolor="#FF8566">Date</th>
            <th width="140" bgcolor="#FF8566">Name</th>  
            <th width="340" bgcolor="#FF8566">Service Name</th>             
            <th width="90" bgcolor="#FF8566">Status</th>
            th width="40" bgcolor="#FF8566"></th>

                </tr>
            </thead>
            <tbody>
       <tr data-ng-repeat="userDetails in (data | filter:status:status.status).slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage)) ">   
            <td></td>
            <td> {{ userDetails.subDate | date : 'MM-dd-yyyy' }} </td>
            <td> {{ userDetails.fullName }} </td>
            <td> {{ userDetails.serviceType.serviceName }} </td>
            <td> {{ userDetails.status.status }} </td>
            <td>
                <button type="button" class="btn btn-xs btn-success" data-ng-click="editUser(userDetails.id)" ng-if="userDetails.status.status !=='Closed'" >View</button></td>
         </tr>
      </tbody>       
    </table>

  </div>


<uib-pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()" class="pagination-sm" items-per-page="itemsPerPage" ng-show ="blocked"></uib-pagination>

</div>

JS

app.controller('adminController',[ '$scope', '$http', function($scope, $http) {
    $scope.$watch('status', function()
            {
                var array =[];
                alert($scope.status);
                if($scope.status!=undefined)
                {
                    totaldata.forEach(function(value, arr)
                        {

                            if(value.status.status==$scope.status)
                                {
                                    array.push(value);

                                }
                            console.log($scope.status);

                        })
                        $scope.data = array;        
                        $scope.totalItems = array.length;
                        console.log(array.length);

                }
                else{
                    $scope.data = totaldata;        
                    $scope.totalItems = totaldata.length;
                    console.log(totaldata.length);
                }
            })      

    $http.get('getAlldetails').success(function(response)
            {
        $scope.blocked = true;
                $scope.data = response;
                totaldata=response;
                $scope.totalItems = response.length;
                $scope.currentPage = 1;
                $scope.itemsPerPage = 9;
                $scope.maxSize = 5; 

                alert($scope.totalItems.length);

                $scope.setPage = function (pageNo) {
                    $scope.currentPage = pageNo;
                };


                }).error(function(error)
                    {
                alert(error);
            });

    $scope.editUser = function(id)
    {
        $location.url('/editUser/'+ id);
    }
}]);

如果应用过滤器后只有一页显示,如何隐藏分页?我尝试使用 ng-showng-if 但没有运气。

【问题讨论】:

  • 您要禁用它还是完全隐藏它?
  • @svarog 如果只有一页要显示,我想隐藏分页栏

标签: angularjs pagination


【解决方案1】:

像这样过滤ng-repeat上的数据

<tr ng-repeat="row in filteredData = (tableData | filter: filterQuery)">                        
      <td>{{row.name}}</td>
      <td>{{row.last_name}}</td>
      <td>{{row.age}}</td>
      <td>{{row.email}}</td>
 </tr>

uib-pagination 包裹在 div 中并设置 ng-show/ng-hide,仅当 filteredData 的数量大于每页的项目时才显示分页(项目多于每页的项目意味着你有超过 1 页)

<div ng-show="filteredData.length > itemsPerPage">
    <uib-pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()" class="pagination-sm" items-per-page="itemsPerPage" ng-show ="blocked"></uib-pagination>
</div>

因此,当您更改过滤条件时,摘要循环将运行重新评估您的 ng-show

【讨论】:

  • 不客气!是 ng-repeat 还是 ng-show 的问题?
猜你喜欢
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多