AngularJs 如何在 Angular UI Bootstrap 分页中显示过滤结果

标签 angularjs angularjs-ng-repeat angular-ui-bootstrap angularjs-filter angular-ui-pagination

由于 Angular UI Bootstrap分页,过滤结果只显示在 numPerPage 结果中。是否有任何选项可以将 $scope.expenses 的过滤结果显示为过滤结果?

演示:http://codepen.io/athimannil/pen/zGbBqR

angular.module('mytodos', ['ui.bootstrap']).controller('TodoController', function($scope) {
  /* paggination */
  if (localStorage.getItem("itemPerPage") === null) {
    localStorage.setItem("itemPerPage", 10);
  }
  function getPerPage(){			
    return parseInt(localStorage.itemPerPage);
  }
  $scope.changeNum = function (itemNum) {
    localStorage.itemPerPage = itemNum;
    $scope.numPerPage = getPerPage();
  };

  console.log(localStorage.itemPerPage);
  $scope.numsForPage = [5, 10, 25, 50, 100];
  $scope.currentPage = 1;
  $scope.numPerPage = getPerPage();
  $scope.maxSize = 5;
  
  $scope.filteredTodos = [];
  $scope.currentPage = 1;
  $scope.numPerPage = 10;
  $scope.maxSize = 5;
  $scope.expenses = [
    {
      "id": 1,
      "name": "Warren Phillips",
      "amount": "37.47",
      "date": "12/11/2015",
      "description": "blandit. Nam nulla magna, malesuada vel, convallis in, cursus et, eros. Proin ultrices. Duis volutpat nunc sit amet metus. Aliquam erat volutpat. Nulla facilisis. Suspendisse commodo tincidunt nibh. Phasellus nulla. Integer vulputate, risus a ultricies adipiscing, enim mi tempor lorem, eget mollis lectus pede et risus. Quisque libero lacus, varius et, euismod et, commodo at, libero. Morbi accumsan"
    }, {
      "id": 2,
      "name": "Declan Ayala",
      "amount": "29.18",
      "date": "12/17/2014",
      "description": "Integer eu lacus. Quisque imperdiet, erat nonummy ultricies ornare, elit elit fermentum risus, at fringilla purus mauris a nunc. In at pede. Cras"
    }, {
      "id": 3,
      "name": "Ivana Bell",
      "amount": "88.53",
      "date": "11/20/2014",
      "description": "Etiam imperdiet dictum magna. Ut tincidunt orci quis lectus. Nullam suscipit, est ac facilisis facilisis, magna tellus faucibus leo, in lobortis tellus justo sit amet nulla."
    }, {
      "id": 4,
      "name": "Brennan Noel",
      "amount": "17.76",
      "date": "02/06/2015",
      "description": "velit eu sem. Pellentesque ut ipsum ac mi eleifend egestas. Sed pharetra, felis"
    }, {
      "id": 5,
      "name": "Thomas Tran",
      "amount": "29.59",
      "date": "08/20/2014",
      "description": "nisl sem, consequat nec, mollis vitae, posuere at, velit. Cras lorem lorem, luctus ut, pellentesque eget, dictum placerat, augue. Sed molestie. Sed id risus quis diam luctus lobortis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Mauris ut quam vel sapien imperdiet ornare."
    }, {
      "id": 6,
      "name": "Tucker Bolton",
      "amount": "66.69",
      "date": "12/09/2014",
      "description": "et risus. Quisque libero lacus, varius et, euismod et, commodo at, libero. Morbi accumsan laoreet ipsum. Curabitur consequat, lectus sit amet luctus vulputate, nisi sem semper erat, in consectetuer ipsum nunc id enim. Curabitur massa. Vestibulum accumsan neque et nunc. Quisque ornare tortor at risus. Nunc ac sem ut dolor dapibus gravida. Aliquam tincidunt, nunc ac mattis ornare, lectus ante dictum mi, ac mattis velit justo nec ante. Maecenas mi felis, adipiscing fringilla, porttitor vulputate, posuere vulputate, lacus. Cras interdum."
    }, {
      "id": 7,
      "name": "Medge Mullen",
      "amount": "23.78",
      "date": "10/17/2015",
      "description": "dictum mi, ac mattis velit justo nec ante. Maecenas mi felis, adipiscing fringilla, porttitor vulputate, posuere vulputate, lacus. Cras interdum. Nunc sollicitudin commodo ipsum. Suspendisse non leo. Vivamus nibh dolor, nonummy ac, feugiat non, lobortis quis, pede. Suspendisse dui. Fusce diam nunc, ullamcorper eu, euismod ac, fermentum vel, mauris. Integer sem elit, pharetra ut, pharetra sed, hendrerit a, arcu. Sed et"
    }, {
      "id": 8,
      "name": "Morgan Merritt",
      "amount": "54.09",
      "date": "04/10/2016",
      "description": "Nulla facilisis. Suspendisse commodo tincidunt nibh. Phasellus nulla. Integer vulputate, risus a ultricies adipiscing, enim mi tempor lorem, eget mollis lectus pede et risus. Quisque libero lacus, varius et, euismod et,"
    }, {
      "id": 9,
      "name": "Janna Casey",
      "amount": "22.61",
      "date": "08/05/2015",
      "description": "eros turpis non enim. Mauris quis turpis vitae purus gravida sagittis. Duis gravida. Praesent eu nulla at sem molestie sodales. Mauris blandit enim consequat purus. Maecenas libero est, congue a, aliquet vel, vulputate eu, odio. Phasellus at augue id ante dictum"
    }, {
      "id": 10,
      "name": "Buffy Best",
      "amount": "59.98",
      "date": "05/06/2015",
      "description": "lacus vestibulum lorem, sit amet ultricies sem magna nec quam. Curabitur vel lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec dignissim magna a tortor. Nunc commodo auctor velit. Aliquam nisl. Nulla eu neque pellentesque massa lobortis ultrices. Vivamus rhoncus. Donec est. Nunc ullamcorper, velit in aliquet lobortis, nisi nibh lacinia orci, consectetuer euismod est arcu ac orci. Ut semper pretium neque. Morbi quis urna."
    }, {
      "id": 11,
      "name": "Brielle Newman",
      "amount": "52.26",
      "date": "04/16/2016",
      "description": "placerat, augue. Sed molestie. Sed id risus quis diam luctus lobortis."
    }, {
      "id": 12,
      "name": "Julie James",
      "amount": "6.38",
      "date": "08/15/2014",
      "description": "placerat, orci lacus vestibulum lorem, sit amet ultricies sem magna nec quam. Curabitur vel lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec dignissim magna a tortor. Nunc commodo auctor velit. Aliquam nisl. Nulla eu neque"
    }, {
      "id": 13,
      "name": "Shaine Barnett",
      "amount": "0.04",
      "date": "09/29/2014",
      "description": "laoreet, libero et tristique pellentesque, tellus sem mollis dui, in sodales elit erat vitae risus. Duis a mi fringilla mi lacinia mattis. Integer eu lacus. Quisque imperdiet, erat nonummy ultricies ornare, elit elit fermentum risus, at fringilla purus mauris a nunc. In at pede. Cras vulputate velit eu sem. Pellentesque ut ipsum ac mi eleifend egestas. Sed"
    }, {
      "id": 14,
      "name": "Genevieve Underwood",
      "amount": "50.71",
      "date": "09/24/2015",
      "description": "consequat auctor, nunc nulla vulputate dui, nec tempus mauris erat eget ipsum. Suspendisse sagittis. Nullam vitae diam. Proin dolor. Nulla semper"
    }, {
      "id": 15,
      "name": "Adam Villarreal",
      "amount": "18.48",
      "date": "04/06/2016",
      "description": "nisl. Maecenas malesuada fringilla est. Mauris eu turpis. Nulla aliquet. Proin velit. Sed malesuada augue ut lacus. Nulla tincidunt, neque vitae semper egestas, urna justo faucibus lectus, a sollicitudin orci sem eget massa. Suspendisse eleifend. Cras sed leo. Cras vehicula aliquet libero. Integer in magna. Phasellus dolor elit, pellentesque a, facilisis non, bibendum sed, est. Nunc laoreet lectus quis massa. Mauris"
    }
  ];
}).filter('pagination', function() {
	  return function(input, currentPage, pageSize) {
	    if(angular.isArray(input)) {
	      var start = (currentPage-1)*pageSize;
	      var end = currentPage*pageSize;
	      return input.slice(start, end);
	    }
	  };
	});
