【问题标题】:How to apply multiple filters with pagination in Angular JS?如何在Angular JS中应用多个带有分页的过滤器?
【发布时间】:2018-04-26 17:20:09
【问题描述】:

我正在尝试在Angular JS 中应用多个带有分页的过滤器请帮助我实现这一目标任何帮助将不胜感激。这是我的代码。 这是我的 index.html

 <html xmlns:ng="http://angularjs.org" ng-app lang="en">
        <head>
            <meta charset="utf-8">
            <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap.no-icons.min.css" rel="stylesheet">
            <link href="http://netdna.bootstrapcdn.com/font-awesome/2.0/css/font-awesome.css" rel="stylesheet">
            <script src="http://code.angularjs.org/1.1.0/angular.min.js"></script>
        </head>
        <body>

    </div>            
            <div ng-controller="ctrlRead">
                <div class="input-append">
                    <input type="text" ng-model="name" ng-change="nameSearch()" class="input-large search-query" placeholder="Search Name">
<input type="text" ng-model="country" ng-change="countrySearch()" class="input-large search-query" placeholder="Search Name">
                <span class="add-on"><i class="icon-search"></i></span>
                </div>

                            <div class="pagination pull-right">
                                <ul>
                                    <li ng-class="{disabled: currentPage == 0}">
                                        <a href ng-click="prevPage()">« Prev</a>
                                    </li>
                                    <li ng-repeat="n in range(pagedItems.length)"
                                        ng-class="{active: n == currentPage}"
                                    ng-click="setPage()">
                                        <a href ng-bind="n + 1">1</a>
                                    </li>
                                    <li ng-class="{disabled: currentPage == pagedItems.length - 1}">
                                        <a href ng-click="nextPage()">Next »</a>
                                    </li>
                                </ul>
                            </div>

            </div>
        </body>
    </html>

这是我的脚本。

