Filtering by Multiple Specific Model Properties in AngularJS (in OR relationship)

前端 未结 13 2400
北恋
北恋 2020-11-22 17:08

Take a look at the example here: http://docs.angularjs.org/api/ng.filter:filter

You can search by any of the phone properties by using

相关标签:
13条回答
  • 2020-11-22 18:06

    Pretty straight forward approach using filterBy in angularJs

    For working plnkr follow this link

    http://plnkr.co/edit/US6xE4h0gD5CEYV0aMDf?p=preview

    This has specially used a single property to search against multiple column but not all.

    <!DOCTYPE html>
    <html ng-app="myApp">
    
      <head>
        <script data-require="angular.js@1.4.1" data-semver="1.4.1" src="https://code.angularjs.org/1.4.1/angular.js"></script>
        <script data-require="angular-filter@0.5.2" data-semver="0.5.2" src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.2/angular-filter.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script src="script.js"></script>
      </head>
    
      <body ng-controller="myCtrl as vm">
      <input type="text" placeholder="enter your search text" ng-model="vm.searchText" />
       <table>
         <tr ng-repeat="row in vm.tableData | filterBy: ['col1','col2']: vm.searchText">
           <td>{{row.col1}}</td>
           <td>{{row.col2}}</td>
           <td>{{row.col3}}</td>
         </tr>
       </table>
       <p>This will show filter from <b>two columns</b> only(col1 and col2) . Not from all. Whatever columns you add into filter array they
       will be searched. all columns will be used by default if you use <b>filter: vm.searchText</b></p>
      </body>
    </html>
    

    controller

    // Code goes here
    (function(){
    
      var myApp = angular.module('myApp',['angular.filter']);
    
      myApp.controller('myCtrl', function($scope){
        var vm= this;
        vm.x = 20;
    
        vm.tableData = [
          {
            col1: 'Ashok',
            col2: 'Tewatia',
            col3: '12'
          },{
            col1: 'Babita',
            col2: 'Malik',
            col3: '34'
          },{
            col1: 'Dinesh',
            col2: 'Tewatia',
            col3: '56'
          },{
            col1: 'Sabita',
            col2: 'Malik',
            col3: '78'
          }
          ];
      })
    
    })();
    
    0 讨论(0)
提交回复
热议问题