一个关于jqGrid的treeGrid数据合成的问题

发布于 2022-01-05 05:26:39 字数 1600 浏览 845 评论 9

这是一个我在使用的前端表格控件,最近采用它支持的treeGrid:

我是用JSON作为数据交换方式,下面是一段产生的数据:

{
    "pageSize": 3,
    "pageNumber": 1,
    "list": [
        {
            "id": 200,
            "level": 0,
            "expended": true,
            "sortnum": 0,
            "name": "Super",
            "isleaf": false
            "parentid": null
        },
        {
            "id": 201,
            "level": 1,
            "expended": true,
            "sortnum": 1,
            "name": "Administrator",
            "isleaf": true,
            "parentid": 200
        },
        {
            "id": 202,
            "level": 0,
            "expended": true,
            "sortnum": 3,
            "name": "Users",
            "isleaf": false,
            "resource": [
            "parentid": null
        }
    ],
    "totalRow": 3,
    "totalPage": 1
}



其中list数组里面是要展示的对象,level表示显示的节点级别(右缩进),expended描述节点是否展开显示(忽略),isleaf这个参数表示当前节点是否是叶子(子节点)。

那么问题来了,数据库添加的数据不含这三个额外的属性,而且parentid可以设置成多级,程序调取数据之后,需要合成这些数据。想了很久都没实现,有没有比较好的算法呢,请高手指教。

一开始我是遍历结果list,然后role.findParent()的,可是,多级别,

while( parent != null && !parent.equals(role) ) {

    parent = parent.findParent();  // dao

    level++;

    parent.setIsLeaf(false);

}

给我死循环了呵呵。


给点指导性思路吧。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

成熟稳重的好男人 2022-01-08 02:54:13

引用来自“mark35”的评论

还是在数据库中保存相应值比较的好。

nested格式主要是方便数据库查询:判断左右值一条SQL就可以把所有子节点都查出来,而无须递归查询

怎言笑 2022-01-08 02:44:55

考虑这种数据是动态维护的,如果页面显示的额外属性也放在表里面,感觉维护变难了。

温柔少女心 2022-01-08 02:44:15

引用来自“mark35”的评论

还是在数据库中保存相应值比较的好。

nested格式主要是方便数据库查询:判断左右值一条SQL就可以把所有子节点都查出来,而无须递归查询

多彩岁月 2022-01-08 02:39:53

算法我已经实现了:欢迎吐槽!封装一下做一个工具类,实体继承一个Node接口即可。

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestTreeGenera {

	private static final List<Node> list = new ArrayList<Node>();
	
	static {
		Node[] datas1 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", 1),
				new Node(3, "节点3", 1),
				new Node(4, "节点4", null),
				new Node(5, "节点5", 4),
				new Node(6, "节点6", null)
		};
		Node[] datas2 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", 1),
				new Node(3, "节点3", 1),
				new Node(4, "节点4", 2),
				new Node(5, "节点5", 4),
				new Node(6, "节点6", null)
		};
		Node[] datas3 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", null),
				new Node(3, "节点3", null),
				new Node(4, "节点4", null),
				new Node(5, "节点5", null),
				new Node(6, "节点6", null)
		};
		Node[] datas4 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", 1),
				new Node(3, "节点3", 2),
				new Node(4, "节点4", 3),
				new Node(5, "节点5", 4),
				new Node(6, "节点6", 5)
		};
		Node[] datas5 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", 1),
				new Node(3, "节点3", 2),
				new Node(4, "节点4", 3),
				new Node(5, "节点5", 4),
				new Node(6, "节点6", 5),
				new Node(7, "节点7", 5),
				new Node(8, "节点8", 5),
				new Node(9, "节点9", 5),
				new Node(10, "节点10", 5),
				new Node(11, "节点11", 5),
				new Node(12, "节点12", 5),
				new Node(13, "节点13", 5),
				new Node(14, "节点14", 5),
				new Node(15, "节点15", 5)
		};
		list.addAll(Arrays.asList(datas5));
	}
	
	public static void main(String[] args) {
//		List<Node> roots = findRoots();
//		for(Node rootNode : roots) {
//			System.out.println(rootNode);
//			List<Node> childs = findChildAtNode(rootNode);
//			for(Node childNode : childs) {
//				System.out.println("t" + childNode);
//			}
//		}
		List<Node> roots = findRoots();
		for(Node rootNode : roots) {
			deepSearchChildNodes(rootNode, list);
		}
		System.out.println("finish...");
		for(Node n : list) {
			System.out.println(n);
		}
	}
	
	public static void deepSearchChildNodes(Node parent, List<Node> nodes) {
		//for(Node child : nodes) {
			parent.isLeaf = false;
			System.out.println(parent);
			List<Node> findChildAtNode = findChildAtNode(parent);
			if(findChildAtNode.size() == 0) {
				parent.isLeaf = true;
				return;
			} else {
				for(Node n : findChildAtNode) {
					n.level = parent.level + 1;
				}
			}
			
			deepSearchChildNodes( nodes.get(0), findChildAtNode);
		//}
	}
	
	public static List<Node> findRoots() {
		List<Node> roots = new ArrayList<>();
		for(Node n : list) {
			if(n.parent == null) 
				roots.add(n);
		}
		return roots;
	}
	
	public static List<Node> findChildAtNode(Node currentNode) {
		List<Node> childs = new ArrayList<>();
		for(Node n : list) {
			if(n == currentNode) continue;
			if(Integer.valueOf(currentNode.id).equals(n.parent)/*n.parent == n.id*/) {
				childs.add(n);
			}
		}
		return childs;
	}
	
	
	public static class Node {
		
		public int id;
		public String name;
		public Integer parent;
		
		public int level;
		public boolean isLeaf = true;
		
		public Node(int id, String name, Integer parent) {
			super();
			this.id = id;
			this.name = name;
			this.parent = parent;
		}

		@Override
		public String toString() {
			return String.format(
					"Node [id=%s, name=%s, parent=%s, level=%s, isLeaf=%s]",
					id, name, parent, level, isLeaf);
		}
		
	}

}

