首页 技术 正文
技术 2022年11月21日
0 收藏 650 点赞 3,924 浏览 5659 个字

级联下拉列表是项目中常用到的。比如省市县,比如企业性质等,做成一个js通用组件,

在静态页出来后可以直接插入,将数据和html静态页做一个解耦。

贴出来抛砖引玉吧。

/**
* @author sunfengjia edit
* 说明: 使用json数据格式,生成N级下拉框
* params:
* data: json对象
* select: 级联下拉框名称 调用eg:
var comboselect = ComboSelectFactory(data, 'p1', 'p2', 'p3', 'p4'); idKey: 数据的id字段名
textKey: 数据的显示文本字段名
parentIdKey: 数据的父节点字段名
topParentValue:最顶层的值
设定下拉列表 idKey,textKey,parentIdKey,topParent名称的方法eg:
comboselect.setProperties({idKey:'id',textKey:'text',parentIdKey:'parentid',topParentValue:'-1'}); 初始化下拉列表集合的方法eg:
comboselect.initSelect('gd');
*/var topParentValue = "-";
var idKey = "id", textKey = "text", parentIdKey = "parentid";var ComboSelectFactory = function(data){
return new ComboSelect(arguments);
}var ComboSelect = function(data){
this.myData = [].slice.call(data, 0, 1)[0];
this.ids = [].slice.call(data, 1);
this.setProperties({});
}ComboSelect.prototype.setProperties = function(opt){
this.topParentValue = opt.topParentValue || topParentValue;
this.idKey = opt.idKey || idKey;
this.textKey = opt.textKey || textKey;
this.parentIdKey = opt.parentIdKey || parentIdKey; for(var i=0, len=this.ids.length-1; i<len; i++){
var o = this.$(this.ids[i]);
//给第i个select添加change事件,初始化该下拉列表的下级select
this.addEventHandler(o, 'change', this.eventHandle(o,i));
} this.initChild(null, 0);
}ComboSelect.prototype.eventHandle = function(o,i) {
var self = this;
var oreg = o;
var index = i+1;
return function() {
self.initChild(oreg, index);
}
}/**
* 说明: 当select选定一个option后,根据该option的值,查找 parentID 为该 optionID 的项,
* 生成option,插入下级select。
* @params:
* oSelect: 父级select对象
* index: 父级select索引
*/
ComboSelect.prototype.initChild = function(oSelect, index){
var p = null == oSelect ? this.topParentValue : oSelect.options[oSelect.selectedIndex].value;
var ds = this.getChilds(p);
this.clearSelect(index);
var child = this.$(this.ids[index]);
for(var i=0, len=ds.length; i<len; i++){
var currentObj = ds[i];
child.options[child.length] = new Option(currentObj[this.textKey], currentObj[this.idKey]);
}
}/**
* 说明: 查找指定ID的子
* @params:
* parentID: 查找该ID的子
* returns: 子的array
*/
ComboSelect.prototype.getChilds = function(parentID) {
var childs = [];
for(var i=0, len=this.myData.length; i<len; i++){
if(parentID == this.myData[i][this.parentIdKey]){
childs.push(this.myData[i]);
}
}
return childs;
}/**
* 说明: 将索引以下的select清空
* @params:
* index: 父级对象索引
*/
ComboSelect.prototype.clearSelect = function(index) {
for(var i=index, len=this.ids.length; i<len; i++){
this.$(this.ids[i]).length=1;
}
}/**
* 说明: 初始化下拉列表集合。
* @params:
* id: 选中项ID
*/
ComboSelect.prototype.initSelect = function(id){
var parentids = [];
parentids = this.getParent(id);
for (var i=0, len=this.ids.length; i<len; i++){
//顶层直接初始化,子级select用父级的选中值初始化
if(i==0){
this.initChild(null, 0);
}else{
this.initChild(this.$(this.ids[i-1]),i);
}
if(parentids[i]!=null){
this.$(this.ids[i]).value = parentids[i][this.idKey];
}
}
}/**
* 说明: 得到指定ID的父级array
* @params:
* id: 选中项ID
*/
ComboSelect.prototype.getParent = function(id) {
var parents = [];
for(var i=0, len=this.myData.length; i<len; i++){
if(id == this.myData[i][this.idKey]){
if(this.myData[i][this.parentIdKey] == this.topParentValue){
parents.push(this.myData[i]);
break;
}else{
parents = this.getParent(this.myData[i][this.parentIdKey]);
parents.push(this.myData[i]);
}
}
}
return parents;
}ComboSelect.prototype.$ = function(sid) {
return document.getElementById(sid);
}ComboSelect.prototype.addEventHandler = function(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = fnHandler;
}
}