function ctrlRead($scope, $filter) {
    // init
    $scope.sortingOrder = sortingOrder;
    $scope.reverse = false;
    $scope.filteredItems = [];
    $scope.groupedItems = [];
    $scope.itemsPerPage = 5;
    $scope.pagedItems = [];
    $scope.currentPage = 0;
    $scope.items = [
        {"id":"1","name":"John","country":"usa"}, 
        {"id":"2","name":"Peter",,"country":"London"}];



    // init the filtered items
    $scope.nameSearch = function () {
        $scope.filteredItems = $filter('filter')($scope.items, function (item) {
           if(item.name.includes($scope.name)){
           return true;
           }
        });
         $scope.countrySearch = function () {
        $scope.filteredItems = $filter('filter')($scope.items, function (item) {
           if(item.country.includes($scope.country)){
           return true;
           }
        });

        $scope.currentPage = 0;
        // now group by pages
        $scope.groupToPages();
    };

    // calculate page in place
    $scope.groupToPages = function () {
        $scope.pagedItems = [];

        for (var i = 0; i < $scope.filteredItems.length; i++) {
            if (i % $scope.itemsPerPage === 0) {
                $scope.pagedItems[Math.floor(i / $scope.itemsPerPage)] = [ $scope.filteredItems[i] ];
            } else {
                $scope.pagedItems[Math.floor(i / $scope.itemsPerPage)].push($scope.filteredItems[i]);
            }
        }
    };

    $scope.range = function (start, end) {
        var ret = [];
        if (!end) {
            end = start;
            start = 0;
        }
        for (var i = start; i < end; i++) {
            ret.push(i);
        }
        return ret;
    };

    $scope.prevPage = function () {
        if ($scope.currentPage > 0) {
            $scope.currentPage--;
        }
    };

    $scope.nextPage = function () {
        if ($scope.currentPage < $scope.pagedItems.length - 1) {
            $scope.currentPage++;
        }
    };

    $scope.setPage = function () {
        $scope.currentPage = this.n;
    };

    // functions have been describe process the data for display
    $scope.search();


};
ctrlRead.$inject = ['$scope', '$filter'];

【问题讨论】:

    标签: javascript angularjs pagination angular-filters


    【解决方案1】:

    希望这会对您有所帮助,我正在使用标准过滤器和其他 2 个下拉列表,我正在通过这些下拉列表在表格中进行过滤。 HTML 代码

    <div>
      <label>Associated Products:</label>
      <div class="row">
        <div class="col-md-6">
          <div class="col-md-4 custom-select">
            <div class="custom-select-text">{{selected}}</div>
            <select class="form-control select-area c-corporate-contact-us-from-select-country country-lang-select" data-widget="custom-select"
              (change)="onSelectProductLine($event)" [(ngModel)]="selected">
              <option value="0">Select a product line</option>
              <option *ngFor="let ProductLine of productLines" value={{ProductLine.categoryId}}>
                {{ProductLine.title}}
              </option>
            </select>
        </div>
      </div>  
      <div class="col-md-6">
        <div class="col-md-4 custom-select" *ngIf="edited">
          <div class="custom-select-text">{{selectedProduct}}</div>
          <select class="form-control select-area c-corporate-contact-us-from-select-country country-lang-select" data-widget="custom-select"
            (change)="onSelectProduct($event)" [(ngModel)]="selectedProduct">
            <option value="0">Select a product</option>
            <option *ngFor="let Product of products" value={{Product.categoryId}}>
              {{Product.title}}
            </option>
          </select>
        </div>
      </div>
      </div>
    </div>
    
     <div class="example-header">
        <mat-form-field>
          <input matInput (page)="changePage($event)" (keyup)="applyFilter($event.target.value)" placeholder="Search Products based on Product Numbers or Description or Associate Products">
        </mat-form-field>
      </div>
    

    JS 代码

      onSelectProductLine(args) {
    
    if (args.target.value != 0)
      this.edited = true;
    else
      this.edited = false;
    
    this.dataservice.getProduct(args.target.value).subscribe(res => this.products = res);
    this.selected = args.target.options[args.target.selectedIndex].text;
    this.applyFilterTopLevelProduct(this.selected);
     }
    
     onSelectProduct(args) {
        this.selectedProduct = args.target.options[args.target.selectedIndex].text;
        this.applyFilterSecondLevelProduct(this.selectedProduct);
      }
    
    
     applyFilter(filterValue: string) {
    debugger;
    filterValue = filterValue.trim(); // Remove whitespace
    filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
    
    this.dataSource.filterPredicate =
      (data: IFUsApi, filter: string) => {
        let searchStr = (data.productNumbers + data.title + data.associatedProducts).toLowerCase();
        return searchStr.indexOf(filter.toLowerCase()) != -1;
      }
    this.dataSource.filter = filterValue;
    
    
     }
    
    
    
    applyFilterTopLevelProduct(filterValue: string) {
        debugger;
        if (filterValue == "Select a product line")
          filterValue = "";
    
        filterValue = filterValue.trim(); // Remove whitespace
        filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
    
        this.dataSource.filterPredicate =
          (data: IFUsApi, filter: string) => {
            let searchStr = (data.toplevelProduct).toLowerCase();
            return searchStr.indexOf(filter.toLowerCase()) != -1;
          }
        this.dataSource.filter = filterValue;
      }
    
      applyFilterSecondLevelProduct(filterValue: string) {
    
        if (filterValue == "Select a product") {
          filterValue = this.selected;
    
          this.dataSource.filterPredicate =
            (data: IFUsApi, filter: string) => {
              let searchStr = (data.toplevelProduct).toLowerCase();
              return searchStr.indexOf(filter.toLowerCase()) != -1;
            }
          this.dataSource.filter = filterValue;
        }
        else {
          filterValue = filterValue.trim(); // Remove whitespace
          filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
    
          this.dataSource.filterPredicate =
            (data: IFUsApi, filter: string) => {
              let searchStr = (data.associatedProducts).toLowerCase();
              return searchStr.indexOf(filter.toLowerCase()) != -1;
            }
          this.dataSource.filter = filterValue;
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 2016-10-18
      • 1970-01-01
      • 2017-04-11
      • 2019-03-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多