广告位联系
返回顶部
分享到

AngularJS实现多级下拉框的代码

JavaScript 来源:互联网 作者:秩名 发布时间:2022-03-25 23:34:46 人浏览
摘要

具体内容如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 div ng-app=MultiDropDownApp ng-controller=MultiDropDownControl as vm label选择地址:/label !--ng-options加载所有选择项,ng-model记录当前选择项-- select ng-model=vm.pro

具体内容如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<div ng-app="MultiDropDownApp" ng-controller="MultiDropDownControl as vm">

    <label>选择地址:</label>

    <!--ng-options加载所有选择项,ng-model记录当前选择项-->

    <select ng-model="vm.province" ng-options="x.name for x in vm.provinceSort"

            ng-change="vm.selectProvince()" id="" value="" class="form-control width-25">

        <option value="">请选择</option>

 

    </select>

    <label>—</label>

    <select ng-model="vm.city" ng-options="x.name for x in vm.citySort"

            id="" value="" class="form-control width-25">

        <option value="">请选择</option>

 

    </select>

</div>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

<script src="~/Scripts/angular.min.js"></script>

<script>

    var app = angular.module('MultiDropDownApp', []);

    //可以添加上自己注入的服务

    app.controller('MultiDropDownControl', ['$scope', '$http',

        function ($scope, $http) {

            var vm = this;

            vm.provinceSort = [];

            vm.citySort = [];

 

            //选择省级单位,初始化市级数据   二级联动

            vm.selectProvince = function () {

                var fatherID = vm.province.id;

                vm.citySort = [];

                $http({

                    method: 'POST',

                    url: '/AngularjsStudy/GetChildrenSort',

                    data: { fatherID: fatherID }

                }).then(function successCallback(data) {

                    vm.citySort = data.data;

                }, function errorCallback(response) {

                    // 请求失败执行代码

                });

            }

 

            //初始化页面

            function init() {

                //省

                $http({

                    method: 'POST',

                    url: '/AngularjsStudy/GetProvinceSort',

                    data: {}

                }).then(function successCallback(data) {

                    //加载下拉框数据

                    vm.provinceSort = data.data;

                    //设置默认选项

                    vm.province = vm.provinceSort[0];

                }, function errorCallback(response) {

                    // 请求失败执行代码

                });

            }

 

            //初始化

            init();

        }])

</script>

Controller

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

public ActionResult GetProvinceSort()

        {

            List<District> districts = new List<District>();

            districts.Add(new District() {id=1,fatherID=0,name="湖南省" });

            districts.Add(new District() { id =2, fatherID = 0, name = "湖北省" });

            districts.Add(new District() { id =3, fatherID = 0, name = "四川省" });

            return Json(districts);

        }

 

        public ActionResult GetChildrenSort(int fatherID)

        {

            List<District> districts = new List<District>();

            switch (fatherID)

            {

                case 1:

                    districts.Add(new District() { id = 4, fatherID = 1, name = "长沙市" });

                    districts.Add(new District() { id = 5, fatherID = 1, name = "岳阳市" });

                    districts.Add(new District() { id = 6, fatherID = 1, name = "株洲市" });

                    return Json(districts);

                case 2:

                    districts.Add(new District() { id = 7, fatherID = 2, name = "武汉市" });

                    districts.Add(new District() { id = 8, fatherID = 2, name = "宜昌市" });

                    return Json(districts);

                case 3:

                    districts.Add(new District() { id = 9, fatherID = 3, name = "成都市" });

                    districts.Add(new District() { id = 10, fatherID = 3, name = "遂宁市" });

                    districts.Add(new District() { id = 11, fatherID = 3, name = "巴中市" });

                    districts.Add(new District() { id = 12, fatherID = 3, name = "绵阳市" });

                    districts.Add(new District() { id = 13, fatherID = 3, name = "南充市" });

                    return Json(districts);

                default:

                    districts.Add(new District() { id = 14, fatherID = -1, name = "不知道你选了什么∑q|?Д?|p" });

                    return Json(districts);

            }

        }

Model

1

2

3

4

5

6

7

8

9

public class District

{

    public int id { get; set; }

    /// <summary>

    /// 根节点FatherID=0

    /// </summary>

    public int fatherID { get; set; }

    public string name { get; set; }

}


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://www.cnblogs.com/Lulus/p/7874113.html
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计