显示基于Angular中另一个选择的选项列表

iim*_*nov 7 javascript filter angularjs

我有两个选择下拉列表,其中第二个选项中的选项取决于在第一个选择中选择的选项.

目前我正试图找出应该从服务器返回数据的方式,这取决于我设置我的方式filter(s).

对于使用多个选择下拉列表过滤数据结构时的最佳实践,我将不胜感激.

以防万一我正在开发/测试当前稳定版本的AngularJS(v1.3.15).

数据结构1 - 嵌套:

$scope.optionObjs = [
    {
        id: 1, name: 'option 1', desc: '',
        elements: [
            { id: 9, name: 'option 11', desc: '', },
            { id: 10, name: 'option 12', desc: '', },
            { id: 11, name: 'option 13', desc: '', },
            { id: 12, name: 'option 14', desc: '', },
            { id: 13, name: 'option 15', desc: '', },
        ],
    },
];
Run Code Online (Sandbox Code Playgroud)

我希望在我的html中使用角度过滤器,如下面的例子,但似乎没有用.

<select data-ng-model="firstSelect" data-ng-options="option.name for option in optionObjs"></select>

<select data-ng-model="secondSelect" data-ng-options="option.elements.name for option in optionObjs | filter:firstSelect"></select>
Run Code Online (Sandbox Code Playgroud)

更新:

好的,这对我来说太傻了.所有上述数据结构都遵循我的要求是对第二个选择html的简单更改(不需要自定义过滤器功能).

<select data-ng-model="firstSelect" data-ng-options="option.name for option in optionObjs"></select>

<select data-ng-model="secondSelect" data-ng-options="option.name for option in firstSelect.elements"></select>
Run Code Online (Sandbox Code Playgroud)

就这些.D'哦!

数据结构2 - w /父参考:

$scope.optionObjs = [
    { id: 1, parent: null, name: 'option 1', desc: '', },
    { id: 9, parent: 1, name: 'option 11', desc: '', },
    { id: 10, parent: 1, name: 'option 12', desc: '', },
    { id: 11, parent: 1, name: 'option 13', desc: '', },
    { id: 12, parent: 1, name: 'option 14', desc: '', },
    { id: 13, parent: 1, name: 'option 15', desc: '', },
];
Run Code Online (Sandbox Code Playgroud)

继续这个例子,我将不得不为第一个选择编写一个过滤器,只显示那些没有父引用的选项.

<select data-ng-model="firstSelect" data-ng-options="option.name for option in optionObjs | filter:parent=null"></select>

<select data-ng-model="secondSelect" data-ng-options="option.elements.name for option in optionObjs | filter:firstSelect"></select>
Run Code Online (Sandbox Code Playgroud)

更新:

考虑到@adamjld的建议并进行了一些实验,Angular filter我提出了以下html更新,以配合上述数据结构(无需自定义过滤功能).

<select data-ng-model="firstSelect" data-ng-options="option.name for option in optionObjs | filter : { parent: null, }"></select>

<select data-ng-model="secondSelect" data-ng-options="option.elements.name for option in optionObjs | filter : { parent: firstSelect.id, }"></select>
Run Code Online (Sandbox Code Playgroud)

虽然这是一个更简单的解决方案,但在初始加载时会出现轻微问题.因为我没有初始化firstSelect为控制器中的作用域对象,所以第二个选择框允许用户选择他们想要的任何选项.但是,只要在第一个选择框中选择了一个选项,第二个选择框选项就会被过滤,并且只显示相应的(父)选项firstSelect.id.

参考

以防人们开始抱怨我根本没有使用搜索这里有一些参考: 角度过滤器
ng-repeat:按单个字段过滤

小智 4

我认为这将满足您的需求。我编辑了数据以显示它工作得更好一些。我将您的数据分成两个数组,父数组和子数组。

$scope.parentOptionObjs = [{
        id: 1,
        name: 'option 1',
        desc: ''
    }, {
        id: 2,
        name: 'option 2',
        desc: ''
    }];

$scope.childOptionObjs = [{
        parent: 1,
        id: 9,
        name: 'option 11',
        desc: ''
    }, {
        parent: 1,
        id: 10,
        name: 'option 12',
        desc: ''
    }, {
        parent: 1,
        id: 11,
        name: 'option 13',
        desc: ''
    }, {
        parent: 2,
        id: 12,
        name: 'option 14',
        desc: ''
    }, {
        parent: 2,
        id: 13,
        name: 'option 15',
        desc: ''
    }];
});
Run Code Online (Sandbox Code Playgroud)

现在,使用以下过滤器按父级的 id 过滤子级。

app.filter('secondDropdown', function () {
    return function (secondSelect, firstSelect) {
        var filtered = [];
        if (firstSelect === null) {
            return filtered;
        }
        angular.forEach(secondSelect, function (s2) {
            if (s2.parent == firstSelect) {
                filtered.push(s2);
            }
        });
        return filtered;
    };
});
Run Code Online (Sandbox Code Playgroud)

小提琴