Angular实现一个简单的多选复选框的弹出框指令
之前的文章有写过包含树结构下拉框的。
要实现一个包含多个复选框的下拉框该如何做呢?
先上个效果图吧:
代码:
<!DOCTYPE html><html ng-app="app"><head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" type="text/css" href="../bootstrap/css/bootstrap.css"> <script src="../jquery.js"></script> <script src="../bootstrap/js/bootstrap.js"></script> <style type="text/css"> label { display: block; margin-top: 3px; } label.list:hover { cursor: pointer; background-color: red; } label.selected { background-color: red; } </style> <script src="../angular.js"></script> <script type="text/javascript"> angular.module("app", []) .controller("ctrl", function($scope) { jQuery(".dropdown-menu *").click(function(e){ e.stopPropagation(); }); $scope.selectList = [{ name: "选项1", value: "item1", select: false }, { name: "选项2", value: "item2", select: true }]; $scope.$watch("selectList", function(n, o) { $scope.result = (function(arr) { var t = []; for (var i = 0; i < arr.length; i++) { if (arr[i].select) { t.push(arr[i].name); } } if (!t.length) { t.push("--请选择--"); } return t.join(","); })($scope.selectList); }, true) }) .directive("multiSelect", function() { return { scope: { data: "=multiSelect" }, templateUrl: "option.html" } }) </script></head><body> <div ng-controller="ctrl"> <div class="dropdown"> <span class="dropdown-toggle", data-toggle="dropdown"> <button ng-bind="result"> </button> <span class="caret"></span> </span> <ul class="dropdown-menu"> <li> //下拉框数据绑定 <div multi-select="selectList"></div> </li> </ul> </div> </div></body></html>
- 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
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
指令模版代码option.html:
<label for="{{'check_' + $index}}" ng-class="{list:true, selected:data[$index].select}" ng-repeat = "item in data"> //为不同的选项定义不同的id <input id="{{'check_' + $index}}" type="checkbox" ng-model="data[$index].select"> {{item.name}}</label>
- 1
- 2
- 3
- 4
- 5
- 6
此文档的作者:justforuse
Github Pages:justforuse
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.****.net/jiangjunshow
之前的文章有写过包含树结构下拉框的。
要实现一个包含多个复选框的下拉框该如何做呢?
先上个效果图吧:
代码:
<!DOCTYPE html><html ng-app="app"><head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" type="text/css" href="../bootstrap/css/bootstrap.css"> <script src="../jquery.js"></script> <script src="../bootstrap/js/bootstrap.js"></script> <style type="text/css"> label { display: block; margin-top: 3px; } label.list:hover { cursor: pointer; background-color: red; } label.selected { background-color: red; } </style> <script src="../angular.js"></script> <script type="text/javascript"> angular.module("app", []) .controller("ctrl", function($scope) { jQuery(".dropdown-menu *").click(function(e){ e.stopPropagation(); }); $scope.selectList = [{ name: "选项1", value: "item1", select: false }, { name: "选项2", value: "item2", select: true }]; $scope.$watch("selectList", function(n, o) { $scope.result = (function(arr) { var t = []; for (var i = 0; i < arr.length; i++) { if (arr[i].select) { t.push(arr[i].name); } } if (!t.length) { t.push("--请选择--"); } return t.join(","); })($scope.selectList); }, true) }) .directive("multiSelect", function() { return { scope: { data: "=multiSelect" }, templateUrl: "option.html" } }) </script></head><body> <div ng-controller="ctrl"> <div class="dropdown"> <span class="dropdown-toggle", data-toggle="dropdown"> <button ng-bind="result"> </button> <span class="caret"></span> </span> <ul class="dropdown-menu"> <li> //下拉框数据绑定 <div multi-select="selectList"></div> </li> </ul> </div> </div></body></html>
- 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
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
指令模版代码option.html:
<label for="{{'check_' + $index}}" ng-class="{list:true, selected:data[$index].select}" ng-repeat = "item in data"> //为不同的选项定义不同的id <input id="{{'check_' + $index}}" type="checkbox" ng-model="data[$index].select"> {{item.name}}</label>
- 1
- 2
- 3
- 4
- 5
- 6
此文档的作者:justforuse
Github Pages:justforuse