简体   繁体   English

选择和ng选项过滤器

[英]select and ng-option filters

Can anyone fine tune my filters? 谁能微调我的过滤器? I have 3 ng-option directives that each filter from the one before. 我有3个ng-option指令,每个指令都从前一个过滤掉。 I got this working when the select elements were 'returning' an integer but I now need them bound to an object. 当select元素“返回”一个整数时,我开始工作了,但是现在我需要将它们绑定到一个对象上。

This is my setup: 这是我的设置:

formData.divisions = {'Open', 'Mixed'}
formData.teams =
    [{
        "id": 1,
        "division":"Open",
        "name":"Team 1"
    },{
        "id": 2,
        "division":"Open",
        "name":"Team 2"
    },{
        "id": 3,
        "division":"Mixed",
        "name":"Team 3"
    }]

<select
    ng-model="formData.division"
    ng-options="division for division in divisions"></select>

<select
    ng-model="formData.team"
    ng-options="team as team.name for team in teams track by team.id | filter:{division:formData.division}"></select>

<select
    ng-model="formData.opponent"
    ng-options="team as team.name for team in teams | filter:formData.division | filter:{team:'!'+formData.team}"></select>

It's the second and third select elements that aren't quite right. 这是第二和第三个选择元素不太正确。 They're close I'm sure. 我确定他们很接近。

The 1st select simply lists the divisions. 第一个选择仅列出分区。

The 2nd select wants to only list teams within the selected division. 第二名只想列出所选部门内的球队。

The 3rd select wants to do the same as the 2nd but also don't show the selected team from the 2nd select. 第三名希望与第二名相同,但也不要显示第二名所选择的球队。

Thanks! 谢谢!

So this works. 所以这可行。 Both the 2nd and 3rd select element are filtered by the 1st. 第2个和第3个选择元素都被第1个过滤。 The 3rd also filters out whatever was selected in the 2nd. 第3个也会过滤掉第2个中选择的内容。

<select
    ng-model="formData.division"
    ng-options="division for division in divisions"></select>

<select
    ng-model="formData.team"
    ng-options="team as team.name for team in teams | filter:formData.division | track by team.id"></select>

<select
    ng-model="formData.opponent"
    ng-options="team as team.name for team in teams | filter:formData.division | filter:{id:'!'+formData.team.id} track by team.id"></select>

As a side note and possibly helpful to anyone else trying to do something similar. 附带说明,这可能对其他尝试执行类似操作的人有所帮助。 I wanted to pre-populate the options with what the user had selected at a previous visit. 我想用用户上次访问时选择的选项预填充选项。 The object is cached. 该对象被缓存。 Something that helped me do this but also was a little troubling was 'track by'. 帮助我做到这一点但又有些麻烦的是“跟踪”。 This post helped https://coderwall.com/p/p-glog/angularjs-using-trackby-with-filters-in-an-ng-repeat 这篇文章对https://coderwall.com/p/p-glog/angularjs-using-trackby-with-filters-in-an-ng-repeat有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM