AngularJS使用ng-repeat在表格中显示刚刚过滤的项目

时间:2018-12-04 16:21:41

标签: javascript angularjs angular-filters

是否可以隐藏此表的结果并仅显示过滤后的结果?

<table class="table table-responsive" ng-class="{'table- 
striped':!spinnerVisible}">
                    <thead class="thead-inverse">
                        <tr>
                            <th><input type="checkbox" value="selected" ng- 
             model="checkboxes.checkAll" ng-click="checkAllRows()"></th>
                            <th>SID</th>
                            <th>Name</th>
                            <th>Pathway</th>
                            <th>Advisor</th>
                            <th>Credits</th>
                            <th>Campus</th>
                            <th ng-if="isPathwayLead">Action</th>
                        </tr>
                    </thead>
                    <tr ng-repeat="s in           
                   searchData.students|orderBy:'lastName' |filter:search | 
                   range:selectedPage:pageSize" student-row></tr>
                </table>

默认情况下,页面会加载这样的学生列表 enter image description here

我想隐藏此列表,仅显示上面过滤器的结果! 有可能吗?

学生行

<tr ng-class="{awesome:s.selected}">
<td><input name="checker" type="checkbox" value="selected" ng-model="s.selected"></td>
<td><a class="clickableAwesomeFont" ng-click="toStudent(s.sid)">{{s.sid}}</a></td>
<td>{{s.lastName.trim() + ', ' + s.firstName}}</td>
<td>{{s.pathwayName + (s.subPathwayName ? ', ' + s.subPathwayName : '')}}</td>
<td>{{s.advisorName}}</td>
<td>{{s.credits}}</td>
<td>{{s.branchCodeDescription}}</td>
<td ng-if="isPathwayLead"><span class="card-title fa fa-pencil-square-o pull-right clickableAwesomeFont" ng-click="popupReviewForm(s)"></span></td>

SearchFilter

<div>
    <div style="padding-top:100px;" id="mid_container" class="container">
        <div class="row">
            <div class="col-md-5 col-sm-6 col-xs-6">
                <label for="search-term">Search</label>
                <input id="search-term" type="text" ng- 
          keyup="$event.keyCode == 13 && commenceSearch()" ng- 
            model="searchModel.searchTerm"
                       class="form-control" placeholder="Name or SID" 
               aria-describedby="sizing-addon3">
                <label for="pathway_dd">Pathway</label>
                <select id="pathway_dd" class="form-control custom" ng- 
          change=onPathwayChange() ng-options="p.id as p.name for p in 
             pathways"
                        ng-model="searchModel.pathwayID">
                    <option value="">Select Pathway</option>
                </select>
                <label for="subPathway_dd">Area of Interest</label>
                <select id="subPathway_dd" class="form-control custom" ng- 
        options="sp.id as sp.name for sp in subPathways" ng- 
        model="searchModel.subPathwayID">
                    <option value="">Select Area of Interest</option>
                </select>
            </div>
            <div class="col-md-5 col-sm-6 col-xs-6">
                <label for="advisor_dd">Advisor</label>
                <select id="advisor_dd" class="form-control custom" ng- 
            model="searchModel.advisorSID">
                    <option value="">Select Advisor</option>
                    <option value="noadvisor">No Advisor</option>
                    <option ng-repeat="a in advisors| orderBy:'lastName'" 
            value="{{a.sid}}">{{a.lastName + ', ' + a.firstName}}</option>
                </select>
                <label for="credit-select">Credits</label>
                <select id="credit-select" class="form-control custom" ng- 
          model="searchModel.creditID" value="c.id" ng-options="c.id as 
          c.text for c in credits" />
                <label for="campus_dd">Campus</label>
                <select id="campus_dd" class="form-control custom" ng- 
                model="searchModel.branchCode">
                    <option value="">Select Campus</option>
                    <option ng-repeat="b in branches" value=" 
          {{b.branchCode}}">{{b.description}}</option>
                </select>
            </div>
            <div class="col-md-2 col-sm-12 col-xs-12">
                <div class="checkbox">
                    <label>
                        <input ng-model="searchModel.isEnrolled" 
                    type="checkbox">Enrolled
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input ng-model="searchModel.isMandatory" 
           type="checkbox">Mandatory
                    </label>
                </div>
                <button class="btn btn-primary btn-lg btn-block" ng- 
     click="commenceSearch()"><span class="glyphicon glyphicon-search" 
     title="Apply Search Filters" />&nbsp;Search</button><br />
                <button class="btn btn-default btn-sm" ng-

        click="clearSearch()"><span class="glyphicon glyphicon-refresh" 
        title="Reset Search Filters" />&nbsp;Reset</button>
                <!--<button class="btn btn-default btn-sm" ng- 
        click="toggleFilter()"><span class="glyphicon glyphicon-remove" 
          title="Close Search Filter Panel" />&nbsp;Close</button>-->
            </div>
        </div>
    </div>
</div>

基本上只是想知道我是否可以使用ng-Show来显示过滤的结果并隐藏其余部分而无需更改很多内容

1 个答案:

答案 0 :(得分:2)

我正在分享我通常需要在网格列表中进行搜索的方式。通常,我将一个输入作为search term,并在$filter中借助watcher搜索整个列表,如果它与特定对象的任何键匹配,则更新网格列表立即使用新的过滤列表。

我不建议复制粘贴代码,因为我使用了许多自定义指令和材料设计。但是您可以获得主要的想法。

<md-toolbar style="background-color:#F57C00 !important;" ng-show="showSearch">
            <div class="md-toolbar-tools">
            <md-input-container class="md-block" flex-gt-sm>
                <md-icon style="color:white">search</md-icon>
                <input type="text" ng-model="searchPhrase" style="color:white" name="searchPhrase">
            </md-input-container>
            <md-button ng-click="resetFilter()" class="md-icon-button">
                <md-tooltip style="background-color:lightgray;color:black" md-direction="top">close</md-tooltip>
                <md-icon>close</md-icon>
            </md-button>
            </div>
        </md-toolbar>

在我的控制器中,我将观察者写为:

$scope.$watch('searchPhrase', function (keyword) {
            if (keyword!= null && keyword!= '' && keyword!= undefined)
                $scope.gridList = ($filter('filter')($scope.actualGridList, keyword)); 

            // incase keyword was empty, change the list back to original
            else 
                $scope.gridList = angular.copy($scope.actualGridList);
        });

//incase reset button was pressed, change the list back to original
    $scope.resetFilter = function () {
                $scope.showSearch = false;
                $scope.searchPhrase = '';
                $scope.gridList = angular.copy($scope.actualGridList);
            }

请注意:

我将gridlist用作ng-repeat列表而不是actualGridList,这样,我将保留原始列表不变,而gridList将被反映到原始列表通过我的resetFilter()

如果要基于许多关键字过滤searchData.students,则可以基于这些关键字过滤列表,类似于:

$scope.copStudents = $filter('filter')($scope.searchData.students, { 'name': keyword }, true);