easyui---tree

Posted fpcbk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了easyui---tree相关的知识,希望对你有一定的参考价值。

1、ul li的多级列表的html代码tree

 

2、利用jquery

 

  <ul id="tt"></ul>  
  $(#tt).tree({   
 url:tree_data.json    //会发送异步请求返回tree数据
});  

 

tree实体类:
public
class Tree { private int id ; private String name ; private String url ; private int checked ; private String icon ; private int parent_id ; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getChecked() { return checked; } public void setChecked(int checked) { this.checked = checked; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public int getParent_id() { return parent_id; } public void setParent_id(int parent_id) { this.parent_id = parent_id; }
treeVO 返回jsontree格式,必须这种形式tree,才能渲染成树:
public
class TreeVO { private int id; private String text; private int parent_id; private String icon ; private String state ; private Map<String,Object> map=new HashMap<String ,Object>(); //用个map接收自定义属性,必须map,转为json后,是{"a":"","b":..}这种格式,同时具有可扩展性 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public int getParent_id() { return parent_id; } public void setParent_id(int parent_id) { this.parent_id = parent_id; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } }
import com.bjsxt.dto.TreeVO;

public interface TreeDao {

    List<TreeVO> getTreeByParentId(String id);

}
public class TreeDaoImpl implements TreeDao {

    @Override
    public List<TreeVO> getTreeByParentId(String id) {
        String sql;
        if(id==null || id==""){ //刚开始页面加载完成之后,页面出现根节点,这时,前台不带参数id,只有点击closed的节点才会发送一个请求,携带id

技术分享图片

 




int pid=2340; sql ="select * from tree where parent_id ="+2340; System.out.println(sql); }else{

技术分享图片技术分享图片//点击这些关闭的节点时才会发送一个url,同时携带这个节点的id,为什么easyui树高效,dtree不行,dtree是一打开页面加载所有的节点,而easyUi是点击节点才会异步发送一个请求,获取该节点下直接子节点渲染成树

 

 


int pid=Integer.valueOf(id); sql ="select * from tree where parent_id ="+pid; System.out.println(sql); } Connection conn = DBUtils.createConn(); PreparedStatement ps = DBUtils.getPs(conn, sql); try { ResultSet rs = ps.executeQuery(); List<Tree> treeList =new ArrayList<Tree>(); while(rs.next()){ Tree tree=new Tree(); tree.setId(rs.getInt("id")); tree.setChecked(rs.getInt("checked")); tree.setIcon(rs.getString("icon")); tree.setName(rs.getString("name")); tree.setParent_id(rs.getInt("parent_id")); tree.setUrl(rs.getString("url")); treeList.add(tree); } List<TreeVO> treeVOlist = this.tochange(treeList); System.out.println(treeList+"treelist"); return treeVOlist; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private List<TreeVO> tochange(List<Tree> treeList){ List<TreeVO> treeVOlist =new ArrayList<TreeVO>(); for (Tree tree : treeList) { TreeVO treeVO =new TreeVO(); treeVO.setId(tree.getId()); List<Tree> tls = this.distinguish(tree.getId()); if(tls.isEmpty()){ treeVO.setState("open"); }else{ treeVO.setState("closed"); } treeVO.setIcon(tree.getIcon()); treeVO.setParent_id(tree.getParent_id()); treeVO.setText(tree.getName()); Map<String,Object> hashMap=new HashMap<String,Object>(); hashMap.put("url", ""); treeVO.setMap(hashMap); treeVOlist.add(treeVO); } System.out.println(treeList+"treelist1"); return treeVOlist; } private List<Tree> distinguish(int id){//判断节点下面有没有子节点,有的话,下面的子节点不为空,没有的话就是叶子节点,这时会自动渲染成

技术分享图片
    
            String sql ="select * from tree where parent_id ="+id;
    
        Connection conn = DBUtils.createConn();
        PreparedStatement ps = DBUtils.getPs(conn, sql);
        try {
            ResultSet rs = ps.executeQuery();
            List<Tree> treeList =new ArrayList<Tree>();
            while(rs.next()){
                Tree tree=new Tree();
                tree.setId(rs.getInt("id"));
                tree.setChecked(rs.getInt("checked"));
                tree.setIcon(rs.getString("icon"));
                tree.setName(rs.getString("name"));
                tree.setParent_id(rs.getInt("parent_id"));
                tree.setUrl(rs.getString("url"));
                treeList.add(tree);
            }
        
            return treeList;
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
        return null;
    }
}
@WebServlet("/tree")
public class Tree extends HttpServlet {
    private static final long serialVersionUID = 1L;
  private TreeDao treeDao =new TreeDaoImpl();
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if(method.equals("getTree")){
            getTree(request,response);
            
        }
        
        
    }

    private void getTree(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        List<TreeVO> treeList=treeDao.getTreeByParentId(id);
        response.setContentType("text/html;charset=utf-8");
        try {
            response.getWriter().write(JSONArray.fromObject(treeList).toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

 

 

 

-----

以上是关于easyui---tree的主要内容,如果未能解决你的问题,请参考以下文章

easyui tree

jquery easyui tree 怎么自动节点

easyui tree 怎么获取选择节点子节点上的值?js怎么写?

easyui tree获取父节点的父节点

easyui tree 级联从三种状态变为两种状态

用easyui tree从后台传data,遇到树结构层级比较多的怎么办?