Angualrjs:如何管理显示级联选择

时间:2016-01-29 20:09:05

标签: javascript angularjs

我有这个型号:

import math as m
import numpy as np
import itertools

def RetX(x):
    return x

def RetLogX(x):
    return m.log(x)

if __name__ == '__main__':
    funcs = [RetX, RetLogX]
    funcsIdx = np.arange(len(funcs))

    arrayNumbers = [1, 2, 3]
    for combination in itertools.product(funcsIdx, repeat=len(arrayNumbers)):
        result = []
        for idx in range(len(combination)):
            func = funcs[combination[idx]]
            result.append(func(arrayNumbers[idx]))
        print(result)

我想要的是在html var data = [ { id: 1, level:1, name:"X1", subChildren:[{ id:3, level:2, name:"X1-1", subChildren:[...] }] }, { id: 2, level:1, name:"X2", subChildren:[{ id:4, level:2, name:"X2-2", subChildren:[...] }] }....]; var levels=[{ level:1,.. },{ level:2,.. },...]; 元素中显示数据,并根据所选父级将它们级联。即:

<select>

这只是我想要展示的非功能性演示,当你选择其中一个父母时,它将级联下一个level 1: <select> <option>X1</option> <option>X2</option> </select> - level 2: (subChildren) <select> <option>X1-1</option> </select> - level 3 (sub subChildren) <select> <option>X1-1-1</option> </select>,它应该是下一个级别(并包含子子级),所以上..
问题是我不知道<select>的数量或levelsparents的数量,它们都是动态的,并且不知道如何在不写大量的情况下执行此操作本地代码。
整个想法是我想迭代subChildren数组并创建levels标记。

1 个答案:

答案 0 :(得分:0)

此解决方案仅在您知道将遇到的最大子孙数时才有效。请检查plunkr Plnkr

&#13;
&#13;
// Code goes here

var app = angular.module('Dropdown', []).controller('DpdController', function($scope) {
  $scope.data = [{
    id: 1,
    level: 1,
    name: "X1",
    subChildren: [{
      id: 3,
      level: 2,
      name: "X1-1",
      subChildren: [{
        id: 4,
        level: 3,
        name: "X1-2"
      }]
    }]
  }];
  $scope.logValue = function(value) {
    console.log(value);
  }
});
&#13;
<!DOCTYPE html>
<html ng-app="Dropdown">

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.2/angular.min.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-controller="DpdController">
  <select name="repeatSelect" id="repeatSelect" ng-model="selectedValue" ng-change="logValue(selectedValue)" ng-options="option.name for option in data">
  </select>
  <span ng-if='selectedValue'>
         <select name="repeatSelect1" id="repeatSelect1" ng-model="selectedValue1"
         ng-change="logValue(selectedValue1)"
         ng-options="option.name for option in selectedValue.subChildren">
      </select>
    </span>
</body>

</html>
&#13;
&#13;
&#13;