Ext.tree.panel 后端动态加载json数据

首先建一个实体类,如:

package ***.bean;

import java.io.Serializable;
import java.util.List;

public class Competence implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private int roleID;
private int parentRoleID;//如果是自己生成id,就设成字符串形式,有利于拼凑
private String parentRoleName;
private String roleName;
private String createTime;
private String description;
private String useFlag;//角色是否在使用
private boolean leaf;
private boolean expanded;
private List<Competence> children;
public int getRoleID() {
return roleID;
}
public void setRoleID(int roleID) {
this.roleID = roleID;
}
public int getParentRoleID() {
return parentRoleID;
}
public void setParentRoleID(int parentRoleID) {
this.parentRoleID = parentRoleID;
}
public String getParentRoleName() {
return parentRoleName;
}
public void setParentRoleName(String parentRoleName) {
this.parentRoleName = parentRoleName;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
public List<Competence> getChildren() {
return children;
}
public void setChildren(List<Competence> children) {
this.children = children;
}

}

在service层把dao层查出来的数据拼接

package ****.service;

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

import com.eaglory.fq.bean.Competence;
import com.eaglory.fq.dao.CompetenceDao;

public class CompetenceService {

private CompetenceDao competenceDao = new CompetenceDao();

public List<Competence> getAllRole() {
List<Competence> roles = competenceDao.getAllRole();
// List<Competence> result1 = new ArrayList<Competence>();
Set<Integer> set = new HashSet<Integer>();
int m = roles.size();
for(int i=0;i<m; i++){
Competence c1 = roles.get(i);
c1.setChildren(new ArrayList<Competence>());
List<Competence> childrens = c1.getChildren();
// if(childrens == null){
// childrens = new ArrayList<Competence>();
// }
for(int j=0;j<m; j++){
Competence c2 = roles.get(j);
if(c1.getRoleID()==c2.getParentRoleID()){
childrens.add(c2);
c1.setLeaf(false);
c1.setExpanded(true);
set.add(j);
}
}
}
int l =set.size();
int[] arr = new int[l];
List<Integer> list = new ArrayList<Integer>();
list.addAll(set);
for(int i=0;i<list.size();i++){
arr[i]=list.get(i);
}
Arrays.sort(arr);
for(int i = l-1;i>=0;i--){
roles.remove(roles.get(arr[i]));
}
return roles;
}

}

在action层传给前端就可以了

List<Competence> competences;
try {
competences = competenceService.getAllRole();
JSONObject jsonObject = new JSONObject();
logger.info("competences:"+competences);
jsonObject.put("children", competences);
retValue = jsonObject.toString();
} catch (Exception e) {
e.printStackTrace();
retValue = "{\"children\":[]}";
}

!注意: 这里的名称要写children因为前端是默认不显示根节点的

 

前端roleTreeStore 数据源:

var roleTreeStore = Ext.create('Ext.data.TreeStore', {
model: 'Task',
proxy: {
type: 'ajax',
url: getRoleURL,
reader: {
type: 'json',
root : "children"
}
},
fields: ["roleName", "roleID"],
folderSort: true
});

 

就可以得到想要的树状图统计表:

转载于:https://www.cnblogs.com/lf90/p/5073357.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建一个store来获取数据库里面的数据 ``` Ext.define('MyApp.store.TreeStore', { extend: 'Ext.data.TreeStore', alias: 'store.tree', proxy: { type: 'ajax', url: 'tree.php', //修改为你的后端接口地址 reader: { type: 'json', rootProperty: 'data' } } }); ``` 2. 创建一个tree panel并使用store加载数据 ``` Ext.define('MyApp.view.TreePanel', { extend: 'Ext.tree.Panel', xtype: 'treepanel', requires: [ 'MyApp.store.TreeStore' ], store: { type: 'tree' }, rootVisible: false, columns: [ { xtype: 'treecolumn', dataIndex: 'text', flex: 1 } ] }); ``` 3. 编写后端接口返回数据 ``` <?php //连接数据库 $conn = new mysqli("localhost", "root", "", "test"); //查询数据 $result = $conn->query("SELECT id, text, parent_id FROM tree"); //将查询结果转换为树形结构 $data = array(); while ($row = $result->fetch_assoc()) { $data[$row['id']] = array( 'id' => $row['id'], 'text' => $row['text'], 'parent_id' => $row['parent_id'], 'children' => array() ); } $tree = array(); foreach ($data as &$node) { if ($node['parent_id'] != null) { $data[$node['parent_id']]['children'][] = &$node; } else { $tree[] = &$node; } } //返回树形结构数据 echo json_encode(array( 'success' => true, 'data' => $tree )); ``` 4. 修改数据库中的数据 如果要修改数据库中的数据,可以在树节点的编辑事件中发送请求到后端接口,将修改后的数据保存到数据库中,然后重新加载数据即可。 ``` listeners: { edit: function (editor, context) { //获取修改后的数据 var record = context.record, newData = record.getData(); //发送请求保存到数据Ext.Ajax.request({ url: 'update.php', //修改为你的后端接口地址 method: 'POST', params: newData, success: function (response) { Ext.toast('保存成功'); }, failure: function (response) { Ext.toast('保存失败'); } }); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值