.box {
  border: 1px solid #DDD;
  padding: 10px;
  position: relative;
  background: #FFF;
  margin: 0 0 10px;
}
.tab-content > .tab-pane > .box {
  border-top: none;
}
.tab-content > .box {
  border-top: none;
}
select.pagiantion{
  width: auto;
  display: inline-block;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.min.js"></script>
<div ng-app="mytodos" ng-controller="TodoController">
  <div class="container">
    <hr />
    <tabset>
      <tab>
        <tab-heading>Statement</tab-heading>
        <div class="box">
          <h1>Todos</h1>
          <h4>{{expenses.length}} total</h4>
      <div class="row">
        <div class="col-md-6">
          <label for="">Show 
            <select class="form-control pagiantion" ng-model="numPerPage" ng-options="numPage for numPage in numsForPage" ng-change="changeNum(numPerPage);"></select>
            entries
          </label>
        </div>
        <div class="col-md-6 text-right">
          <div class="form-inline form-group">
            <label for="filter-list">Search </label>
            <input type="text" class="form-control" id="filter-list" placeholder="Search" ng-model="filterlist">
          </div>
        </div>
      </div>
      <table class="table table-hover table-responsive">
        <thead>
          <tr>
            <th>Name</th>
            <th>Amount</th>
            <th>Date</th>
            <th>Edit</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="expense in expenses | pagination: currentPage : numPerPage | filter:filterlist">
            <td>{{expense.name}}</td>
            <td>{{expense.amount}}</td>
            <td>{{expense.date}}</td>
            <td>
              <div class="btn-group btn-group-xs" role="group" ng-init="editmode=false">
                <button type="button" class="btn btn-default" ng-click="editExpense(expense);">
                  <i class="fa fa-pencil"></i>
                </button>
                <button type="button" class="btn btn-default" ng-click="deleteExpense(expense); editmode = !editmode">
                  <i class="fa fa-trash-o"></i>
                </button>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="clearfix" ng-show="expenses.length > numPerPage">
        <pagination 
          ng-model="currentPage" 
          total-items="expenses.length" 
          max-size="maxSize" 
          items-per-page="numPerPage"
          boundary-links="true" 
          class="pagination-sm pull-right" 
          previous-text="&lsaquo;" 
          next-text="&rsaquo;" 
          first-text="&laquo;" 
          last-text="&raquo;"
        ></pagination>
      </div>
          
          
        </div>
      </tab>
    </tabset>
  </div>
</div>


enter image description here

最佳答案

我想你想在一个变量中存储一个过滤列表,然后使用它的长度来进行分页。

HTML

表tr区。

<tr ng-repeat="expense in filteredlist = (expenses | filter:filterlist) |pagination: currentPage : numPerPage">

并且在分页

<pagination 
          ng-model="currentPage" 
          total-items="filteredlist.length" 
          max-size="maxSize" 
          items-per-page="numPerPage"
          boundary-links="true" 
          class="pagination-sm pull-right" 
          previous-text="&lsaquo;" 
          next-text="&rsaquo;" 
          first-text="&laquo;" 
          last-text="&raquo;"
        ></pagination>

然后在 $scope 中添加 filteredlist 变量。最初只是将整个费用复制到此变量。

JS

$scope.filteredlist =$scope.expenses;

你的固定code

关于AngularJs 如何在 Angular UI Bootstrap 分页中显示过滤结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31896944/

相关文章:

angularjs - 重置 ngModel 的有效性

javascript - Angularjs拦截器导致错误

javascript - 如果由 javascript 设置,则 ng 更改事件不会在复选框上触发

angularjs - ng-leave-active 在 div 的绝对定位列表上

fullcalendar - 如何在工具提示中显示 HTML 格式的内容? (用户界面日历)

angularjs - 如何在 Angular 工厂中使用 Firebase 查询?

javascript - Angular : How to update controller scope associated to directive scope's object as it changes?

javascript - Angular JS 过滤器 ng-repeat 带字段和下拉列表

javascript - 从结果中取消关闭模态

javascript - 在 Angularjs 应用程序中,如果不按住鼠标单击,则无法搜索子菜单项