Angular ng-repeat filter with greater than

天大地大妈咪最大 提交于 2019-12-06 17:01:03

问题


I'm using angular to create a search property form, and I've got a select box with amount of bedrooms, how can I create a greater then filter? (for example: when 2 bedrooms selected, display all properties with 2 bedrooms or more)

The select box:

<select class="form-control" 
        name="bedrooms" 
        ng-model="Bedrooms" 
        ng-options="property.data.SubType as (property.data.Bedrooms + ' Bedrooms ('+ property.count +')') for property in properties  | unique:'Bedrooms' | orderBy:'data.Bedrooms'">
        <option value="">Minimum Bedrooms</option>
</select>  

And the filter:

<tr ng-repeat="property in filtered = (properties | filterMultiple:{Address: Address, SubArea:SubArea, SubType:SubType} | filter:{Bedrooms:Bedrooms})|  orderBy:Address">  

This is my JSFiddle


回答1:


http://jsfiddle.net/odpw6c6q/43/

In ng-options use ng-options="property.data.Bedrooms as instead of ng-options="property.data.SubType as because data.Bedrooms is the property that contains number of bedrooms which should be stored in ng-model

In scope add this function

//Filter Function
    $scope.greaterThan = function(bedrooms) {
        return function(item) {                          
            if ( item['Bedrooms'] > bedrooms) {                
                return true;
            } else {                
                return false;
            }
        }
    };

In ng-repeat use filter: greaterThan(Bedrooms) instead of filter:{Bedrooms:Bedrooms}



来源:https://stackoverflow.com/questions/29208242/angular-ng-repeat-filter-with-greater-than

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!