js easyUI treegrid 递归构造树

js:


function loadDataTree() {


$('#tg').treegrid(
{
checkbox : false,
lines : true,
method : 'post',
url : 'platform/cmtModuleManager/SectionBoardViewController/getCmtProductTreeData.json',
dataType : "json",
loadFilter : function(data) {
if (data.data) {
return data.data;
} else {
return data;
}
},
onClickRow : function(node) {
if(null!=node && "null"!=node && "undefined"!=node){
loadproductdimensionality(node.ID);
}},
onLoadSuccess : function(node){
var root = $('#tg').treegrid('getRoot');
if(null!=root && ""!=root && "undefined"!=root){
loadproductdimensionality(root.ID);
}
}
});
}
//
/**
 *查询
**/
function initForSearch(){
 $('#formSearch').searchbox({
        width: 200,
        searcher: function (value) {
        if(value==null||value==""){
$("#tg").treegrid("options").url = "platform/cmtModuleManager/SectionBoardViewController/getCmtProductTreeData.json";
$('#tg').treegrid('load',{});
        }else{
$("#tg").treegrid("options").url = "platform/cmtModuleManager/SectionBoardViewController/searchTreeGrid.json";
$('#tg').treegrid('load', {
search_productName: value
});
        }
        },
        prompt: "请输入查询条件!"
    });
}


-----------------------------------------------------------------------------------------------------------------------------------------------

jsp:

<table id="tg" class="easyui-treegrid"
data-options="rownumbers: true,
animate: true,
collapsible: false,
fitColumns: true,
autoRowHeight: false,
url: '',
fit:true,
toolbar:'#toolbar',
method: 'post',
idField: 'ID',
treeField: 'PRODUCT_NAME'
">
<thead>
<tr>
<th data-options="field:'PRODUCT_NAME'" width="30%">产品名称</th>
<th data-options="field:'deptName'" width="30%">责任部门</th>
<th data-options="field:'nodeTypeName'" width="40%">类 别</th>
</tr>
</thead>
</table>

---------------------------------------------------------------------------------------------------------------------------------------------------------

ACTION层

  @RequestMapping("/getCmtProductTreeData")
  public ModelAndView getCmtProductTreeData(String id,HttpServletResponse response) throws Exception{

ModelAndView mv = new ModelAndView();

List<Map<String,Object>> nodeList=new ArrayList<Map<String,Object>>();

try{

int level=1;

//默认为根节点
if(StringUtils.isEmpty(id)){
id="-1";
}

//第一次加载展开2层
if("-1".equals(id)){
level=2;
}

nodeList=this.cmtBoardTreeGridService.recurFindTreeGridNodesByParentId(id,level);

mv.addObject("result",OpResult.success.ordinal());
}catch(Exception e){
logger.error(e.getMessage(),e);
mv.addObject("result",OpResult.failure.ordinal());
mv.addObject("msg","后台处理异常,请联系管理员!");
}
mv.addObject("rows", nodeList); // rows键 存放每页记录 list
return mv;
}

------------------------------------------------------------------------------------------------------------------

实现

public List<Map<String, Object>> recurFindTreeGridNodesByParentId(
String id, int level) {
    //递归一层减一层
  --level;
  
  List<Map<String,Object>> nodes=new ArrayList<Map<String,Object>>();
 
  //查询业务数据
  List<Map<String,Object>>  childTreeList=findNodesByParentId(id);
  
  //将业务数据格式转换成esayui树形格式,因为treeGrid不支持attibute属性,此处使用map组装
  for(Map<String,Object> node:childTreeList){
  String rid=(String)node.get("ID");
  String rname=(String)node.get("PRODUCT_NAME");
  
  if(!"-1".equals(id)){
 node.put("_parentId",id);
  }
  /***处理图标****/
   String nodeType = (String) node.get("NODE_TYPE");
String iconCls = setProductIcon(nodeType);
if(node.get("P_ID").equals("-1")){
iconCls="nodeType00";
}
node.put("iconCls","tree-"+iconCls);
   
/******处理部门********/
String deptName=this.getDeptNameByDeptId((String)node.get("RESPONSIBLE_DEPT"));
node.put("deptName", deptName);
/******处理通用代码节点类型*****/
String nodeTypeName=this.getSysLookupNameByLookUpCode_(isNodeType, nodeType);
node.put("nodeTypeName", nodeTypeName);
  BigDecimal childCount=(BigDecimal)node.get("child_count");
  if(null!=childCount&&childCount.longValue()>0){
  if(level>0){
 List<Map<String,Object>> cnodes=recurFindTreeGridNodesByParentId(rid,level);
 nodes.addAll(cnodes);
 node.put("state","open");
  }else{
 node.put("state","closed");
  }
  }else{
 node.put("state","open");
  }
  nodes.add(node);
  }
  return nodes;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值