• 树形目录形式总结


    这个是前端把后端提交数据变成树形:是第一种方法(爱苗老师提供指导,谢谢!)

    项目名称image

    前端形式image

    数据库image

    数据库结构

    image

    exttree.js

    /**
    * @author ZMZY
    *
    * @requires jQuery,EasyUI
    *
    * 扩展tree,使其支持平滑数据格式
    */
    $.fn.tree.defaults.loadFilter = function(data, parent) {
        var opt = $(this).data().tree.options;
        var idFiled, textFiled, parentField;
        if (opt.parentField) {
            idFiled = opt.idFiled || 'id';
            textFiled = opt.textFiled || 'text';
            parentField = opt.parentField;
            var i, l, treeData = [], tmpMap = [];
            for (i = 0, l = data.length; i < l; i++) {
                tmpMap[data[i][idFiled]] = data[i];
            }
            for (i = 0, l = data.length; i < l; i++) {
                if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
                    if (!tmpMap[data[i][parentField]]['children'])
                        tmpMap[data[i][parentField]]['children'] = [];
                    data[i]['text'] = data[i][textFiled];
                    tmpMap[data[i][parentField]]['children'].push(data[i]);
                } else {
                    data[i]['text'] = data[i][textFiled];
                    treeData.push(data[i]);
                }
            }
            return treeData;
        }
        return data;
    };

    departtree.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

    %>


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
       
        <title>depart-tree</title>
       
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">   
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <link rel="stylesheet" type="text/css" href="<%=path %>/easyui/themes/icon.css">
        <link rel="stylesheet" type="text/css" href="<%=path %>/easyui/themes/default/easyui.css">
        <link rel="stylesheet" type="text/css" href="<%=path %>/css/style.css">
        <script type="text/javascript" src="<%=path %>/easyui/jquery.min.js"></script>
        <script type="text/javascript" src="<%=path %>/easyui/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="<%=path %>/syvoucher/extTree.js"></script>
        <script type="text/javascript" src="<%=path %>/easyui/easyui-lang-zh_CN.js"></script>
        <script type="text/javascript" src="<%=path %>/scripts/function.js"></script>
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
       <script type="text/javascript">
        $(function(){
       // var data = eval("(" + '${tt}' + ")");
       
        $('#tt').tree({
                    url:'${pageContext.request.contextPath}/depart_treelist',
                    parentField:'pid',               
                    onLoadSuccess : function(node, data) {
                    console.info("node"+node);
                    console.info(data);
                    }
                   
             });
        })
        </script>
      </head>
     
      <body>
       <div class="easyui-panel">
           <ul id="tt" class="easyui-tree" ></ul>
       </div>
      </body>
    </html>

    departAction.java

    public void treelist() {
           
            //List<ChannelTree> ct = channeldao.generateTree();
            /*List <ChannelTree> ctl = new ArrayList<ChannelTree>();
            System.out.println(ctl);//[]
            ctl= channelService.generateTree();
            //String resourceTreeJson = com.alibaba.fastjson.JSON.toJSONString(ctl);
            try {
                ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
                ServletActionContext.getResponse().getWriter().write(JSON.toJSONString(ctl));
                System.out.println(ctl);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/
            List<Tree> alt = new ArrayList<Tree>();
            alt=channelService.departTree();
            try {
                ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
                ServletActionContext.getResponse().getWriter().write(JSON.toJSONString(alt));
                System.out.println(alt);
                //HttpServletRequest request = null;
                //request.setAttribute("tt",JSON.toJSONString(alt));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    channelServericeImpl.java

    public List<Tree> departTree() {
           private IDepartDao departDao;

           String hql="from Depart ";
            List<Depart> LD=departDao.list(hql);//****获取父结点
            List<Tree> lt= new ArrayList<Tree>();
           
            for(Depart dept:LD){
                Tree tree = new Tree();
                tree.setId(dept.getDepartid());
                tree.setText(dept.getDepartname());
                if(dept.getDept()!=null){
                    tree.setPid(dept.getDept().getDepartid());
                }
                lt.add(tree);
            }
            return lt;
        }

    IdepartDao.java

    package kongque.dao;

    import kongque.model.Depart;

    public interface IDepartDao extends IBaseDao<Depart>{
       
    }

    BaseDao.java

    public List<T> list(String hql) {
        System.out.println("hql==========");
        //this.getSession().createSQLQuery("").list();  //zhixing sqlchaxun
        return this.getSession().createQuery(hql).list();
    }

    Depart.java

    package kongque.model;

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

    import javax.persistence.*;

    @Entity
    @Table(name="kongque_depart")
    public class Depart {
        private String departid;
        private String departname;
        //private String parentid;
        private Depart dept;
       
        private Set<Depart> depts = new HashSet<Depart>();

        /*参考 dormitory项目
        private List<Depart> deptsnode ;//参考 dormitory项目
        public List<Depart> getDeptsnode() {
            return deptsnode;
        }
        public void setDeptsnode(List<Depart> deptsnode) {
            this.deptsnode = deptsnode;
        }参考 dormitory项目*/
        @Id
        @GeneratedValue
        public String getDepartid() {
            return departid;
        }
        public void setDepartid(String departid) {
            this.departid = departid;
        }
       
        public String getDepartname() {
            return departname;
        }
        public void setDepartname(String departname) {
            this.departname = departname;
        }
        /*public String getParentid() {
            return parentid;
        }
        public void setParentid(String parentid) {
            this.parentid = parentid;
        }*/
        /*
         * 父结点
         */
        @ManyToOne
        @JoinColumn(name="pid")

        public Depart getDept() {
            return dept;
        }
        public void setDept(Depart dept) {
            this.dept = dept;
        }
        /*
         * 子结点
         */
        @OneToMany(mappedBy="dept")
        public Set<Depart> getDepts(){
            return depts;
        }
        public void setDepts(Set<Depart> depts) {
            this.depts = depts;
        }
       
       
    }

    Tree.java

    package kongque.model;
    import java.util.List;
    public class Tree implements java.io.Serializable {

        private String id;
        private String text;
        private String state = "open";// open,closed
        private boolean checked = false;
        private Object attributes;
        private List<Tree> children;
        private String iconCls;
        private String pid;

        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 boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public Object getAttributes() {
            return attributes;
        }

        public void setAttributes(Object attributes) {
            this.attributes = attributes;
        }

        public List<Tree> getChildren() {
            return children;
        }

        public void setChildren(List<Tree> children) {
            this.children = children;
        }

        public String getIconCls() {
            return iconCls;
        }

        public void setIconCls(String iconCls) {
            this.iconCls = iconCls;
        }

        public String getPid() {
            return pid;
        }

        public void setPid(String pid) {
            this.pid = pid;
        }

    }

  • 相关阅读:
    a标签中调用js的几种方法
    IE11浏览器:请不要再叫我IE,谢谢
    IE11浏览器:请不要再叫我IE,谢谢
    浅谈href=#与href=javascript:void(0)的区别
    浅谈href=#与href=javascript:void(0)的区别
    Google Java编程风格指南
    Google Java编程风格指南
    Git学习小结 ~ Lethe's Blog
    Binder机制简析(三)
    Ness
  • 原文地址:https://www.cnblogs.com/elite-2012/p/4754304.html
Copyright © 2020-2023  润新知