angular 级联选择

HTML:

<link rel="stylesheet" href="views/tree/checkbox.css"/> <div class="tree"> <ul> <li ng-repeat="country in vm.countries" ng-class="{closed:country.closed}"> <div> <input type="checkbox" ng-model="country.checked" ng-change="vm.countryChanged(country)" ng-class="{intermediate: vm.isIntermediateCountry(country)}"/> <span ng-click="country.closed=!country.closed"> <span class="glyphicon" ng-class="country.closed?‘glyphicon-plus‘:‘glyphicon-minus‘"></span> <img ng-src="images/{{country.flag}}"/> {{country.label}} </span> </div> <ul ng-class="{hidden: country.closed}"> <li ng-repeat="province in country.provinces" ng-class="{closed:province.closed}"> <input type="checkbox" ng-model="province.checked" ng-change="vm.provinceChanged(province, country)" ng-class="{intermediate: vm.isIntermediateProvince(province)}"/> <span ng-click="province.closed=!province.closed"> <span class="glyphicon" ng-class="province.closed?‘glyphicon-plus‘:‘glyphicon-minus‘"></span> {{province.label}} </span> <ul ng-class="{hidden: province.closed}"> <li ng-repeat="city in province.cities"> <label> <input type="checkbox" ng-model="city.checked" ng-change="vm.cityChanged(city, province, country)"/> {{city.label}} </label> </li> </ul> </li> </ul> </li> </ul> </div> <h3>说明</h3> 这是一个使用controller中的逻辑来实现级联复选框的表,可以支持级联选择以及半选中(semi-checked)状态

JS:

‘use strict‘; angular.module(‘ngShowcaseApp‘).controller(‘ctrl.tree.checkbox‘, function ($scope, CityData) { var vm = $scope.vm = {}; vm.countries = CityData; vm.countryChanged = function(country) { // 自动选中所有下级 _.each(country.provinces, function(province) { province.checked = country.checked; _.each(province.cities, function(city) { city.checked = country.checked; }); }); }; vm.provinceChanged = function(province, country) { // 自动选中所有下级 _.each(province.cities, function(city) { city.checked = province.checked; }); // 如果有任何一个子节点被选中,则让上级节点也选中 // 注意!checkbox的ng-model只能绑定到逻辑型值,所以不能直接把findWhere的结果赋值过去 country.checked = !!_.findWhere(country.provinces, {checked: true}) }; vm.cityChanged = function(city, province, country) { // 如果有任何一个子节点被选中,则让上级节点也选中 // 注意!checkbox的ng-model只能绑定到逻辑型值,所以不能直接把findWhere的结果赋值过去 province.checked = !!_.findWhere(province.cities, {checked: true}); country.checked = !!_.findWhere(country.provinces, {checked: true}); }; vm.isIntermediateCountry = function(country) { // 是否有任何被选中的节点 var hasChecked = _.find(country.provinces, function(province) { return province.checked && _.findWhere(province.cities, {checked: true}); }); // 是否有任何没有选中的节点 var hasNoChecked = _.find(country.provinces, function(province) { return !province.checked || _.findWhere(province.cities, {checked: false}); }); // 如果同时有选中状态和非选中的节点,则为中间状态 return hasChecked && hasNoChecked; }; vm.isIntermediateProvince = function(province) { var hasChecked = _.findWhere(province.cities, {checked: true}); var hasNoChecked = _.findWhere(province.cities, {checked: false}); return hasChecked && hasNoChecked; }; });

CSS:

.tree li { cursor: pointer; padding-left: 1.3em; } .tree ul { list-style: none; padding-left: 0; } .intermediate { opacity: 0.3; }

时间: 2024-10-13 00:59:40

angular 级联选择的相关文章

extjs tree check 级联选择

extjs4 tree check 级联选择 实现效果: 关键代码: function changeAllNode(node, isCheck) { allChild(node, isCheck); allParent(node, isCheck); function allChild(nodec, isCheckc) { var chileNodes = nodec.childNodes; Ext.Array.each(chileNodes, function (nd) { if (nd.ha

利用InfoPath实现SharePoint Server 2013列表的级联选择(Cascading Drop Down List)

最近在利用SharePoint Server 2013的列表组织和存储数据,发现SharePoint列表原始不支持级联选择的功能. 谷歌百度一通以后,发现了很多通过代码实现的方案,利用第三方的插件spservices的http://spservices.codeplex.com/releases/view/119578,或者自己编段代码的http://www.cnblogs.com/jianyus/p/3812758.html(JQuery+JavaScript),但觉得都不太理想.最后发现可以

省市区级联选择

<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>省市区级联选择</title> <script type="text/javascript" src="

iview Cascader级联选择省市区问题,并附上数据

vue项目中,用到了iview的Cascader级联选择省市区,之前用的是iview-area这个插件做的省市区下拉选择, 没啥大问题,就是选择省市区后,屏幕会抖动一下,体验不好,又找不到解决办法, 之后重新选择了iview中的Cascader <FormItem label="省市区:" prop="chinaArea"> <Cascader v-model="eroomInfo.chinaArea" :data="

Js异步级联选择框实践方法

HTML: <li> <span>所在地区:</span> <select name="prov" id="ddl_prov" onchange="event_change_prov(this);" class="required" missingmsg="请选择省"> <option value="">请选择省</opt

WdatePicker控件级联选择

控件代码 <td nowrap="true"> <input style="width: 90px;" onclick="WdatePicker({ dateFmt: 'yyyy-MM',startDate:'%y-%M' })" class="Wdate" name="DateMonth" id="DateMonth"> 月 </td> <td

JSP下拉选框,级联选择

前端: <%@ page contentType="text/html;charset=UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Cont

基于jQuery + JSON 的级联选择效果

昨天做项目的时候遇到了一个填写地址时选择省市的效果,看到了一个非常好用的 js ,这个是基于jQuery + JSON实现的,文件 jquery.cityselect.js 只有5kb 很小很实用. html 首先在 <head>中添加引用 jQuery 和 cityselect 插件. 1<script src="js/jquery.2.1.4.js" type="text/javascript" charset="utf-8"

app级联选择-sencha touch-博士