控制台结果:

Node [id=1, name=节点1, parent=null, level=0, isLeaf=false]
Node [id=2, name=节点2, parent=1, level=1, isLeaf=false]
Node [id=3, name=节点3, parent=2, level=2, isLeaf=false]
Node [id=4, name=节点4, parent=3, level=3, isLeaf=false]
Node [id=5, name=节点5, parent=4, level=4, isLeaf=false]
Node [id=6, name=节点6, parent=5, level=5, isLeaf=true]
Node [id=7, name=节点7, parent=5, level=5, isLeaf=true]
Node [id=8, name=节点8, parent=5, level=5, isLeaf=true]
Node [id=9, name=节点9, parent=5, level=5, isLeaf=true]
Node [id=10, name=节点10, parent=5, level=5, isLeaf=true]
Node [id=11, name=节点11, parent=5, level=5, isLeaf=true]
Node [id=12, name=节点12, parent=5, level=5, isLeaf=true]
Node [id=13, name=节点13, parent=5, level=5, isLeaf=true]
Node [id=14, name=节点14, parent=5, level=5, isLeaf=true]
Node [id=15, name=节点15, parent=5, level=5, isLeaf=true]

算法比较丑陋。

海之角 2022-01-08 02:35:25
var grid = jQuery("#system-role-grid").jqGrid({
 	treeGrid: true,
    treeGridModel: 'adjacency', //'Nested'|'adjacency', //treeGrid模式,跟json元数据有关
    ExpandColumn : 'name',
    scroll: "true",
    url: "systemrole.txt", // TODO Test系统角色数据接口地址
    datatype: 'json',
    colNames:['角色名称','排序','上级角色','拥有资源','备注','创建时间', 'id', 'parentid'],
    colModel:[
        {name:'name',index:'name', width:120,sortable:false},
        {name:'sortnum',index:'sortnum', width:80, align:'center', sortable:false},
        {name:'parent',index:'parent', width:120,sortable:false},
        {name:'resource',index:'resource', width:300, sortable:false},
        {name:'comment',index:'comment', width:150, sortable:false},
        {name:'createtime',index:'createtime', type:'date', width:120,sortable:false},
        {name:'id',index:'id', hidden:true},
        {name:'parentid',index:'parentid', hidden:true}
     ],
    pager: "false",
    sortname: 'id',
    sortorder: "desc",
    jsonReader: {
    	page: "pageNumber",
   		records: "totalRow",
   		repeatitems: false,
   		root: "list",
   		total: "totalPage"
    },
    treeReader : {
      level_field: "level",
   	  parent_id_field: "parentid",
   	  leaf_field: "isleaf",
   	  expanded_field: "expanded"
	},
    caption: "角色管理",
    mtype: "POST",
    height: "200",
    rowNum : "-1",
    shrinkToFit:false  // 控制水平滚动条
});

终遇你 2022-01-07 23:48:52

jqGrid TreeGrid 加载数据 排序 扩展 https://www.cnblogs.com/cl-blogs/p/10829279.html

空城仅有旧梦在 2022-01-07 03:49:09

还是在数据库中保存相应值比较的好。

nested格式主要是方便数据库查询:判断左右值一条SQL就可以把所有子节点都查出来,而无须递归查询

甜扑 2022-01-05 23:52:27

引用来自“Twisst”的评论

