js基于json的级联下拉框
级联下拉列表是项目中常用到的。比如省市县,比如企业性质等,做成一个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的级联下拉框的更多相关文章
- struts-hibernate-ajax完成区县和街道级联下拉框功能(二补充使用json解析list结果集,ajax循环json层级处理)
针对<struts-hibernate-ajax完成区县和街道级联下拉框功能>进行补充,上一篇中,要在action中拼接JSON格式字符串,很容易手抖.直接用json处理一下转成json格 ...
- JS级联下拉框
//Ajax级联获取SDKfunction GetDropDownList(parent_ddlID, fill_dllID, url, param) { this.pId = parent_d ...
- jQuery无限级联下拉框插件
自己编写jQuery插件 之 无限级联下拉框 因为是级联,所以数据必须是树型结构的,我这里的测试数据如下: 看下效果图: 1.>图一: 2.>图二: 3.>图三: 由图可知,下拉 ...
- JQuery和ASP.NET分别实现级联下拉框效果
在学习JQuery之前知道下拉框的级联效果可以通过asp.net控件实现,现在学习了JQuery,知道了JQuery和select也能实现.我分别举两个小例子说明这两种方法如何实现. 1.用JQuer ...
- 自动补齐flexselect+级联下拉框案例
在开发web应用时,经常遇到类似省市区级联下拉框操作,即选中省份自动级联加载该省份所有的市,选中市自动级联加载该市所有的区:假设省市区的数据量很大,此时用户想选中某市,因而要从上往下查找,可能半天都找 ...
- EXCEL(1)级联下拉框
EXCEL级联下拉框 http://jingyan.baidu.com/article/3c343ff756e0cf0d377963f9.html 在输入一些多级项目时,如果输入前一级内容后,能够自动 ...
- js实现可输入的下拉框
<HTML> <HEAD> <META http-equiv='Content-Type' content='text/html; charset=gb2312'> ...
- Vue.js中使用select选择下拉框
在Vue.js中使用select选择下拉框有两种方法: 第一种: Add.html: <select v-model="sysNotice.noticeType" id=&q ...
- C# ,数据导出到带有级联下拉框的模板(一,模板的级联功能)
一.首先解决如何做模板中增加级联功能 1,首先打开一个新的Excel文件,新增sheet,把分类保存在里面,如下图所示 2.回到sheet1,选中要增加下拉框的行(注意:请排除首行,首行是标题) 3. ...
随机推荐
- mysql 5.7 修改密码
mysql 5.7 ,user表就没有password 这个字段了. ') where user='root' and host='localhost'; 这样当然就改不了密码了. ') where ...
- hdu-1394(线段树&逆序数的性质和求法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目大意: 给出一个序列,一对逆序数就是满足i<j&&a[i]>a[ ...
- 安装kafka 集群 步骤
1.下载 http://mirror.bit.edu.cn/apache/kafka/2.1.0/kafka_2.11-2.1.0.tgz 2.解压 tar -zxvf kafka_2.11-2.1 ...
- Java数据类型的转换
Java数据类型,从小到大排序 byte ,short ,int ,long ,float, double,char 1.小数据类型转换大的数据类型,自动转换 int a = 3; double b ...
- python模块:pickle
"""Create portable serialized representations of Python objects. See module copyreg f ...
- Redis-环境搭建
Redis官方不提供Windows版,不过微软开源组织提供了Windows版本的Redis,此处将安装Windows版的Reids,供学习使用. 1.下载Windows版Redis安装包: 安装包地址 ...
- git服务器使用
服务器版本:CentOS6.3 root用户密码:123456 服务器地址:192.168.1.125 搭建Git服务器参考:搭建Git服务器 使用git服务器首先要克隆仓库,即添加一个远程仓库,参考 ...
- C#-VS SQLServer数据库编程-摘
ado.net 通用类对象.在本地内存暂存数据 托管类对象.让本地通用类对象连接数据库,让本地通用类对象和数据库同步 连接数据库 new connection(connectstring) comma ...
- noip第19课作业
1. 谁考了第K名 在一次考试中,每个学生的成绩都不相同,现知道了每个学生的学号和成绩,求考第k名学生的学号和成绩. [输入格式] 第一行有两个整数,分别是学生的人数n(1<=n<=100 ...
- C语言中:static与extern对变量和函数的作用
1.两者对全局变量 static对全局变量,表示定义一个内部变量 extern对全局变量,表示声明一个外部变量 说明: 1.内部变量:定义的变量只能在本文件中访问,不能被其他文件访问. 2.不同文件中 ...