前台:
$('#source').combotree({
url:'${ctx}/dict/pub/dict-tree-box!closedTree2.action?cateEname=source&abc=${carTask.assetsSrcEn}',
onBeforeLoad:function(node){
if(node){
var tree = $('#source').combotree('tree');
$(tree).tree('options').url='${ctx}/dict/pub/dict-tree-box!closedTree.action?cateEname=source&id='+node.id;
}
},
onBeforeSelect:function(node){
//此处用于模拟单选时文件夹不能选择,主要通过后台数据attributes属性来标识
var t = $('#source').combotree('tree');
if(t.tree('isLeaf',node.target)){
return true ;
}else {
window.setTimeout('source()',0);
return false ;
}
},
onChange:function(newValue, oldValue){
},
onSelect:function(node){
},
value:"${carTask.assetsSrcEn}",
panelHeight:400
});
<select id="source" name="source" style="width:295px;" class="easyui-validatebox" validType="selectValid['--请选择--']">
</select>
后台:@Namespace("/dict/pub")
@Results({})
public class DictTreeBoxAction extends ActionSupport {
private static final long serialVersionUID = 7945924152310819279L;
@Autowired
private DataItemManager dataItemManager;
private String cateEname;
private String fatherValue;
private Boolean isShowPrefix;
private String abc;
public Boolean getIsShowPrefix() {
return isShowPrefix;
}
public void setIsShowPrefix(Boolean isShowPrefix) {
this.isShowPrefix = isShowPrefix;
}
public String getCateEname() {
return cateEname;
}
public void setCateEname(String cateEname) {
this.cateEname = cateEname;
}
public String getFatherValue() {
return fatherValue;
}
public void setFatherValue(String fatherValue) {
this.fatherValue = fatherValue;
}
public String getAbc() {
return abc;
}
public void setAbc(String abc) {
this.abc = abc;
}
public String createIds(String[] strs ,int i){
String str = "";
for(int j=0;j<i;j++){
if(j!=i-1){
str += strs[j]+"-";
}else{
str += strs[j];
}
}
return str ;
}
public String closedTree2() throws Exception {
String[] strs = abc.split("-");
int length = strs.length-1;
List<DataItem> rootList = createItem(cateEname,"0");
List<TreeNode> rootTreeNodes = createTreeNodeList(rootList,null);
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
for(int i= length ; i>0;i-- ){
if(i==length){
List<DataItem> list = createItem(cateEname,strs[i-1]);
String ids = createIds(strs,i);
treeNodes = createTreeNodeList(list,ids);
}else{
List<DataItem> list = createItem(cateEname,strs[i-1]);
String ids = createIds(strs,i);
String ids1 = createIds(strs,i+1);
List<TreeNode> newtreeNodes =createTreeNodeList(list,ids);
for(TreeNode treeNode: newtreeNodes){
if(treeNode.getId().equals(ids1)){
treeNode.setState("closed");
treeNode.setChildren(treeNodes);
break;
}
}
treeNodes = newtreeNodes;
}
}
for(TreeNode node : rootTreeNodes){
if(node.getId().equals(strs[0])){
System.out.println();
if(length==0){
}else{
node.setState("open");
node.setChildren(treeNodes);
}
break;
}
}
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(
Inclusion.NON_NULL);
// 设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
mapper.getDeserializationConfig()
.set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
String jsonData = mapper.writeValueAsString(rootTreeNodes);
Struts2Utils.getResponse().getWriter().write(jsonData);
return null;
}
public List<DataItem> createItem(String cateEname,String id){
return dataItemManager.getDataItemSubList(cateEname,id) ;
}
public List<TreeNode> createTreeNodeList(List<DataItem> itemList,String ids){
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
for (DataItem dataItem : itemList) {
TreeNode treeNode = new TreeNode();
if(ids == null){
treeNode.setId(dataItem.getItemValue());
}else{
treeNode.setId(ids+"-"+dataItem.getItemValue());
}
treeNode.setText(dataItem.getItemText());
String subFatherValue = dataItem.getItemValue();
List<DataItem> subList = createItem(cateEname,
subFatherValue);
if(subList!=null&&subList.size()>0){
treeNode.setState("closed");
}
treeNodes.add(treeNode);
}
return treeNodes;
}
public String closedTree() throws Exception {
String ids = Struts2Utils.getRequest().getParameter("id");
String id;
if(ids == null){
id = "0";
}else{
id = ids.substring(ids.lastIndexOf("-")+1);
}
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
List<DataItem> list = dataItemManager.getDataItemSubList(cateEname,id);
for (DataItem dataItem : list) {
TreeNode treeNode = new TreeNode();
if(ids == null){
treeNode.setId(dataItem.getItemValue());
}else{
treeNode.setId(ids+"-"+dataItem.getItemValue());
}
treeNode.setText(dataItem.getItemText());
String subFatherValue = dataItem.getItemValue();
List<DataItem> subList = dataItemManager.getDataItemSubList(cateEname,
subFatherValue);
if(subList!=null&&subList.size()>0){
treeNode.setState("closed");
}
treeNodes.add(treeNode);
}
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(
Inclusion.NON_NULL);
// 设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
mapper.getDeserializationConfig()
.set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
String jsonData = mapper.writeValueAsString(treeNodes);
Struts2Utils.getResponse().getWriter().write(jsonData);
return null;
}
public String execute() {
try {
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
List<DataItem> list = dataItemManager.getDataItemSubList(cateEname,
fatherValue);
for (DataItem dataItem : list) {
TreeNode treeNode = new TreeNode();
treeNode.setId(dataItem.getItemValue());
treeNode.setText(dataItem.getItemText());
// treeNode.setIconCls();
// treeNode.setChecked();
String subFatherValue = dataItem.getItemValue();
// List<TreeNode> children = new ArrayList<TreeNode>();
// List<DataItem> subList = dataItemManager.getDataItemSubList(
// cateEname, subFatherValue);
// for (DataItem subDataItem : subList) {
// TreeNode subTreeNode = new TreeNode();
// 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);
}
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(
Inclusion.NON_NULL);
// 设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
mapper.getDeserializationConfig()
.set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
String jsonData = mapper.writeValueAsString(treeNodes);
Struts2Utils.getResponse().getWriter().write(jsonData);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void recursiveCall(TreeNode treeNode, String cateEname,
String fatherValue, Boolean isShowPrefix) {
List<TreeNode> children = new ArrayList<TreeNode>();
List<DataItem> subList = dataItemManager.getDataItemSubList(cateEname,
fatherValue);
for (DataItem subDataItem : subList) {
TreeNode subTreeNode = new TreeNode();
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);
}
}