搜索期间的动态结果计数器

dynamic result counter during search

本文关键字:结果 计数器 动态 搜索      更新时间:2023-09-26

在这个代码片段中,您可以看到一个小概述,它在几个表页面上显示了一些示例数据。分页和其他一切都很好,但如果我在搜索中键入一些内容,结果的计数会改变,但顶部的计数器保持不变。如果我通过搜索限制结果,它也应该动态更改。例如,如果我键入"item_44",它应该显示1的1-1,因为只有1个条目。

如果有人知道如何解决这个问题,我会很高兴。

以下是工作示例。

var myApp = angular.module('myApp', []);
myApp.filter('startFrom', function() {
  return function(input, start) {
    start = +start; //parse to int
    return input.slice(start);
  }
});
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
  var vm = this;
  //vm.currentActive = sessionService.getState();
  $scope.currentPage = 0;
  $scope.pageSize = 10;
  $scope.data = [];
  $scope.firstItem = 0;
  $scope.lastItem = $scope.firstItem + $scope.pageSize;
  $scope.numberOfPages = function() {
    return Math.ceil($scope.data.length / $scope.pageSize);
  }
  for (var i = 0; i < 45; i++) {
    $scope.data.push("Item " + i);
  }
  $scope.nextPage = function() {
    if ($scope.currentPage >= $scope.data.length / $scope.pageSize - 1) {
    } else {
      $scope.currentPage = $scope.currentPage + 1;
      $scope.firstItem = $scope.firstItem + $scope.pageSize;
      if ($scope.firstItem + $scope.pageSize > $scope.data.length) {
        $scope.lastItem = $scope.data.length;
      } else {
        $scope.lastItem = $scope.firstItem + $scope.pageSize;
      }
    }
  }
  $scope.prevPage = function() {
    if ($scope.currentPage === 0) {
    } else {
      $scope.currentPage = $scope.currentPage - 1;
      $scope.firstItem = $scope.firstItem - $scope.pageSize;
      $scope.lastItem = $scope.firstItem + $scope.pageSize;
    }
  }
}
.name-row {
  width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div ng-controller="MyCtrl" class="wrapper">
  <div class="view-navigation">
    <span ng-click="prevPage()" id="hoverfinger"><i class="material-icons">skip_previous</i></span> 
    <span class="counter">{{firstItem +1}} - {{lastItem}} of {{data.length}}</span>	
    <span ng-click="nextPage()" id="hoverfinger"><i class="material-icons" >skip_next</i></span>
  </div>
  <br>
  <span ng-click="nextPage()" id="hoverfinger"><i class="material-icons" >search</i></span>
  <input type="text" ng-model="search" placeholder="search..." />
  <table border="1">
    <tr>
      <th class="name-row">Name</th>
      <th class="info-row">Info</th>
    </tr>
    <tr ng-repeat="item in data | filter:search | startFrom:currentPage*pageSize | limitTo:pageSize">
      <td>{{item}}</td>
      <td><a href="#">more info...</a>
      </td>
    </tr>
  </table>
</div>

不知怎的,这个片段在StackOverflow站点中不起作用。

试试这个,它有效。

HTML

    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div ng-controller="MyCtrl">
  <div class="view-navigation">
    <span ng-disabled="currentPage == 0" ng-click="back()" id="hoverfinger"><i class="material-icons">skip_previous</i></span>    
    <span class="counter">{{currentPage+1}} - {{numberOfPages()}} von {{getDataLength()}}</span>    
    <span ng-disabled="currentPage >= getData().length/pageSize - 1" ng-click="forward()" id="hoverfinger"><i class="material-icons" >skip_next</i></span>
  </div>
   <br>
   <span ng-click="nextPage()" id="hoverfinger"><i class="material-icons" >skip_next</i></span><input type="text" ng-model="q" placeholder="search..."/>
  <table border="1">
    <tr>
      <th>Status</th>
      <th>Name</th>
      <th>Info</th>
    </tr>
    <tr ng-repeat="item in s=( data | filter:q | startFrom:currentPage*pageSize | limitTo:pageSize)">
      <td><span><i class="material-icons" style="color: #8AC65B">check_circle</i></span></td>
      <td>{{item}}</td>
      <td><a href="#">more info...</a></td>          
    </tr>
  </table>
</div>

javascript

    var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', ['$scope', '$filter', function ($scope, $filter) {
    $scope.currentPage = 0;
    $scope.pageSize = 10;
    $scope.data = [];
    $scope.q = '';
    $scope.getData = function () {
      return $filter('filter')($scope.data, $scope.q)
    }
        $scope.getDataLength = function () {
      var arr = [];
      arr = $filter('filter')($scope.data, $scope.q)
      return arr.length;
    }
    $scope.numberOfPages=function(){
        return Math.ceil($scope.getData().length/$scope.pageSize);                
    }
     $scope.back = function(){
     if($scope.currentPage == 0){return}else{
    $scope.currentPage=$scope.currentPage-1;}
    }
    $scope.forward = function(){
    var val = $scope.numberOfPages();
    if(val == ($scope.currentPage+1)){
    alert('val');
    }
    else {
    $scope.currentPage+=1;}
    }
    for (var i=0; i<45; i++) {
        $scope.data.push("Item "+i);
    }
}]);

