树型下拉框组件的构建
2016-01-05 16:09
232 查看
//js代码
$(function (){
// 扩展combotree
createCrewTree();
// 树形选择框
$("#crewName").combotree({
url: "crewController/getAllCrewNameJsonTree.do",
onSelect:function(node){
$("#crewName").val(node.text);
}
});
});
// 扩展combotree
function createCrewTree(){
$.fn.combotree.defaults.editable = true;
$.extend($.fn.combotree.defaults.keyHandler,{
up:function(){
console.log('up');
},
down:function(){
console.log('down');
},
enter:function(){
console.log('enter');
},
query:function(q){
var t = $(this).combotree('tree');
t.tree("expandAll");
var nodes = t.tree('getChildren');
for(var i=0; i<nodes.length; i++){
var node = nodes[i];
if (node.text.indexOf(q) >= 0){
$(node.target).show();
node.show = true;
} else {
$(node.target).hide();
node.show = false;
}
}
if(q == "") {
t.tree("collapseAll");
return;
}
for(var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if(node.children) {
var flag = false;
for(var j = 0;j < node.children.length; j++) {
var child = node.children[j];
if(child.show == true) {
flag = true;
break;
}
}
if(flag) {
$(node.target).show();
}
}
}
var opts = $(this).combotree('options');
if (!opts.hasSetEvents){
opts.hasSetEvents = true;
var onShowPanel = opts.onShowPanel;
opts.onShowPanel = function(){
var nodes = t.tree('getChildren');
for(var i=0; i<nodes.length; i++){
$(nodes[i].target).show();
}
onShowPanel.call(this);
};
$(this).combo('options').onShowPanel = opts.onShowPanel;
}
}
});
}
//java代码
public String getAllCrewNameJsonTree() {
List<Process> processTypeList = processDao.getProcesssByCondition("");
List<String> processNames = new ArrayList<String>();
List<List> processNameLists = new ArrayList<List>();
String result = "[";
//从productTypeDao中将产品类型读出
for(int i = 0;i < processTypeList.size(); i++) {
String name = processTypeList.get(i).getProcessName();
if(!name.equals(ConstantManage.CONSTANTWFL)) {
processNames.add(name);
}
}
//根据产品类型,分别把每个类型的各个产品读出
for(int i = 0; i < processNames.size(); i++) {
String processName = processNames.get(i);
List<Crew> processList = dao.getCrewsByCondition(" processName = \"" + processName + "\";");
List<String> processNameList = new ArrayList<String>();
for(int j = 0;j < processList.size(); j++) {
processNameList.add(processList.get(j).getCrewName());
}
processNameLists.add(processNameList);
}
//处理这些产品,将其转换为json字符串
for(int i = 0; i < processNames.size(); i++) {
if(i == processNames.size() - 1) {
String item = "{ \"attribute\":\"node\",";
item += "\"text\":\"" + processNames.get(i) + "\", ";
String children = "\"children\":[";
List<String> processNameList = processNameLists.get(i);
for(int j = 0; j < processNameList.size(); j++) {
if(j == processNameList.size() - 1) {
children += "{\"attribute\":\"leave\",\"text\":\"" + processNameList.get(j) + "\"}]";
}
else {
children += "{\"attribute\":\"leave\",\"text\":\"" + processNameList.get(j) + "\"},";
}
}
if(processNameList.size() == 0) {
item += "\"state\":\"open\"}";
}else {
item += "\"state\":\"closed\"," + children + "}";
}
result += item;
}
else {
String item = "{\"attribute\":\"node\",";
item += "\"text\":\"" + processNames.get(i) + "\",";
String children = "\"children\":[";
List<String> processNameList = processNameLists.get(i);
for(int j = 0; j < processNameList.size(); j++) {
if(j == processNameList.size() - 1) {
children += "{\"attribute\":\"leave\",\"text\":\"" + processNameList.get(j) + "\"}]";
}
else {
children += "{\"attribute\":\"leave\",\"text\":\"" + processNameList.get(j) + "\"},";
}
}
if(processNameList.size() == 0) {
item += "\"state\":\"open\"}";
}else {
item +="\"state\":\"closed\"," + children + "}";
}
result += item + ",";
}
}
result += "]";
System.out.println(result);
return result;
}
}
$(function (){
// 扩展combotree
createCrewTree();
// 树形选择框
$("#crewName").combotree({
url: "crewController/getAllCrewNameJsonTree.do",
onSelect:function(node){
$("#crewName").val(node.text);
}
});
});
// 扩展combotree
function createCrewTree(){
$.fn.combotree.defaults.editable = true;
$.extend($.fn.combotree.defaults.keyHandler,{
up:function(){
console.log('up');
},
down:function(){
console.log('down');
},
enter:function(){
console.log('enter');
},
query:function(q){
var t = $(this).combotree('tree');
t.tree("expandAll");
var nodes = t.tree('getChildren');
for(var i=0; i<nodes.length; i++){
var node = nodes[i];
if (node.text.indexOf(q) >= 0){
$(node.target).show();
node.show = true;
} else {
$(node.target).hide();
node.show = false;
}
}
if(q == "") {
t.tree("collapseAll");
return;
}
for(var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if(node.children) {
var flag = false;
for(var j = 0;j < node.children.length; j++) {
var child = node.children[j];
if(child.show == true) {
flag = true;
break;
}
}
if(flag) {
$(node.target).show();
}
}
}
var opts = $(this).combotree('options');
if (!opts.hasSetEvents){
opts.hasSetEvents = true;
var onShowPanel = opts.onShowPanel;
opts.onShowPanel = function(){
var nodes = t.tree('getChildren');
for(var i=0; i<nodes.length; i++){
$(nodes[i].target).show();
}
onShowPanel.call(this);
};
$(this).combo('options').onShowPanel = opts.onShowPanel;
}
}
});
}
//java代码
public String getAllCrewNameJsonTree() {
List<Process> processTypeList = processDao.getProcesssByCondition("");
List<String> processNames = new ArrayList<String>();
List<List> processNameLists = new ArrayList<List>();
String result = "[";
//从productTypeDao中将产品类型读出
for(int i = 0;i < processTypeList.size(); i++) {
String name = processTypeList.get(i).getProcessName();
if(!name.equals(ConstantManage.CONSTANTWFL)) {
processNames.add(name);
}
}
//根据产品类型,分别把每个类型的各个产品读出
for(int i = 0; i < processNames.size(); i++) {
String processName = processNames.get(i);
List<Crew> processList = dao.getCrewsByCondition(" processName = \"" + processName + "\";");
List<String> processNameList = new ArrayList<String>();
for(int j = 0;j < processList.size(); j++) {
processNameList.add(processList.get(j).getCrewName());
}
processNameLists.add(processNameList);
}
//处理这些产品,将其转换为json字符串
for(int i = 0; i < processNames.size(); i++) {
if(i == processNames.size() - 1) {
String item = "{ \"attribute\":\"node\",";
item += "\"text\":\"" + processNames.get(i) + "\", ";
String children = "\"children\":[";
List<String> processNameList = processNameLists.get(i);
for(int j = 0; j < processNameList.size(); j++) {
if(j == processNameList.size() - 1) {
children += "{\"attribute\":\"leave\",\"text\":\"" + processNameList.get(j) + "\"}]";
}
else {
children += "{\"attribute\":\"leave\",\"text\":\"" + processNameList.get(j) + "\"},";
}
}
if(processNameList.size() == 0) {
item += "\"state\":\"open\"}";
}else {
item += "\"state\":\"closed\"," + children + "}";
}
result += item;
}
else {
String item = "{\"attribute\":\"node\",";
item += "\"text\":\"" + processNames.get(i) + "\",";
String children = "\"children\":[";
List<String> processNameList = processNameLists.get(i);
for(int j = 0; j < processNameList.size(); j++) {
if(j == processNameList.size() - 1) {
children += "{\"attribute\":\"leave\",\"text\":\"" + processNameList.get(j) + "\"}]";
}
else {
children += "{\"attribute\":\"leave\",\"text\":\"" + processNameList.get(j) + "\"},";
}
}
if(processNameList.size() == 0) {
item += "\"state\":\"open\"}";
}else {
item +="\"state\":\"closed\"," + children + "}";
}
result += item + ",";
}
}
result += "]";
System.out.println(result);
return result;
}
}
相关文章推荐
- angularjs中button点击变色的实现
- Tornado Web 防止XSS攻击,即对变量进行特殊字符过滤
- word缩印
- 问题:无法获得锁 /var/lib/dpkg/lock - open (11: 资源临时不可用)
- axis2开发webservice接口入门到精通详解
- axis2开发webservice接口入门到精通详解
- Android应用数字证书生成和签名
- JavaScript高级程序设计--读书心得
- iOS使用位置和方向服务(来自苹果apple官方)
- MySQL 用户和权限
- 自定义ActionBar、Toolbar布局(解决自定义ActionBar布局左边始终有一点边距的问题)
- OpenGL(5)--太阳系和的日月地实现
- 向脚本传递参数
- 大数据技术Hadoop入门理论系列之一----hadoop生态圈介绍
- java面试题
- 10 个非常有用的 AngularJS 框架
- afnetworking传图片与其他参数
- 新的。
- 使用Dapper接收多个结果集,并实现存储过程分页
- 深入浅析JavaScript系列(13):This? Yes,this!