- 论坛徽章:
- 1
|
适用于javaWeb结合easyui使用
easyui的Tree节点JSON格式- [{
- "id":1,
- "text":"Folder1",
- "iconCls":"icon-save",
- "children":[{
- "text":"File1",
- "checked":true
- },{
- "text":"Books",
- "state":"open",
- "attributes":{
- "url":"/demo/book/abc",
- "price":100
- },
- "children":[{
- "text":"PhotoShop",
- "checked":true
- },{
- "id": 8,
- "text":"Sub Bookds",
- "state":"closed"
- }]
- }]
- },{
- "text":"Languages",
- "state":"closed",
- "children":[{
- "text":"Java"
- },{
- "text":"C#"
- }]
- }]
复制代码- /**
- * 树 json model 数据
- * @author glq
- *
- */
- public class JsonTreeData {
-
- public String id; //json id
- public String pid; //
- public String text; //json 显示文本
- public String state; //json 'open','closed'
- public List<JsonTreeData> children; //
-
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- public String getState() {
- return state;
- }
- public void setState(String state) {
- this.state = state;
- }
- public List<JsonTreeData> getChildren() {
- return children;
- }
- public void setChildren(List<JsonTreeData> children) {
- this.children = children;
- }
- public String getPid() {
- return pid;
- }
- public void setPid(String pid) {
- this.pid = pid;
- }
- }
复制代码- /**
- * @ClassName: TreeNodeUtil
- * @Description 描述: 获取树节点集合(这里用一句话描述这个类的作用)
- * @author: 青柠loft
- * @date 最后修改时间: 2015年6月9日 下午6:39:28
- *
- */
- public class TreeNodeUtil {
-
- /**
- * @Title: getfatherNode
- * @Description 方法描述: 父节点
- * @param 设定文件: @param treeDataList
- * @param 设定文件: @return
- * @return 返回类型:List<JsonTreeData>
- * @throws
- * @date 最后修改时间:2015年6月9日 下午6:39:26
- */
- public final static List<JsonTreeData> getfatherNode(List<JsonTreeData> treeDataList) {
- List<JsonTreeData> newTreeDataList = new ArrayList<JsonTreeData>();
- for (JsonTreeData jsonTreeData : treeDataList) {
- if(jsonTreeData.getPid() == null) {
- //获取父节点下的子节点
- jsonTreeData.setChildren(getChildrenNode(jsonTreeData.getId(),treeDataList));
- jsonTreeData.setState("open");
- newTreeDataList.add(jsonTreeData);
- }
- }
- return newTreeDataList;
- }
-
- /**
- * @Title: getChildrenNode
- * @Description 方法描述: 子节点
- * @param 设定文件: @param pid
- * @param 设定文件: @param treeDataList
- * @param 设定文件: @return
- * @return 返回类型:List<JsonTreeData>
- * @throws
- * @date 最后修改时间:2015年6月9日 下午6:39:50
- */
- private final static List<JsonTreeData> getChildrenNode(String pid , List<JsonTreeData> treeDataList) {
- List<JsonTreeData> newTreeDataList = new ArrayList<JsonTreeData>();
- for (JsonTreeData jsonTreeData : treeDataList) {
- if(jsonTreeData.getPid() == null) continue;
- //这是一个子节点
- if(jsonTreeData.getPid().equals(pid)){
- //递归获取子节点下的子节点
- jsonTreeData.setChildren(getChildrenNode(jsonTreeData.getId() , treeDataList));
- newTreeDataList.add(jsonTreeData);
- }
- }
- return newTreeDataList;
- }
- }
复制代码- /*
- *serviceImpl 业务实现
- */
- public List<JsonTreeData> getGoodsSpec(HtSpecifications spec) {
- List<HtSpecifications> resultList = htSpecificationsDaoImpl.findAll("getSpecList", spec);
- List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
- /*为了整理成公用的方法,所以将查询结果进行二次转换。
- * 其中specid为主键ID,varchar类型UUID生成
- * parentid为父ID
- * specname为节点名称
- * */
- for (HtSpecifications htSpecifications : resultList) {
- JsonTreeData treeData = new JsonTreeData();
- treeData.setId(htSpecifications.getSpecid());
- treeData.setPid(htSpecifications.getParentid());
- treeData.setText(htSpecifications.getSpecname());
- treeDataList.add(treeData);
- }
- //最后得到结果集,经过FirstJSON转换后就可得所需的json格式
- List<JsonTreeData> newTreeDataList = TreeNodeUtil.getfatherNode(treeDataList);
- return newTreeDataList;
- }
复制代码 |
|