JEECG树(TreeGrid)字段的扩展

本文介绍了如何在JEECG集成框架中扩展TreeGrid的字段。默认情况下,树列字段只显示两列,但通过利用框架预留的扩展方法,可以在后台添加attribute属性,并在前台读取显示额外信息。具体实现包括后台代码的修改,添加attribute字段,以及前台JSP代码的两种写法展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前使用jeecg集成框架做过一个项目

这里说说框架中树(tree)字段的扩展,在jeecg集成框架中,树列字段默认只能显示两列,如下图所示:



通常情况下前后台代码格式如下所示

前台代码获取(部分)

<t:datagrid name="departList" title="部门列表" actionUrl="departController.do?departgrid" treegrid="true" idField="departid" pagination="false" onClick="queryUsersByRowData">
 <t:dgCol title="编号" field="id" treefield="id" hidden="false"></t:dgCol>
 <t:dgCol title="部门名称" field="departname" treefield="text"  ></t:dgCol>
 <t:dgCol title="职能描述" field="description" treefield="src"></t:dgCol>
 <t:dgCol title="操作" field="opt"></t:dgCol>
 <t:dgDelOpt url="departController.do?del&id={id}" title="删除"></t:dgDelOpt>
 <t:dgFunOpt funname="queryUsersByDepart(id)" title="查看成员"></t:dgFunOpt>
</t:datagrid>


后台代码(部分)

	/**
	 * 部门列表,树形展示
	 * @param request
	 * @param response
	 * @param treegrid
	 * @return
	 */
	@RequestMapping(params = "departgrid")
	@ResponseBody
	public  List<TreeGrid> departgrid(TSDepart tSDepart,HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		CriteriaQuery cq = new CriteriaQuery(TSDepart.class);
		if("yes".equals(request.getParameter("isSearch"))){
			treegrid.setId(null);
			tSDepart.setId(null);
		} 
		if(null != tSDepart.getDepartname()){
			org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tSDepart);
		}
		if (treegrid.getId() != null) {
			cq.eq("TSPDepart.id", Integer.parseInt(treegrid.getId()));
		}
		if (treegrid.getId() == null) {
			cq.isNull("TSPDepart.id");
		}
		cq.add();
		List<TreeGrid> departList =null;
		departList = systemService.getListByCriteriaQuery(cq, false);
		if(departList.size() == 0 && tSDepart.getDepartname() != null){ 
			cq = new CriteriaQuery(TSDepart.class);
			TSDepart parDepart = new TSDepart();
			tSDepart.setTSPDepart(parDepart);
			org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tSDepart);
		    departList = systemService.getListByCriteriaQuery(cq, false);
		}
		List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
		TreeGridModel treeGridModel = new TreeGridModel();
		treeGridModel.setTextField("departname");
		treeGridModel.setParentText("TSPDepart_departname");
		treeGridModel.setParentId("TSPDepart_id");
		treeGridModel.setSrc("description");
		treeGridModel.setIdField("id");
		treeGridModel.setChildList("TSDeparts");
		treeGrids = systemService.treegrid(departList, treeGridModel);
		return treeGrids;
	}

以上是没有是没有扩展树字段的实例,以下是扩展的实例

!扩展方法

其实扩展方法很简单,因为 作者已经帮我们留了扩展的方法了。捕捉树列表响应的数据如下(部分响应数据):

[
  {
    "operations": null,
    "note": null,
    "order": null,
    "src": "",
    "parentId": "",
    "parentText": "中联总部",
    "id": "1",
    "state": "closed",
    "attributes": null,
    "text": "中联总部",
    "code": null
  },
  {
    "operations": null,
    "note": null,
    "order": null,
    "src": "",
    "parentId": "",
    "parentText": "混凝土机械国际管理公司",
    "id": "2",
    "state": "closed",
    "attributes": null,
    "text": "混凝土机械国际管理公司",
    "code": null
  },
  {
    "operations": null,
    "note": null,
    "order": null,
    "src": "",
    "parentId": "",
    "parentText": "工程起重机公司",
    "id": "5",
    "state": "closed",
    "attributes": null,
    "text": "工程起重机公司",
    "code": null
  },
  {
    "operations": null,
    "note": null,
    "order": null,
    "src": "",
    "parentId": "",
    "parentText": "建筑起重机公司",
    "id": "6",
    "state": "closed",
    "attributes": null,
    "text": "建筑起重机公司",
    "code": null
  }
]

以上数据可以看出在每条返回的数据中存在一个attribute字段,但都是空值。其实这个就是可以扩展的字段attribute是一个HashMap


即扩展树字段的方法原理就是后台添加attribute并把需要在前台显示的值存进去,前台取attribute的值。代码如下

后台代码(因为没有扩展上面机构tree代码,我挑选了其他树扩展的代码)


        Map<String , String> map;
		for (int i = 0; i < treeGrids.size(); i++) {
			map = new HashMap<String, String>();
			map.put("description", assetDepreciationList.get(i).getDescription());
			treeGrids.get(i).setAttributes(map);
		}
把需要扩展显示的字段put进去,上面我增加了一个显示字段,如果有多个同样put多个进去。

前台jsp代码

        <t:datagrid name="assetDepreciationList" title="折旧类型管理" actionUrl="assetDepreciationController.do?assetDepreciationGrid" idField="id" treegrid="true" pagination="false" fit="true">
            <t:dgCol title="编号" field="id" treefield="id" hidden="false"></t:dgCol>
            <t:dgCol title="折旧计算方法" field="depreciationWay" treefield="text"></t:dgCol>
            <t:dgCol title="折算周期" field="depreciationCycle" treefield="src"></t:dgCol>
            <t:dgCol title="方法描述" field="attributes.description" treefield="attributes.description" extendParams="formatter:function(value, rec, index){return rec.attributes.description};" width="200"></t:dgCol>
            <t:dgCol title="操作" field="opt" width="100"></t:dgCol>
            <t:dgDelOpt title="删除" url="assetDepreciationController.do?del&id={id}"/>
            <t:dgToolBar title="录入" icon="icon-add" url="assetDepreciationController.do?addorupdate" funname="add"></t:dgToolBar>
            <t:dgToolBar title="编辑" icon="icon-edit" url="assetDepreciationController.do?addorupdate" funname="update"></t:dgToolBar>
            <t:dgToolBar title="查看" icon="icon-search" url="assetDepreciationController.do?addorupdate" funname="detail"></t:dgToolBar>
        </t:datagrid>

方法描述即为扩展的显示列(写法如下)

写法一

<t:dgCol title="方法描述" field="attributes.description" treefield="attributes.description" extendParams="formatter:function(value, rec, index){return rec.attributes.description};" width="200"></t:dgCol>


或者是将js方法写出来

写法二

<t:dgCol title="方法描述" field="attributes.description" treefield="attributes.description" extendParams="formatter:getDescription;" width="200"></t:dgCol>
	function getDescription(value, rec, index) {
	    return rec.attributes.descripition;
	}
以上就是jeecg中树的列显示字段扩展,如果有多个需要扩展就根据以上格式编写多个即可。









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值