级联下拉列表是项目中常用到的。比如省市县,比如企业性质等,做成一个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

js基于json的级联下拉框的更多相关文章

  1. struts-hibernate-ajax完成区县和街道级联下拉框功能(二补充使用json解析list结果集,ajax循环json层级处理)

    针对<struts-hibernate-ajax完成区县和街道级联下拉框功能>进行补充,上一篇中,要在action中拼接JSON格式字符串,很容易手抖.直接用json处理一下转成json格 ...

  2. JS级联下拉框

    //Ajax级联获取SDKfunction GetDropDownList(parent_ddlID, fill_dllID, url, param) {    this.pId = parent_d ...

  3. jQuery无限级联下拉框插件

    自己编写jQuery插件 之 无限级联下拉框   因为是级联,所以数据必须是树型结构的,我这里的测试数据如下: 看下效果图: 1.>图一: 2.>图二: 3.>图三: 由图可知,下拉 ...

  4. JQuery和ASP.NET分别实现级联下拉框效果

    在学习JQuery之前知道下拉框的级联效果可以通过asp.net控件实现,现在学习了JQuery,知道了JQuery和select也能实现.我分别举两个小例子说明这两种方法如何实现. 1.用JQuer ...

  5. 自动补齐flexselect+级联下拉框案例

    在开发web应用时,经常遇到类似省市区级联下拉框操作,即选中省份自动级联加载该省份所有的市,选中市自动级联加载该市所有的区:假设省市区的数据量很大,此时用户想选中某市,因而要从上往下查找,可能半天都找 ...

  6. EXCEL(1)级联下拉框

    EXCEL级联下拉框 http://jingyan.baidu.com/article/3c343ff756e0cf0d377963f9.html 在输入一些多级项目时,如果输入前一级内容后,能够自动 ...

  7. js实现可输入的下拉框

    <HTML> <HEAD> <META http-equiv='Content-Type' content='text/html; charset=gb2312'> ...

  8. Vue.js中使用select选择下拉框

    在Vue.js中使用select选择下拉框有两种方法: 第一种: Add.html: <select v-model="sysNotice.noticeType" id=&q ...

  9. C# ,数据导出到带有级联下拉框的模板(一,模板的级联功能)

    一.首先解决如何做模板中增加级联功能 1,首先打开一个新的Excel文件,新增sheet,把分类保存在里面,如下图所示 2.回到sheet1,选中要增加下拉框的行(注意:请排除首行,首行是标题) 3. ...

随机推荐

  1. 2018.12.22 bzoj3277: 串(后缀自动机+启发式合并)

    传送门 跟这道题是一模一样的. 于是本蒟蒻又写了一遍10min1A庆祝 代码: #include<bits/stdc++.h> #define ri register int using ...

  2. GDI基础(3):绘制图片

    1.CBitmap位图类封装了Windows GDI中的位图和操作位图的成员函数.CPen.CBrush.CFont.CBitmap是常用的Windows GDI对象,和CFont一样,CBitmap ...

  3. Gym-101102-K-Topological Sort

    K. Topological Sort 题面 Consider a directed graph G of N nodes and all edges (u→v) such that u < v ...

  4. C++标准库addressof的应用

    C++11将addressof作为标准库的一部分,用于取变量和函数等内存地址. 代码示例: #include <memory> #include <stdio.h> void ...

  5. Unicode和多字节的相互转换

    多字节转Unicode 四步: Step1 #include <iostream> #include "windows.h" using namespace std; ...

  6. rpcbind.service启动失败

    新装的服务器,启动rpcbind.service通常失败,执行下面的两个命令经常卡死,一直不返回,也不报错 #systemctl start nfs-server.service #systemctl ...

  7. iOS程序的执行顺序 和 UIViewController的生命周期

    iOS程序的执行顺序 1 进入程序的入口 进入main函数, 设置AppDelegate称为函数的代理 2  程序完成加载 -[AppDelegate application:didFinishLau ...

  8. MySQL中的数据约束

    什么是数据约束: 对用户操作表的数据进行约束 1.默认值: 作用:当用户对使用默认值的字段不插入值的时候,就使用默认值. 注意:1)对默认值字段插入null是可以的. 2)对默认值字段可以插入非nul ...

  9. Python基础的练习

    ---恢复内容开始--- 简单输入输出交互. >>> name='Jame' >>> print('Hi,%s.'%name) Hi,Jame. >>& ...

  10. hive中数据存储格式对比:textfile,parquent,orc,thrift,avro,protubuf

    这篇文章我会从业务中关注的: 1. 存储大小 2.查询效率 3.是否支持表结构变更既数据版本变迁 5.能否避免分隔符问题 6.优势和劣势总结 几方面完整的介绍下hive中数据以下几种数据格式:text ...