angularjs中的多个过滤器

时间:2014-06-14 00:37:33

标签: angularjs filtering

我花了过去9个小时试图解决这个问题,我已经阅读了多个问题和角度文档,但我还没有找到解决方案。

文本字段工作得很好,而选择字段只显示其中一个,并按字母顺序排列....但是 当我在选择字段中选择某些内容时,所有内容都会从表格中删除。我必须重新加载页面才能恢复商品...

如果选择“宜家”,如何才能显示与宜家公司相关的项目? 我想避免硬编码的过滤功能,因为我会在几个地方进行这种类型的多重过滤,而且我不想每次都创建一个单独的函数。

来自控制器的JSON:

[
{"id": "1", "position": "Lagerarbetare", "startdate": "06-07-2014", "city": "Kungenskurva", "company": "IKEA"},
{"id": "2", "position": "Webbdesigner", "startdate": "16-06-2014", "city": "Skarpnäck", "company": "Snowpeak"},
{"id": "3", "position": "Kassör", "startdate": "06-07-2014", "city": "Birsta", "company": "IKEA"},
{"id": "4", "position": "Säljare", "startdate": "06-07-2014", "city": "Gunnebo", "company": "Gunnebo"},
{"id": "5", "position": "Säljare", "startdate": "06-07-2014", "city": "Kungenskurva", "company": "Elgiganten"}
]

查看代码:

<input type="text" ng-model="search_position" placeholder="Sök" />
<div class="uk-button uk-form-select" data-uk-form-select>
    <span></span>
    <i class="uk-icon-caret-down"></i>
    <select ng-model="search_company" ng-options="job.company for job in jobs | unique: 'company' | orderBy:'company'">
        <option value="">Välj företag</option>
    </select>
</div>

<table class="uk-table uk-table-striped uk-table-hover">
    <thead>
        <tr>
            <th>Position</th>
            <th>Företag</th>
            <th>Start datum</th>
            <th>Ort</th>

        </tr>
    </thead>
    <tr ng-repeat="job in jobs | filter: { $: search_position} | filter:{ company: search_company }">
        <td class="uk-width-2-5">
            {{job.position}}
        </td>
        <td class="uk-width-1-5">
            {{job.company}}
        </td>
        <td class="uk-width-1-5">
            {{job.startdate}}
        </td>
        <td class="uk-width-1-5">
            {{job.city}}
        </td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:1)

对不起,我不能给你9个小时,但我希望我可以帮助你解渴,找到一个可行的解决方案。 :)

ng-options必须进行一些调整以达到您的需求。所选选项的值必须与过滤条件匹配。

ng-options="job.company as job.company for job in jobs | unique:'company' | orderBy:'company'"

请注意使用job.company as job.company for job in jobs

在选择默认选项后,还必须监视模型search_company以重置过滤器。

$scope.$watch('search_company', function(val){
    // Watch to reset the filter
    $scope.search_company = $scope.search_company || undefined;
});

请参阅此完整DEMO以见证相同情况。另请阅读ng-options docs中参数表中的最后一行,了解asfor在ng-options中的工作原理。