简体   繁体   English

AngularJS-使用复选框和文本框过滤数据

[英]AngularJS - Filtering Data using checkbox and textbox

I'm new in angularJS, I just want to filter my data using checkbox and textbox but I have no Idea how to do it. 我是angularJS的新手,我只想使用复选框和文本框过滤数据,但我不知道该怎么做。 Please Help me. 请帮我。

Here is my data: 这是我的数据:

  $scope.array = [
    {name: 'Tobias', lname: 'TLname', company: 'x'},
    {name: 'Jeff', lname: 'JLname', company: 'x'},
    {name: 'Brian', lname: 'BLname', company: 'x'},
    {name: 'Igor', lname: 'ILname', company: 'y'},
    {name: 'James', lname: 'JLname', company: 'z'},
    {name: 'Brad', lname: 'BLname', company: 'y'}
  ];

All I want is if I going to choose x from checkbox it will show the rows related with Tobias Jeff Brian and if I'm going to uncheck the checkbox and type br , Brian and Brad will show but if I'm going to check the x checkbox only Bryan should show. 我想要的是,如果我要从复选框中选择x ,它将显示与Tobias Jeff Brian相关的行,如果我要取消选中该复选框并键入br ,则将显示BrianBrad,但是如果我要检查x仅Bryan应显示的复选框。 if I'm going to remove/erase the br and check both x and y all the related data should show. 如果我要删除/擦除br并检查xy,则所有相关数据都应显示。 (Tobias, Jeff, Brian, Igor and Brad). (Tobias,Jeff,Brian,Igor和Brad)。

Respectfully Yours. 肃然。

You can write custom filter function. 您可以编写自定义过滤器功能。 Check it out. 看看这个。

$scope.filterUsers = function (user) {

    var result = true,
        filters = $scope.filterBy,
        condition = $scope.filter;

    for (key in filters) {
        if (filters[key]) {
            if (user[key].indexOf(condition) !== -1) {
                return true;
            }
            else result = false;
        }
    }

    return result;
};

Usage: 用法:

<li ng-repeat="user in array | filter:filterUsers">{{user.name}} {{user.lname}},   {{user.company}}</li>

Demo: http://plnkr.co/edit/oqe5ADcE1t3OOoePE1oK?p=preview 演示: http//plnkr.co/edit/oqe5ADcE1t3OOoePE1oK?p = preview

Hi this could be done using a custom filter. 嗨,这可以使用自定义过滤器来完成。

Here's the fiddle: http://jsfiddle.net/w4XqV/27/ 这是小提琴: http : //jsfiddle.net/w4XqV/27/

Markup: 标记:

<div ng-app="app" ng-controller="mainCtrl">
    <div>Search by Name: <input type="text" ng-model="filters.search"></div>
       <div>Show Company X: <input type="checkbox" ng-model="companyFilters.x"/></div>
       <div>Show Company Y: <input type="checkbox" ng-model="companyFilters.y"/></div>
       <div>Show Company Z: <input type="checkbox" ng-model="companyFilters.z"/></div>
       <div ng-repeat="arr in array | filter:filters.search | customFilter:companyFilters">
           <span ng-bind="arr.name"></span>
       </div>
</div>

JavaScript: JavaScript:

var app = angular.module('app', []);
app.controller('mainCtrl', function($scope) {
    $scope.companyFilters = {
        x: false,
        y: false,
        z: false
    };

    $scope.filters = {
        search: ''
    };

    $scope.array = [
    {name: 'Tobias', lname: 'TLname', company: 'x'},
    {name: 'Jeff', lname: 'JLname', company: 'x'},
    {name: 'Brian', lname: 'BLname', company: 'x'},
    {name: 'Igor', lname: 'ILname', company: 'y'},
    {name: 'James', lname: 'JLname', company: 'z'},
    {name: 'Brad', lname: 'BLname', company: 'y'}
  ];
})
.filter('customFilter', function() {
  return function(input, companies) {

      console.log(input);
      consoloe.log(companies);
      var filteredList = [];

      for(var i = 0; i < input.length; i ++) {
          for(var c = 0; c < companies.length; c ++) {
              console.log(input[i].company);
              console.log(companies[i]);
              if(input[i].company === companies[c]) {
                  filteredList.push(input[i]);
                  break;
              }
          }
      }
      console.log(filteredList);
      return filteredList;
  };
});

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM