【问题标题】:Reset the filter if match not found in select box如果在选择框中找不到匹配项,则重置过滤器
【发布时间】:2018-05-18 03:00:14
【问题描述】:

我正在尝试创建一个过滤器,它将根据选择框中的选择在表格中显示结果。

此外,默认情况下,“城市”选择框将设置用户位置,我正在抓取并设置 ng-init="my.city='${city}'"。因此,将根据用户位置过滤默认结果。

我遇到的唯一问题是,当用户位置与我的“城市”选择框中的任何值都不匹配时,它应该被重置。即应该选择“选择一个城市”选项,并且所有结果都将显示而不应用任何过滤器。

在我的代码中,如果我选择任何城市,然后尝试选择“选择城市”,则过滤器不会被重置。

我该如何解决这个问题?

这是完整的代码:

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.fullData = [{
  name: 'ABC',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'CDE',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'DEF',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'CDE',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'ABC',
  countryName: 'United Kingdom',
  city: 'London'
 }, {
  name: 'ABC',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'DEF',
  countryName: 'Canada',
  city: 'Toronto'
 }, {
  name: 'DEF',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'BCD',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  countryName: 'Canada',
  city: 'Toronto'
 }];
}]);
<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="countryName as countryName for countryName in country">
          <option value="" selected>Select a country</option>
        </select>
      </div>
      <div class="col-md-3">
        <label>City</label>
        <select class="form-control" ng-model="my.city" ng-init="my.city='${city}'" ng-options="key as key for (key, value) in loc">
          <option value="" selected>Select a city</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>

【问题讨论】:

    标签: angularjs filter html-table reset angularjs-select


    【解决方案1】:

    在这种情况下,过滤器应用于空对象,该对象是一个名为 city 的属性,但应用在其上的过滤器对象的所有属性首先应该有一个空值。

    你必须在你的控制器中初始化你的对象

     $scope.my={$:'',countryName:'',city:''};
    

    并删除 ng-init 城市属性

    尝试关注

    var app = angular.module('myApp', []);
    app.controller('myCtrl', ['$scope', '$filter', function($scope, $filter) {
    $scope.my={$:'',countryName:'',city:''};
     $scope.loc = {
      Sydney: 4,
      Toronto: 7,
      London: 3,
      Berlin: 7
     };
     $scope.country = ['Australia', 'India', 'Germany', 'China', 'Canada', 'United Kingdom'];
     $scope.fullData = [{
      name: 'ABC',
      countryName: 'Germany',
      city: 'Berlin'
     }, {
      name: 'BCD',
      countryName: 'India',
      city: 'Bangalore'
     }, {
      name: 'CDE',
      countryName: 'Germany',
      city: 'Berlin'
     }, {
      name: 'ABC',
      countryName: 'Australia',
      city: 'Sydney'
     }, {
      name: 'DEF',
      countryName: 'China',
      city: 'Shanghai'
     }, {
      name: 'CDE',
      countryName: 'Germany',
      city: 'Berlin'
     }, {
      name: 'BCD',
      countryName: 'Australia',
      city: 'Sydney'
     }, {
      name: 'ABC',
      countryName: 'United Kingdom',
      city: 'London'
     }, {
      name: 'ABC',
      countryName: 'China',
      city: 'Shanghai'
     }, {
      name: 'DEF',
      countryName: 'Canada',
      city: 'Toronto'
     }, {
      name: 'DEF',
      countryName: 'India',
      city: 'Bangalore'
     }, {
      name: 'BCD',
      countryName: 'Germany',
      city: 'Berlin'
     }, {
      name: 'ABC',
      countryName: 'Canada',
      city: 'Toronto'
     }];
    }]);
    <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="countryName as countryName for countryName in country">
              <option value="" selected>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">
              <option value="" selected>Select a city</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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-27
      • 2020-12-07
      • 2018-02-07
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多