使用通过angularJS中的object属性创建的select来过滤表中的结果

时间:2017-12-13 04:46:39

标签: angularjs json object filter ng-options

我正在尝试创建一个过滤器,该过滤器将根据下拉列表中选择的值过滤掉表中的结果。 目前,它适用于除“类型”之外的所有下拉列表。

使用对象数组中的唯一属性创建了“类型”下拉列表。 所以,现在,它包含两个值'Internal'和'External'。

根据我选择的值,它应该只显示表中的过滤结果。 但截至目前,它只是显示空白。

我该如何解决这个问题?

以下是代码:

var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', '$filter', function($scope, $filter) {
 $scope.loc = {
  Sydney: 4,
  Toronto: 7,
  London: 3,
  Berlin: 7
 };
 
 $scope.country = ['Australia', 'India', 'Germany', 'China', 'Canada', 'United Kingdom'];
 $scope.items = [{
  name: 'ABC',
  type: 'INTERNAL',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  type: 'EXTERNAL',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'CDE',
  type: 'INTERNAL',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  type: 'EXTERNAL',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'DEF',
  type: 'INTERNAL',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'CDE',
  type: 'EXTERNAL',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  type: 'INTERNAL',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'ABC',
  type: 'EXTERNAL',
  countryName: 'United Kingdom',
  city: 'London'
 }, {
  name: 'ABC',
  type: 'INTERNAL',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'DEF',
  type: 'EXTERNAL',
  countryName: 'Canada',
  city: 'Toronto'
 }, {
  name: 'DEF',
  type: 'INTERNAL',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'BCD',
  type: 'EXTERNAL',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  type: 'INTERNAL',
  countryName: 'Canada',
  city: 'Toronto'
 }];
 
 $scope.changeFilter = function() {
	 $scope.my.countryName = $scope.my.countryName || undefined;
	 $scope.my.city = $scope.my.city || undefined;
	 $scope.my.type = $scope.my.type || undefined;
 };
}]);

app.filter('unique', function() {
    return function(input, key) {
        var unique = {};
        var uniqueList = [];
        for(var i = 0; i < input.length; i++){
            if(typeof unique[input[i][key]] == "undefined"){
                unique[input[i][key]] = "";
                uniqueList.push(input[i]);
            }
        }
        return uniqueList;
    };
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div class="container">
  <div ng-app="myApp" ng-controller="myCtrl">
	<div class="row">
	  <div class="col-md-3">
		<label>Search Keyword:</label>
		<input type="text" class="form-control" ng-model="my.$"/>
	  </div>
	  <div class="col-md-3">
		<label>Country:</label>
		<select class="form-control" ng-model="my.countryName" ng-options="ctry for ctry in country" ng-change="changeFilter()">
		  <option value="">Select a country</option>
		</select>
	  </div>
	  <div class="col-md-3">
		<label>City</label>
		<select class="form-control" ng-model="my.city" ng-options="key as key for (key, value) in loc" ng-change="changeFilter()">
		  <option value="">Select a city</option>
		</select>
	  </div>		  
	  <div class="col-md-3">
		<label>Type:</label>
		<select class="form-control" ng-model="my.type" ng-options="item.type for item in items | unique:'type'" ng-change="changeFilter()">
		  <option value="">Select a type</option>
		</select>
	  </div>
	</div>
	<hr />
	<div class="row">
	  <table class="table table-bordered">
		<tr>
		  <th>Name</th>
		  <th>Country</th>
		  <th>City</th>
		</tr>
		<tr ng-repeat="item in items | filter: my">
		  <td>{{item.name}}</td>
		  <td>{{item.countryName}}</td>
		  <td>{{item.city}}</td>
		</tr>
	  </table>
	</div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

实际上你的方法是正确的。只需将'item.type作为item.type'。

<select class="form-control" ng-model="my.type" ng-options="item.type 
as item.type for item in items | unique:'type'" ng-
change="changeFilter()">
<option value="">Select a type</option>
</select>

这很有效。 以前,当您在选择一个值时选择“my.type”,即“INTERNAL / EX ..”时,会选择一个整个项目。但我们只需要一个价值。

答案 1 :(得分:0)

&#13;
&#13;
 var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', '$filter', function($scope, $filter) {
 $scope.loc = {
  Sydney: 4,
  Toronto: 7,
  London: 3,
  Berlin: 7
 };

 $scope.locData = {
  INTERNAL: 1,
  EXTERNAL: 2
 };
 
 $scope.country = ['Australia', 'India', 'Germany', 'China', 'Canada', 'United Kingdom'];
 $scope.fullData = [{
  name: 'ABC',
  type: 'INTERNAL',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  type: 'EXTERNAL',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'CDE',
  type: 'INTERNAL',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  type: 'EXTERNAL',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'DEF',
  type: 'INTERNAL',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'CDE',
  type: 'EXTERNAL',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  type: 'INTERNAL',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'ABC',
  type: 'EXTERNAL',
  countryName: 'United Kingdom',
  city: 'London'
 }, {
  name: 'ABC',
  type: 'INTERNAL',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'DEF',
  type: 'EXTERNAL',
  countryName: 'Canada',
  city: 'Toronto'
 }, {
  name: 'DEF',
  type: 'INTERNAL',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'BCD',
  type: 'EXTERNAL',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  type: 'INTERNAL',
  countryName: 'Canada',
  city: 'Toronto'
 }];
 
 $scope.changeFilter = function() {
   $scope.my.countryName = $scope.my.countryName || undefined;
   $scope.my.city = $scope.my.city || undefined;
   $scope.my.type = $scope.my.type || undefined;
 };
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div ng-app="myApp" ng-controller="myCtrl">
    <div class="row">
      <div class="col-md-3">
        <label>Search Keyword:</label>
        <input type="text" class="form-control" ng-model="my.$"/>
      </div>
      <div class="col-md-3">
        <label>Country:</label>
        <select class="form-control" ng-model="my.countryName" ng-options="ctry for ctry in country" ng-change="changeFilter()">
          <option value="">Select a country</option>
        </select>
      </div>
      <div class="col-md-3">
        <label>City</label>
        <select class="form-control" ng-model="my.city" ng-options="key as key for (key, value) in loc" ng-change="changeFilter()">
          <option value="">Select a city</option>
        </select>
      </div>
      <div class="col-md-3">
        <label>Type:</label>
        <select class="form-control" ng-model="my.type" ng-options="key as key for (key, value) in locData" ng-change="changeFilter()">
          <option value="">Select a type</option>
        </select>
      </div>
    </div>
    <hr />
    <div class="row">
      <table class="table table-bordered">
        <tr>
          <th>Name</th>
          <th>Country</th>
          <th>City</th>
        </tr>
        <tr ng-repeat="item in fullData | filter: my">
          <td>{{item.name}}</td>
          <td>{{item.countryName}}</td>
          <td>{{item.city}}</td>
        </tr>
      </table>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;