jquery easyui树的简答构造:
JSP页面
- 组织机构: <input id="p_organId" name="p_organId" style="width: 160px;height: 28px;">
- function loadOrgan(){
- organ_combotree = $("#p_organId").combotree({
- url:'${ctxFront}/cust/tree',
- multiple:false,//是否可多选
- editable:false,//是否可编辑
- required:false
- });
- }
后台代码:
- @SuppressWarnings("serial")
- public class TreeNode implements Serializable {
- /**
- * 静态变量 展开节点
- */
- public static final String STATE_OPEN = "open";
- /**
- * 静态变量 关闭节点
- */
- public static final String STATE_CLOASED = "closed";
- /**
- * 节点id
- */
- private String id;
- /**
- * 父级节点id (用于zTree简单数据模型)
- */
- private String pId;
- /**
- * 树节点名称
- */
- private String text;
- /**
- * 前面的小图标样式
- */
- private String iconCls = "";
- /**
- * 是否勾选状态(默认:否false)
- */
- private Boolean checked = false;
- /**
- * 自定义属性
- */
- private Map<String, Object> attributes = Maps.newHashMap();
- /**
- * 子节点
- */
- private List<TreeNode> children;
- /**
- * 是否展开 (open,closed)-(默认值:open)
- */
- private String state = STATE_OPEN;
- public TreeNode() {
- this(null, null, "");
- }
- /**
- *
- * @param id
- * 节点id
- * @param text
- * 树节点名称
- */
- public TreeNode(String id, String text) {
- this(id, text, "");
- }
- /**
- *
- * @param id
- * 节点id
- * @param text
- * 树节点名称
- * @param iconCls
- * 图标样式
- */
- public TreeNode(String id, String text, String iconCls) {
- this(id, text, STATE_OPEN, iconCls);
- }
- /**
- *
- * @param id
- * 节点id
- * @param text
- * 树节点名称
- * @param state
- * 是否展开
- * @param iconCls
- * 图标样式
- */
- public TreeNode(String id, String text, String state, String iconCls) {
- this.id = id;
- this.text = text;
- this.state = state;
- this.iconCls = iconCls;
- this.children = Lists.newArrayList();
- }
- /**
- *
- * @param id
- * 节点id
- * @param text
- * 树节点名称
- * @param iconCls
- * 图标样式
- * @param checked
- * 是否勾选状态(默认:否)
- * @param attributes
- * 自定义属性
- * @param children
- * 子节点
- * @param state
- * 是否展开
- */
- public TreeNode(String id, String text, String iconCls, Boolean checked,
- Map<String, Object> attributes, List<TreeNode> children,
- String state) {
- super();
- this.id = id;
- this.text = text;
- this.iconCls = iconCls;
- this.checked = checked;
- this.attributes = attributes;
- this.children = children;
- this.state = state;
- }
- /**
- * 添加子节点.
- *
- * @param childNode
- * 子节点
- */
- public TreeNode addChild(TreeNode childNode) {
- this.children.add(childNode);
- return this;
- }
- /**
- * 节点id
- */
- public String getId() {
- return id;
- }
- public TreeNode setId(String id) {
- this.id = id;
- return this;
- }
- /**
- * 父级节点id (用于zTree简单数据模型)
- * @return
- */
- public String getpId() {
- return pId;
- }
- /**
- * @param pId 父级节点id (用于zTree简单数据模型)
- * @return
- */
- public TreeNode setpId(String pId) {
- this.pId = pId;
- return this;
- }
- /**
- * 树节点名称
- */
- public String getText() {
- return text;
- }
- public TreeNode setText(String text) {
- this.text = text;
- return this;
- }
- /**
- * 是否勾选状态(默认:否)
- */
- public Boolean getChecked() {
- return checked;
- }
- public TreeNode setChecked(Boolean checked) {
- this.checked = checked;
- return this;
- }
- /**
- * 自定义属性
- */
- public Map<String, Object> getAttributes() {
- return attributes;
- }
- public TreeNode setAttributes(Map<String, Object> attributes) {
- this.attributes = attributes;
- return this;
- }
- /**
- * 子节点
- */
- public List<TreeNode> getChildren() {
- return children;
- }
- public TreeNode setChildren(List<TreeNode> children) {
- this.children = children;
- return this;
- }
- /**
- * 是否展开
- */
- public String getState() {
- return state;
- }
- public TreeNode setState(String state) {
- this.state = state;
- return this;
- }
- /**
- * 图标样式
- */
- public String getIconCls() {
- return iconCls;
- }
- public TreeNode setIconCls(String iconCls) {
- this.iconCls = iconCls;
- return this;
- }
- @Override
- public String toString() {
- return ToStringBuilder.reflectionToString(this);
- }
- }
@RequestMapping(value = {"tree"})
@ResponseBody
@DataSource(AppConstants.DATASOURCE_DW)//切换到datasource数据源
public List<TreeNode> tree() throws Exception {
List<TreeNode> treeNodes = Lists.newArrayList();
TreeNode proNode = new TreeNode("113600","江西省");
proNode.setChecked(false);
proNode.setIconCls("icon-organ-root");
proNode.setpId(null);
proNode.setState("closed");
List<Map> citylist=custService.findBySql("select distinct city_id,city_name from dim_organ_sale_procityxian where pro_id='113600' order by city_id", new Parameter());
List<TreeNode> citytree = Lists.newArrayList();
for(Map cityMap :citylist){
TreeNode cityNode = new TreeNode((String)cityMap.get("CITY_ID"),(String)cityMap.get("CITY_NAME"));
cityNode.setChecked(false);
cityNode.setIconCls("icon-organ-root");
cityNode.setpId(null);
cityNode.setState("closed");
List<Map> xialist=custService.findBySql("select distinct xia_id,xia_name from dim_organ_sale_procityxian where city_id='"+cityMap.get("CITY_ID")+"' order by xia_id", new Parameter());
List<TreeNode> xiatree = Lists.newArrayList();
for(Map xiaMap :xialist){
TreeNode xiaNode = new TreeNode((String)xiaMap.get("XIA_ID"),(String)xiaMap.get("XIA_NAME"));
xiaNode.setChecked(false);
xiaNode.setIconCls("icon-organ-root");
xiaNode.setpId(null);
xiaNode.setState("open");
xiatree.add(xiaNode);
}
cityNode.setChildren(xiatree);
citytree.add(cityNode);
}
proNode.setChildren(citytree);
treeNodes.add(proNode);
return treeNodes;
}
@ResponseBody
@DataSource(AppConstants.DATASOURCE_DW)//切换到datasource数据源
public List<TreeNode> tree() throws Exception {
List<TreeNode> treeNodes = Lists.newArrayList();
TreeNode proNode = new TreeNode("113600","江西省");
proNode.setChecked(false);
proNode.setIconCls("icon-organ-root");
proNode.setpId(null);
proNode.setState("closed");
List<Map> citylist=custService.findBySql("select distinct city_id,city_name from dim_organ_sale_procityxian where pro_id='113600' order by city_id", new Parameter());
List<TreeNode> citytree = Lists.newArrayList();
for(Map cityMap :citylist){
TreeNode cityNode = new TreeNode((String)cityMap.get("CITY_ID"),(String)cityMap.get("CITY_NAME"));
cityNode.setChecked(false);
cityNode.setIconCls("icon-organ-root");
cityNode.setpId(null);
cityNode.setState("closed");
List<Map> xialist=custService.findBySql("select distinct xia_id,xia_name from dim_organ_sale_procityxian where city_id='"+cityMap.get("CITY_ID")+"' order by xia_id", new Parameter());
List<TreeNode> xiatree = Lists.newArrayList();
for(Map xiaMap :xialist){
TreeNode xiaNode = new TreeNode((String)xiaMap.get("XIA_ID"),(String)xiaMap.get("XIA_NAME"));
xiaNode.setChecked(false);
xiaNode.setIconCls("icon-organ-root");
xiaNode.setpId(null);
xiaNode.setState("open");
xiatree.add(xiaNode);
}
cityNode.setChildren(xiatree);
citytree.add(cityNode);
}
proNode.setChildren(citytree);
treeNodes.add(proNode);
return treeNodes;
}
由于我的机构表没有父子关系,所以不能用递归的方法进行构造,只是一层层的手动构造,不过从这手动简单的构造可以学到要形成树形的数据结构
可以看出,要形成树有2个方法,1.json里面有children集合;2.json里面有_parentId。
相当于生成了var page0=1;var page1=1;var page2=1;var page3=1;
- [ {
- "id" : "113600",
- "pId" : null,
- "text" : "江西省",
- "iconCls" : "icon-organ-root",
- "checked" : false,
- "attributes" : { },
- "children" : [ {
- "id" : "113601",
- "pId" : null,
- "text" : "南昌市",
- "iconCls" : "icon-organ-root",
- "checked" : false,
- "attributes" : { },
- "children" : [ {
- "id" : "11360102",
- "pId" : null,
- "text" : "南昌县",
- "iconCls" : "icon-organ-root",
- "checked" : false,
- "attributes" : { },
- "children" : [ ],
- "state" : "open"
- }],
- "state" : "closed"
- },
- "state" : "closed"
- } ]
可以看出,要形成树有2个方法,1.json里面有children集合;2.json里面有_parentId。
动态生成js全局变量:
由于android项目中要不同Tab页进行分页,需要根据栏目的数量动态生成页码page变量存放页码。
- for(var i = 0; i < 4; i++) {
- window["page" + i] = 1;
- }
相当于生成了var page0=1;var page1=1;var page2=1;var page3=1;
在后面的使用中可以用使用,或者用window["page" + i] 当变量用。比如累加:window["page" + i] ++;或者赋值var page=window["page" + i]