您的位置:首页 > 其它

树型下拉框组件的构建

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: