AngularJs 显示筛选列表中的第一个和最后一个

AngularJs show first and last from filtered list

在从数组中过滤空字符串后仅显示第一个和最后一个重复元素时遇到一些问题。

我的代码:

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

myApp.controller("myCtrl", function ($scope, $window) {
    $scope.items = [
        { name: "item1", color: "green", form: "" },
        { name: "item2", color: "", form: "circle" },
        { name: "item3", color: "red", form: "" },
        { name: "item4", color: "", form: "oval" },
        { name: "item5", color: "blue", form: "" },
        { name: "item6", color: "", form: "square" },
        { name: "item7", color: "yellow", form: "" },
        { name: "item8", color: "", form: "rectangle" }     
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl" class="ng-scope">
<div ng-repeat="item in items" ng-if="item.form == ''">
    {{item.name}}-{{item.color}}
</div>
<br />
<div ng-repeat="item in items" ng-if="item.color == ''">
    {{item.name}}-{{item.form}}
</div>
<br />
</body>

但我只需要在列表中显示第一个和最后一个元素。 前任。在第一个列表中:item1-green 和 item7-yellow(必须隐藏 item3-red 和 item5-blue),第二个列表 item2-circle 和 item8-rectangle。

如果您只想显示 ng-repeat 中项目的第一个和最后一个元素,您可以使用 ng-repeat 模板的 $first & $last 变量,它表示 $first & $last 如果它们为真则出现。

<div ng-repeat="item in items" ng-if="$first || $last">
    {{item.name}}-{{item.form}}
</div>

更新

对于这种情况,您应该更喜欢自定义过滤器,它将接受 arraypropertyNamevalueToCheck。最后它将 return 第一个和最后一个元素

HTML

<div ng-repeat="item in items| returnFirstAndLastValue: 'form': ''">
   {{item.name}}-{{item.color}}
</div>
<div ng-repeat="item in items| returnFirstAndLastValue: 'color': ''">
   {{item.name}}-{{item.form}}
</div>

过滤器

myApp.filter('returnFirstAndLastValue', function(){
  return function(array, prop, valueToCheck){
    var length = (array || []).length, tempOutput = [];
    angular.forEach(array, function(ele, index){
      if(ele[prop] === valueToCheck)
        tempOutput.push(ele);
    })
    return tempOutput.filter(function(element, index){
      if(index == 0 || index == (tempOutput.length - 1) )
        return element;
    })
  }
})

Demo Here

我为您的用例创建了自定义过滤器。 基本上,过滤器将通过过滤传递的参数(形式和颜色)的空值来 return 数组。

过滤后,可以使用$first$last配合ng-if只显示第一个和最后一个元素。

查看下面的代码。

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

  myApp.filter('customFilter', function() {
      return function(input, param) {
        if(input.length == 0) { return [];}
        var result = [];
        angular.forEach(input, function(item) {
          if(item[param] && item[param]!= '') {
            result.push(item);
          }
        });
        return result;
     }
  })

myApp.controller("myCtrl", function ($scope, $window) {
    $scope.items = [
        { name: "item1", color: "green", form: "" },
        { name: "item2", color: "", form: "circle" },
        { name: "item3", color: "red", form: "" },
        { name: "item4", color: "", form: "oval" },
        { name: "item5", color: "blue", form: "" },
        { name: "item6", color: "", form: "square" },
        { name: "item7", color: "yellow", form: "" },
        { name: "item8", color: "", form: "rectangle" }     
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl" class="ng-scope">
<div ng-repeat="item in items | customFilter:'color'" ng-if="$first || $last">
    {{item.name}}-{{item.color}}
</div>
<br />
<div ng-repeat="item in items | customFilter:'form'" ng-if="$first || $last">
    {{item.name}}-{{item.form}}
</div>
<br />
</body>

我做到了 ;)

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

myApp.controller("myCtrl", function ($scope, $window) {
    $scope.items = [
        { name: "item1", color: "green", shape: "" },
        { name: "item2", color: "", shape: "circle" },
        { name: "item3", color: "red", shape: "" },
        { name: "item4", color: "", shape: "oval" },
        { name: "item5", color: "blue", shape: "" },
        { name: "item6", color: "", shape: "square" },
        { name: "item7", color: "yellow", form: "" },
        { name: "item8", color: "", shape: "rectangle" }     
    ];
  
 $scope.shape = function(item){
  return !(item.color == '')
 };
 
 $scope.color = function(item){
  return !(item.shape == '')
 };  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl" class="ng-scope">
<div ng-repeat="item in items | filter:shape" ng-if="$first || $last">
    {{item.name}}-{{item.color}}
</div>
<br />
<div ng-repeat="item in items | filter:color" ng-if="$first || $last">
    {{item.name}}-{{item.shape}}
</div>
<br />
</body>