jquery下拉列表四级快速反显
前台:
$('#source').combotree({
url:'${ctx}/dict/pub/dict-tree-box!closedTree2.action?cateEname=source&abc=${carTask.assetsSrcEn}',
onBeforeLoad:function(node){
if(node){
vartree=$('#source').combotree('tree');
$(tree).tree('options').url='${ctx}/dict/pub/dict-tree-box!closedTree.action?cateEname=source&id='+node.id;
}
},
onBeforeSelect:function(node){
//此处用于模拟单选时文件夹不能选择,主要通过后台数据attributes属性来标识
vart=$('#source').combotree('tree');
if(t.tree('isLeaf',node.target)){
returntrue;
}else{
window.setTimeout('source()',0);
returnfalse;
}
},
onChange:function(newValue,oldValue){
},
onSelect:function(node){
},
value:"${carTask.assetsSrcEn}",
panelHeight:400
});
<selectid="source"name="source"style="width:295px;"class="easyui-validatebox"validType="selectValid['--请选择--']">
</select>
后台:@Namespace("/dict/pub")
@Results({})
publicclassDictTreeBoxActionextendsActionSupport{
privatestaticfinallongserialVersionUID=7945924152310819279L;
@Autowired
privateDataItemManagerdataItemManager;
privateStringcateEname;
privateStringfatherValue;
privateBooleanisShowPrefix;
privateStringabc;
publicBooleangetIsShowPrefix(){
returnisShowPrefix;
}
publicvoidsetIsShowPrefix(BooleanisShowPrefix){
this.isShowPrefix=isShowPrefix;
}
publicStringgetCateEname(){
returncateEname;
}
publicvoidsetCateEname(StringcateEname){
this.cateEname=cateEname;
}
publicStringgetFatherValue(){
returnfatherValue;
}
publicvoidsetFatherValue(StringfatherValue){
this.fatherValue=fatherValue;
}
publicStringgetAbc(){
returnabc;
}
publicvoidsetAbc(Stringabc){
this.abc=abc;
}
/*
*/
publicStringcreateIds(String[]strs,inti){
Stringstr="";
for(intj=0;j<i;j++){
if(j!=i-1){
str+=strs[j]+"-";
}else{
str+=strs[j];
}
}
returnstr;
}
/**
*反显
*@return
*@throwsException
*/
publicStringclosedTree2()throwsException{
String[]strs=abc.split("-");
intlength=strs.length-1;
List<DataItem>rootList=createItem(cateEname,"0");
List<TreeNode>rootTreeNodes=createTreeNodeList(rootList,null);
List<TreeNode>treeNodes=newArrayList<TreeNode>();
for(inti=length;i>0;i--){
if(i==length){
List<DataItem>list=createItem(cateEname,strs[i-1]);
Stringids=createIds(strs,i);
treeNodes=createTreeNodeList(list,ids);
}else{
List<DataItem>list=createItem(cateEname,strs[i-1]);
Stringids=createIds(strs,i);
Stringids1=createIds(strs,i+1);
List<TreeNode>newtreeNodes=createTreeNodeList(list,ids);
for(TreeNodetreeNode:newtreeNodes){
if(treeNode.getId().equals(ids1)){
treeNode.setState("closed");
treeNode.setChildren(treeNodes);
break;
}
}
treeNodes=newtreeNodes;
}
}
for(TreeNodenode:rootTreeNodes){
if(node.getId().equals(strs[0])){
System.out.println();
if(length==0){
}else{
node.setState("open");
node.setChildren(treeNodes);
}
break;
}
}
ObjectMappermapper=newObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(
Inclusion.NON_NULL);
//设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
mapper.getDeserializationConfig()
.set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
StringjsonData=mapper.writeValueAsString(rootTreeNodes);
Struts2Utils.getResponse().getWriter().write(jsonData);
returnnull;
}
/**
*产生dataitem
*@paramcateEname
*@paramid
*@return
*/
publicList<DataItem>createItem(StringcateEname,Stringid){
returndataItemManager.getDataItemSubList(cateEname,id);
}
publicList<TreeNode>createTreeNodeList(List<DataItem>itemList,Stringids){
List<TreeNode>treeNodes=newArrayList<TreeNode>();
for(DataItemdataItem:itemList){
TreeNodetreeNode=newTreeNode();
if(ids==null){
treeNode.setId(dataItem.getItemValue());
}else{
treeNode.setId(ids+"-"+dataItem.getItemValue());
}
treeNode.setText(dataItem.getItemText());
StringsubFatherValue=dataItem.getItemValue();
List<DataItem>subList=createItem(cateEname,
subFatherValue);
if(subList!=null&&subList.size()>0){
treeNode.setState("closed");
}
treeNodes.add(treeNode);
}
returntreeNodes;
}
publicStringclosedTree()throwsException{
Stringids=Struts2Utils.getRequest().getParameter("id");
Stringid;
if(ids==null){
id="0";
}else{
id=ids.substring(ids.lastIndexOf("-")+1);
}
List<TreeNode>treeNodes=newArrayList<TreeNode>();
List<DataItem>list=dataItemManager.getDataItemSubList(cateEname,id);
for(DataItemdataItem:list){
TreeNodetreeNode=newTreeNode();
if(ids==null){
treeNode.setId(dataItem.getItemValue());
}else{
treeNode.setId(ids+"-"+dataItem.getItemValue());
}
treeNode.setText(dataItem.getItemText());
StringsubFatherValue=dataItem.getItemValue();
List<DataItem>subList=dataItemManager.getDataItemSubList(cateEname,
subFatherValue);
if(subList!=null&&subList.size()>0){
treeNode.setState("closed");
}
treeNodes.add(treeNode);
}
ObjectMappermapper=newObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(
Inclusion.NON_NULL);
//设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
mapper.getDeserializationConfig()
.set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
StringjsonData=mapper.writeValueAsString(treeNodes);
Struts2Utils.getResponse().getWriter().write(jsonData);
returnnull;
}
/**
*得到所有全部展开的树形下拉列表
*/
publicStringexecute(){
try{
List<TreeNode>treeNodes=newArrayList<TreeNode>();
List<DataItem>list=dataItemManager.getDataItemSubList(cateEname,
fatherValue);
for(DataItemdataItem:list){
TreeNodetreeNode=newTreeNode();
treeNode.setId(dataItem.getItemValue());
treeNode.setText(dataItem.getItemText());
//treeNode.setIconCls();
//treeNode.setChecked();
StringsubFatherValue=dataItem.getItemValue();
//List<TreeNode>children=newArrayList<TreeNode>();
//List<DataItem>subList=dataItemManager.getDataItemSubList(
//cateEname,subFatherValue);
//for(DataItemsubDataItem:subList){
//TreeNodesubTreeNode=newTreeNode();
//subTreeNode.setId(subDataItem.getItemValue());
//subTreeNode.setText(subDataItem.getItemValue()+""
//+subDataItem.getItemText());
//children.add(subTreeNode);
//}
//treeNode.setChildren(children);
if(isShowPrefix==null){
isShowPrefix=false;
}
this.recursiveCall(treeNode,cateEname,subFatherValue,
isShowPrefix);
treeNodes.add(treeNode);
}
ObjectMappermapper=newObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(
Inclusion.NON_NULL);
//设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
mapper.getDeserializationConfig()
.set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
StringjsonData=mapper.writeValueAsString(treeNodes);
Struts2Utils.getResponse().getWriter().write(jsonData);
}catch(IOExceptione){
e.printStackTrace();
}
returnnull;
}
/**
*
*@paramtreeNode
*@paramcateEname
*@paramfatherValue
*@paramisShowPrefix
*显示前缀
*/
publicvoidrecursiveCall(TreeNodetreeNode,StringcateEname,
StringfatherValue,BooleanisShowPrefix){
List<TreeNode>children=newArrayList<TreeNode>();
List<DataItem>subList=dataItemManager.getDataItemSubList(cateEname,
fatherValue);
for(DataItemsubDataItem:subList){
TreeNodesubTreeNode=newTreeNode();
subTreeNode.setId(subDataItem.getItemValue());
if(isShowPrefix){
subTreeNode.setText(subDataItem.getItemValue()+""
+subDataItem.getItemText());
}else{
subTreeNode.setText(subDataItem.getItemText());
}
children.add(subTreeNode);
recursiveCall(subTreeNode,cateEname,subDataItem.getItemValue(),
isShowPrefix);
}
treeNode.setChildren(children);
}
}