Angularjs:从列表中选择组合

时间:2016-01-19 14:07:10

标签: javascript angularjs

我有一个简单的清单:

$scope.myArr = ["a_b_c","a_b_d","a_e_g","f_t_r","f_t_g","f_u_m"];

从此我想构建3 <select></select>

第一个将包含由_分隔的数组的第一项。 所以["a","a","a","f","f","f"]

第二个["b","b","e","t","t","u"]

第三个["c","d","g","r","g","m"]

我已经构建了这3个数组,并且我创造了独特的值。

我需要的是,例如,如果我在第一个选择中选择a,则第二个只显示b,b,e

我实际上想要尊重start($scope.myArr)

数组中的可能组合

关于如何实现它的任何想法?

http://jsfiddle.net/uxo0jue3/

1 个答案:

答案 0 :(得分:6)

您可以循环数组,拆分值并创建一个水平对象。只需将其链接到视图:

var myArr = ["a_b_c","a_b_d","a_e_g","f_t_r","f_t_g","f_u_m"];
var levels = {};

myArr.forEach(function(arr) {
    var split = arr.split("_");
    if (!levels[split[0]])
        levels[split[0]] = {};

    if (!levels[split[0]][split[1]])
        levels[split[0]][split[1]] = []

    if (levels[split[0]][split[1]].indexOf(split[2]) === -1) 
        levels[split[0]][split[1]].push(split[2])
})

$scope.levels = levels;

观点:

<div ng-controller="myAppList">
    <select name="clienT" ng-model="clientW" ng-options="letter as letter for (letter, opts) in levels"></select>
    <select name="projecT" ng-model="clientX" ng-options="letter as letter for (letter, opts) in levels[clientW]"></select>
    <select name="platforM" ng-model="clientZ" ng-options="letter as letter for letter in levels[clientW][clientX]"></select>
</div>

演示:http://jsfiddle.net/uxo0jue3/4/