myApp.filter('startFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    }
});
I am sure Please Change below code according in your code working fine.

var myApp = angular.module('myApp',[]);
myApp.filter('startFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    }
});
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
    var vm = this;
	//vm.currentActive = sessionService.getState();
	
	  $scope.currentPage = 0;
    $scope.pageSize = 10;
    $scope.data = [];
    $scope.firstItem = 0;
    $scope.lastItem = $scope.firstItem + $scope.pageSize;
    $scope.numberOfPages=function(){
        return Math.ceil($scope.data.length/$scope.pageSize);                
    }
    for (var i=0; i<45; i++) {
        $scope.data.push("Item "+i);
    }
    
    $scope.nextPage = function(){
      
      if($scope.currentPage >= $scope.data.length/$scope.pageSize - 1){
        
      }
      else{
        $scope.currentPage = $scope.currentPage + 1;
        $scope.firstItem = $scope.firstItem + $scope.pageSize;   
        
           
        
        if($scope.firstItem + $scope.pageSize > $scope.data.length){
          $scope.lastItem = $scope.data.length;
        }
        else{
          $scope.lastItem = $scope.firstItem + $scope.pageSize;
        }
        
       
      }
      
    }
    
    $scope.prevPage = function(){
      
      if($scope.currentPage === 0){
       
      }
      else{
         $scope.currentPage = $scope.currentPage - 1;
         $scope.firstItem = $scope.firstItem - $scope.pageSize;
         $scope.lastItem = $scope.firstItem + $scope.pageSize;
      }
    }
 	
	
}
<html ng-app='myApp'>
  <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    </head><body ng-controller="MyCtrl" >
<div class="wrapper">
  
  <div class="view-navigation">
    <span ng-click="prevPage()" id="hoverfinger"><i class="material-icons">skip_previous</i></span>    
    <span class="counter">{{firstItem +1}} - {{griddata.length}} of {{data.length}}</span>	
    <span ng-click="nextPage()" id="hoverfinger"><i class="material-icons" >skip_next</i></span>
  </div>
   
   <br>
   <span><i class="material-icons" >search</i></span><input type="text" ng-model="search" placeholder="search..."/>
   
  <table border="1">
    <tr>
      <th class="name-row">Name</th>
      <th class="info-row">Info</th>
    </tr>
    <tr ng-repeat="item in griddata=(data | filter:search | startFrom:currentPage*pageSize | limitTo:pageSize)">
      <td>{{item}}</td>
      <td><a href="#">more info...</a></td>          
    </tr>
  </table>
</div>
  </body>
  </html>