html页面中的调用

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script type='text/javascript' src='scripts/ComboSelect.js'></script></head>
<script type="text/javascript">/*-- 数据定义 ---------------------------*/
var data = []; data.push({id:'cn', text:'中国', parentid:'-'}); data.push({id:'fj', text:'福建', parentid:'cn'});
data.push({id:'gd', text:'广东', parentid:'cn'}); data.push({id:'fz', text:'福州', parentid:'fj'});
data.push({id:'xm', text:'厦门', parentid:'fj'});
data.push({id:'ly', text:'龙岩', parentid:'fj'}); data.push({id:'fz-fq', text:'福州1', parentid:'fz'});
data.push({id:'fz-mh', text:'福州2', parentid:'fz'});
data.push({id:'fz-cl', text:'福州3', parentid:'fz'}); data.push({id:'xm-dn', text:'厦门1', parentid:'xm'});
data.push({id:'xm-jm', text:'厦门2', parentid:'xm'});
data.push({id:'xm-xl', text:'厦门3', parentid:'xm'}); data.push({id:'yl-xl', text:'龙岩1', parentid:'ly'});
data.push({id:'yl-lc', text:'龙岩2', parentid:'ly'});
data.push({id:'yl-sh', text:'龙岩3', parentid:'ly'});
data.push({id:'yl-wp', text:'龙岩4', parentid:'ly'}); data.push({id:'gz', text:'广州', parentid:'gd'});
data.push({id:'sz', text:'深圳', parentid:'gd'});
data.push({id:'mx', text:'梅县', parentid:'gd'}); data.push({id:'gz-fq', text:'广州1', parentid:'gz'});
data.push({id:'gz-mh', text:'广州2', parentid:'gz'});
data.push({id:'gz-cl', text:'广州3', parentid:'gz'}); data.push({id:'sz-dn', text:'深圳1', parentid:'sz'});
data.push({id:'sz-jm', text:'深圳2', parentid:'sz'});
data.push({id:'sz-xl', text:'深圳3', parentid:'sz'}); data.push({id:'mx-xl', text:'梅县1', parentid:'mx'});
data.push({id:'mx-lc', text:'梅县2', parentid:'mx'});
data.push({id:'mx-sh', text:'梅县3', parentid:'mx'});
data.push({id:'mx-wp', text:'梅县4', parentid:'mx'}); data.push({id:'ny', text:'纽约', parentid:'am'});
data.push({id:'hsd', text:'华盛顿', parentid:'am'});
data.push({id:'am', text:'美国', parentid:'-'});function makeArray(arg1, arg2){
return [ this, arg1, arg2 ];
}
//在onload后执行
window.onload = function() {
comboselect = ComboSelectFactory(data, 'p1', 'p2', 'p3', 'p4');
comboselect.setProperties({idKey:'id', texKey:'text', parentIdKey:'parentid', topParentValue:'-'});
comboselect.initSelect('mx-wp');
}
</script>
<body>
<select id="p1"><option>-选择-</option></select><br/>
<select id="p2"><option>-选择-</option></select><br/>
<select id="p3"><option>-选择-</option></select><br/>
<select id="p4"><option>-选择-</option></select>
</body> </html>
comboselect.setProperties方法可以根据数据值自己自定义代码已上传https://github.com/sunfengjiajia/spring-boot-test
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905