过滤第一个字母在AngularJS中不起作用 [英] Filter first letter doesn't work in AngularJS

查看:48
本文介绍了过滤第一个字母在AngularJS中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上次我在这里发布了我的问题,如何过滤数据中的第一个字母,有人帮助我解决了这个问题.但是,当我尝试将其添加到项目中时,我不知道将startsWithA过滤器函数放在过滤数据的ng-repeat中的位置.知道如何解决吗?再次感谢.

Last time i have posted my problem here how filter the first letter in a data, and someone helped me on how to do it. But when I try to add it to my project I don't know where I put the startsWithA filter function to the ng-repeat that filtered the data. Any idea how fix it? Thanks again.

<script>
	var app = angular.module('app', ['ui.bootstrap']);
	
    $scope.student = [{
			        name: 'Andrew'        
			    }, {
			        name: 'Butler'
			    }, {
			        name: 'Cameron'
			    }, {
			        name: 'Delo'
			    },
                   {
			        name: 'Emman'
			    },
                   {
			        name: 'Ferbs'
			    }];
	
	app.filter('startFrom', function()
	{	
		 return function(input, start) 
		 {
		 	if(input) 
			{
				 start = +start; //parse to int
		 		 return input.slice(start);
		 	}
		 	return [];
		 }
	});

	app.controller('customersCrtl', function ($scope, $timeout)
    {  
		
				 $scope.list = student; 
				 $scope.currentPage = 1; //current page
				 $scope.entryLimit = 10; //max no of items to display in a page
				 $scope.filteredItems = $scope.list.length; //Initially for no filter
				 $scope.totalItems = $scope.list.length;

			 

			 
			 $scope.setPage = function(pageNo) 
			 {
				 $scope.currentPage = pageNo;
			 };
			 
			 $scope.filter = function() 
			 {
				 $timeout(function() 
				 {
					 $scope.filteredItems = $scope.filtered.length;
				 }, 10);

				
			 };
			 
			 
			 $scope.sort_by = function(predicate) 
			 {
				 $scope.predicate = predicate;
				 $scope.reverse = !$scope.reverse;
			 };	

			 
		
	});
	
	app.filter('startsWithA', function () {
	    return function (letter,items) {
	        var filtered = [];
	        var letterMatch = new RegExp(letter, 'i');
	        for (var i = 0; i < items.length; i++) {
	            var item = items[i];
	            if (letterMatch.test(item.name.substring(0, 1))) {
	                filtered.push(item);
	            }
	        }
	        return filtered;
	    };
	});
	
</script>

</style>
<script src="//code.angularjs.org/1.3.0-beta.7/angular.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.js"></script>
<style>

<div class="container" ng-controller="customersCrtl">
    <div class="row">
    <div class="col-12">
		<h2 id="titleHead"><center>Student List</center></h2>
		</div>
        <div class="option-panel">
         	<div class="col-sm-3 col-md-3 pull-right">
            	<form class="navbar-form">
               		 <div class="input-group">
                    	<input type="text" ng-model="search" ng-click="filter()" placeholder="Search student" class="form-control" placeholder="Search" name="search">
               		 </div>
           		 </form>
        	</div>     	   
        </div>
		 <div class="nav navbar-default"> 
    		<div class="tab-panel">
       			 <nav>
       			 <ul>
          			<li class="active" name = "active"><a ng-click="letter = [AB]">A-B</a>  </li>
                   	<li class="active" name = "active"><a ng-click="letter = [CD]">C-D</a>  </li>
                   	<li class="active" name = "active"><a ng-click="letter = [EF]">E-F</a>  </li>
                   
          		
        		</ul>
        		</nav>
   		 	</div>
   		 </div>  
        <div id="no-more-tables">
            <table class="col-md-12 table-bordered table-condensed cf" ng-show="filteredItems > 0">
        		<thead class="cf">
        			<tr>
        				
        				<th><center>Name&nbsp;<a ng-click="sort_by('first_name');"></a></center></th>
        			
        			</tr>
        		</thead>      
        		<tbody color="#">       
        			<tr ng-repeat="data in filtered = (list | filter:search |orderBy : predicate :reverse) | startFrom:(currentPage-1)*entryLimit |startWithA:letter limitTo:entryLimit ">   			
        				<td data-title="Name" class="text-center">{{data.name}}</td>
    
        							    			
        			</tr> 
        		</tbody>  		       		
        	</table>
        	 <div class="col-md-12" ng-show="filteredItems == 0">
		        <div class="col-md-12">
		           <center><h4>No results found</h4></center>
		        </div>
		    </div>
		    <div class="col-md-12" ng-show="filteredItems > 0">
		       <center><div pagination="" page="currentPage" on-select-page="setPage(page)" boundary-links="true" total-items="filteredItems" items-per-page="entryLimit" class="pagination-small" previous-text="&laquo;" next-text="&raquo;"></div></center>	 
		    </div>       	
      	  </div>
      	  
