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;
}