【问题标题】:Filter with radio buttons with different values (AngularJS)?过滤具有不同值的单选按钮(AngularJS)?
【发布时间】:2019-03-12 00:51:09
【问题描述】:

我有一个要使用单选按钮过滤的项目列表,这是我的数组的一个示例:

$scope.items = [
  {
    "name":"person 1",
    "section":"one",
    "job":false
  },
  {
    "name":"person 2",
    "section":"two",
    "job":true
  },
  {
    "name":"person 3",
    "section":"one",
    "job":false
  },
  {
    "name":"person 4",
    "section":"one",
    "job":true
  }
];

在我的 HTML 中,我有一个 ng-repeat 列表,我想用 单选按钮 对其进行过滤:

<div class="menu">
   <label>
      <input type="radio" ng-model="filteradio.section" value="">
      <p>All</p>
   </label>

   <label>
      <input type="radio" ng-model="filteradio.section" value="one">
      <p>Section 1</p>
   </label>

   <label>
      <input type="radio" ng-model="filteradio.section" value="two">
      <p>Section 2</p>
   </label>

   <label>
      <input type="radio" ng-model="filteradio.job" value="true">
      <p>People with job</p>
   </label>
</div>

<table>
   <tr>
      <td>Name</td>
      <td>Section</td>
      <td>Job</td>
   </tr>
   <tr ng-repeat="item in items | filter:filteradio:strict">
      <td>{{item.name}}</td>
      <td>{{item.section}}</td>
      <td ng-if="item.job">yes</td>
   </tr>
</table>

问题是 最后一个单选按钮 不起作用,因为当我选择带有 "filteradio.section" 的按钮时,它们可以正常工作,但是一旦我点击在 "filteradio.job" 中,其他单选按钮保持选中状态!
我尝试将相同的“name 属性”添加到所有单选按钮,但是一旦我单击 "filteradio.job",所有项目都会消失。
如何按“部分”以及“是否有工作”过滤它们?有没有更简单的方法来解决这个问题?

【问题讨论】:

    标签: angularjs filter radio-button


    【解决方案1】:

    我在下面提供了一个工作示例,但我将介绍一些值得注意的点:

    • 如果选项不是互斥的,您的所有无线电输入都应绑定到相同的 ng-model,否则您最终可能会处于选择多个无线电输入的状态
    • 要在表上实现您需要的过滤:
      • 跟踪存储项目属性和值的过滤器结构
      • 根据需要使用ng-change 更新此过滤器结构
      • 使用利用过滤器结构的自定义过滤器比较器
    • 请注意,某些无线电输入使用 ng-value 而不是 value,后者的值将始终被解释为字符串文字

    angular
      .module('app', [])
      .controller('ctrl', function ($scope) {
        var radioFilter = {
          prop: 'section',
          value: null,
        };
        $scope.items = [
          {
            name: 'person 1',
            section: 'one',
            job: false,
          },
          {
            name: 'person 2',
            section: 'two',
            job: true,
          },
          {
            name: 'person 3',
            section: 'one',
            job: false,
          },
          {
            name: 'person 4',
            section: 'one',
            job: true,
          }
        ];
        $scope.radio = null;
        $scope.radioChanged = function (prop) {
          radioFilter = {
            prop: prop,
            value: $scope.radio,
          };
        };
        $scope.filterByRadio = function (item) {
          return $scope.radio === null || item[radioFilter.prop] === $scope.radio;
        };
      });
    <div ng-app="app" ng-controller="ctrl">
      <div>
         <label>
            <input type="radio" ng-model="radio" ng-change="radioChanged('section')" ng-value="null"> All
         </label>
         <label>
            <input type="radio" ng-model="radio" ng-change="radioChanged('section')" value="one"> Section 1
         </label>
         <label>
            <input type="radio" ng-model="radio" ng-change="radioChanged('section')" value="two"> Section 2
         </label>
         <label>
            <input type="radio" ng-model="radio" ng-change="radioChanged('job')" ng-value="true"> People with job
         </label>
      </div>
      <table>
        <tr>
          <td>Name</td>
          <td>Section</td>
          <td>Job</td>
        </tr>
        <tr ng-repeat="item in items | filter:filterByRadio">
          <td>{{ item.name }}</td>
          <td>{{ item.section }}</td>
          <td>{{ item.job ? "yes" : "" }}</td>
        </tr>
      </table>
    </div>
    <script src="https://unpkg.com/angular@1.7.4/angular.min.js"></script>

    【讨论】:

    • 太棒了,太棒了!非常感谢,我还是 Angular 的新手,你刚刚救了我,这很好用!
    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2013-03-09
    相关资源
    最近更新 更多