ng-repeat filter “show all” items if no filter sel

2020-08-11 10:58发布

I have an ng-repeat filter that uses a <select> menu to filter through a bunch of countries.

For example:

<div class="col-md-3 col-sm-6" ng-repeat="item in listings | filter: { location: locationFilter }">

How can I make it so that if nothing is selected, it automatically shows ALL the countries? And only starts filtering once a particular country is selected?

I'm sure this is probably a simple question but I am quite new to this and couldn't find how to do it.

Thanks!

标签: angularjs
5条回答
啃猪蹄的小仙女
2楼-- · 2020-08-11 11:14

If the filter expression returned undefined, then the filter would not apply. So, you could do something like the following:

<div ng-repeat="item in listings | filter:(!!locationFilter || undefined) && {location: locationFilter}">
   {{item}}
</div>

(!!locationFilter handles '', falsy and undefined values)

plunker

查看更多
Fickle 薄情
3楼-- · 2020-08-11 11:24

I would advise you to keep a simple declarative statement in your ng-repeat | filter

Then in your <select> tag, add a field for All like this :

<select ng-model="search.filter">
    <option value="{{undefined}}">All</option>
    <option ng-repeat="field in locations" value="field">field</option>
</select>
查看更多
放我归山
4楼-- · 2020-08-11 11:27

I have tried this and it worked

 <select ng-options="model.FeatureVersion as model.FeatureVersion for model in PostDataResponse" ng-model="featureVersion">
             <option value="">All</option>
            </select>
<table>
    <tr ng-repeat="model in PostDataResponse  | filter:{FeatureVersion: !!featureVersion?featureVersion: undefined  }">
        <td>{{model.ClientId}}</td>
    </tr>
</table>
查看更多
Bombasti
5楼-- · 2020-08-11 11:33

<div class="col-md-3 col-sm-6" ng-repeat="item in listings | filter: filtername ? filtername:''">

using this type of filter will first check the filtername is null or some value then it will take further actions. I Think this will be the easiest way.

查看更多
Emotional °昔
6楼-- · 2020-08-11 11:34

Here is the fiddle how i achieve this http://jsfiddle.net/L5wcgdhy/:-

HTML:-

<div ng-controller="MyCtrl">
 <select ng-model="filter.categoryId">
        <option value="!!"></option>
        <option ng-repeat="category in categories" value="{{category.id}}">{{category.id}}</option>
    </select>
    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Category</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="record in records | filter:filter">
                <td>{{record.title}}</td>
                <td>{{record.categoryId}}</td>
            </tr>
        </tbody>
    </table>
</div>

Ctrl:-

function MyCtrl($scope) {
    $scope.categories = [
        {id: 1},
        {id: 2},
        {id: 3}
    ];

    $scope.filter = {categoryId: "!!"};

    $scope.records = [
        {title: "I'm in category #1!", categoryId: 1},
        {title: "Category 1 is for suckas. #2 ya'll!", categoryId: 2},
        {title: "Three is best", categoryId: 3}
    ];
}
查看更多
登录 后发表回答