</div>
        </div>

推荐答案

修复后:

  1. customerCrtl(js)-> customerCtrl(html)
  2. startsWithA(js)-> startWithA(html)
  3. 缺少"|"在limitTo(html)之前

我发现问题是您没有在ng-click中转义字母:

I found the problem is that you aren't escaping the letters in the ng-clicks:

ng-click="letter = [AB]"

您需要:

ng-click="letter = '[AB]'"

$ scope.letters 始终为 [null] . [AB] 的计算结果为 [null] ,因为未定义 $ scope.AB .然后,当 $ scope.letters [null] 时,当尝试使用 startsWithA 过滤器时,您将永远不会返回任何已过滤的项目.

$scope.letters always is [null]. [AB] evaluates to [null] because $scope.AB is not defined. And then when you try to use the startsWithA filter when $scope.letters is [null] you never return any filtered items.

我的摘要在下面工作.

var app = angular.module('app', ['ui.bootstrap']);

var student = [{
  name: 'Andrew'
}, {
  name: 'Butler'
}, {
  name: 'Cameron'
}, {
  name: 'Delo'
}, {
  name: 'Emman'
}, {
  name: 'Ferbs'
}];

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

app.controller('customersCtrl', function($scope, $timeout) {

  $scope.list = student;
  $scope.currentPage = 1; //current page
  $scope.entryLimit = 10; //max no of items to display in a page
  $scope.filteredItems = $scope.list.length; //Initially for no filter
  $scope.totalItems = $scope.list.length;

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

  $scope.filter = function() {
    $timeout(function() {
      $scope.filteredItems = $scope.filtered.length;
    }, 10);
  };


  $scope.sort_by = function(predicate) {
    $scope.predicate = predicate;
    $scope.reverse = !$scope.reverse;
  };



});

app.filter('startsWithA', function() {
  return function(items, letter) {
    console.log(items, letter)
    var filtered = [];
    var letterMatch = new RegExp(letter, 'i');
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      if (letterMatch.test(item.name.substring(0, 1))) {
        filtered.push(item);
      }
    }
    console.log(filtered);
    return filtered;
  };
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.js"></script>
<div ng-app="app">
  <div class="container" ng-controller="customersCtrl">
    <div class="row">
      <div class="col-12">
        <h2 id="titleHead"><center>Student List</center></h2>
      </div>
      <div class="option-panel">
        <div class="col-sm-3 col-md-3 pull-right">
          <form class="navbar-form">
            <div class="input-group">
              <input type="text" ng-model="search" ng-click="filter()" placeholder="Search student" class="form-control" placeholder="Search" name="search">
            </div>
          </form>
        </div>
      </div>
      <div class="nav navbar-default">
        <div class="tab-panel">
          <nav>
            <ul>
              <li class="active" name="active"><a ng-click="letter = '[AB]'">A-B</a> 
              </li>
              <li class="active" name="active"><a ng-click="letter = '[CD]'">C-D</a> 
              </li>
              <li class="active" name="active"><a ng-click="letter = '[EF]'">E-F</a> 
              </li>


            </ul>
          </nav>
        </div>
      </div>
      <div id="no-more-tables">
        <table class="col-md-12 table-bordered table-condensed cf" ng-show="filteredItems > 0">
          <thead class="cf">
            <tr>

              <th>
                <center>Name&nbsp;
                  <a ng-click="sort_by('first_name');"></a>
                </center>
              </th>

            </tr>
          </thead>
          <tbody color="#">
            <tr ng-repeat="data in filtered = (list | filter:search |orderBy : predicate :reverse) | startFrom:(currentPage-1)*entryLimit |startsWithA:letter |limitTo:entryLimit ">
              <td data-title="Name" class="text-center">{{data.name}}</td>
            </tr>
          </tbody>
        </table>
        <div class="col-md-12" ng-show="filteredItems == 0">
          <div class="col-md-12">
            <center>
              <h4>No results found</h4>
            </center>
          </div>
        </div>
        <div class="col-md-12" ng-show="filteredItems > 0">
          <center>
            <div pagination="" page="currentPage" on-select-page="setPage(page)" boundary-links="true" total-items="filteredItems" items-per-page="entryLimit" class="pagination-small" previous-text="&laquo;" next-text="&raquo;"></div>
          </center>
        </div>
      </div>
    </div>
  </div>
</div>

这篇关于过滤第一个字母在AngularJS中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