算法我已经实现了:欢迎吐槽!封装一下做一个工具类,实体继承一个Node接口即可。

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestTreeGenera {

	private static final List<Node> list = new ArrayList<Node>();
	
	static {
		Node[] datas1 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", 1),
				new Node(3, "节点3", 1),
				new Node(4, "节点4", null),
				new Node(5, "节点5", 4),
				new Node(6, "节点6", null)
		};
		Node[] datas2 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", 1),
				new Node(3, "节点3", 1),
				new Node(4, "节点4", 2),
				new Node(5, "节点5", 4),
				new Node(6, "节点6", null)
		};
		Node[] datas3 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", null),
				new Node(3, "节点3", null),
				new Node(4, "节点4", null),
				new Node(5, "节点5", null),
				new Node(6, "节点6", null)
		};
		Node[] datas4 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", 1),
				new Node(3, "节点3", 2),
				new Node(4, "节点4", 3),
				new Node(5, "节点5", 4),
				new Node(6, "节点6", 5)
		};
		Node[] datas5 = {
				new Node(1, "节点1", null),
				new Node(2, "节点2", 1),
				new Node(3, "节点3", 2),
				new Node(4, "节点4", 3),
				new Node(5, "节点5", 4),
				new Node(6, "节点6", 5),
				new Node(7, "节点7", 5),
				new Node(8, "节点8", 5),
				new Node(9, "节点9", 5),
				new Node(10, "节点10", 5),
				new Node(11, "节点11", 5),
				new Node(12, "节点12", 5),
				new Node(13, "节点13", 5),
				new Node(14, "节点14", 5),
				new Node(15, "节点15", 5)
		};
		list.addAll(Arrays.asList(datas5));
	}
	
	public static void main(String[] args) {
//		List<Node> roots = findRoots();
//		for(Node rootNode : roots) {
//			System.out.println(rootNode);
//			List<Node> childs = findChildAtNode(rootNode);
//			for(Node childNode : childs) {
//				System.out.println("t" + childNode);
//			}
//		}
		List<Node> roots = findRoots();
		for(Node rootNode : roots) {
			deepSearchChildNodes(rootNode, list);
		}
		System.out.println("finish...");
		for(Node n : list) {
			System.out.println(n);
		}
	}
	
	public static void deepSearchChildNodes(Node parent, List<Node> nodes) {
		//for(Node child : nodes) {
			parent.isLeaf = false;
			System.out.println(parent);
			List<Node> findChildAtNode = findChildAtNode(parent);
			if(findChildAtNode.size() == 0) {
				parent.isLeaf = true;
				return;
			} else {
				for(Node n : findChildAtNode) {
					n.level = parent.level + 1;
				}
			}
			
			deepSearchChildNodes( nodes.get(0), findChildAtNode);
		//}
	}
	
	public static List<Node> findRoots() {
		List<Node> roots = new ArrayList<>();
		for(Node n : list) {
			if(n.parent == null) 
				roots.add(n);
		}
		return roots;
	}
	
	public static List<Node> findChildAtNode(Node currentNode) {
		List<Node> childs = new ArrayList<>();
		for(Node n : list) {
			if(n == currentNode) continue;
			if(Integer.valueOf(currentNode.id).equals(n.parent)/*n.parent == n.id*/) {
				childs.add(n);
			}
		}
		return childs;
	}
	
	
	public static class Node {
		
		public int id;
		public String name;
		public Integer parent;
		
		public int level;
		public boolean isLeaf = true;
		
		public Node(int id, String name, Integer parent) {
			super();
			this.id = id;
			this.name = name;
			this.parent = parent;
		}

		@Override
		public String toString() {
			return String.format(
					"Node [id=%s, name=%s, parent=%s, level=%s, isLeaf=%s]",
					id, name, parent, level, isLeaf);
		}
		
	}

}

控制台结果:

Node [id=1, name=节点1, parent=null, level=0, isLeaf=false]
Node [id=2, name=节点2, parent=1, level=1, isLeaf=false]
Node [id=3, name=节点3, parent=2, level=2, isLeaf=false]
Node [id=4, name=节点4, parent=3, level=3, isLeaf=false]
Node [id=5, name=节点5, parent=4, level=4, isLeaf=false]
Node [id=6, name=节点6, parent=5, level=5, isLeaf=true]
Node [id=7, name=节点7, parent=5, level=5, isLeaf=true]
Node [id=8, name=节点8, parent=5, level=5, isLeaf=true]
Node [id=9, name=节点9, parent=5, level=5, isLeaf=true]
Node [id=10, name=节点10, parent=5, level=5, isLeaf=true]
Node [id=11, name=节点11, parent=5, level=5, isLeaf=true]
Node [id=12, name=节点12, parent=5, level=5, isLeaf=true]
Node [id=13, name=节点13, parent=5, level=5, isLeaf=true]
Node [id=14, name=节点14, parent=5, level=5, isLeaf=true]
Node [id=15, name=节点15, parent=5, level=5, isLeaf=true]

算法比较丑陋。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文