bootstrap treeview Java拼接菜单

数据库:

  <resultMap id="BaseResultMap" type="com.bas.bean.Menu" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="menu_no" property="menuNo" jdbcType="VARCHAR" />
    <result column="pid" property="pid" jdbcType="VARCHAR" />
    <result column="url" property="url" jdbcType="VARCHAR" />
    <result column="type" property="type" jdbcType="INTEGER" />
    <result column="icon" property="icon" jdbcType="VARCHAR" />
    <result column="createTime" property="createtime" jdbcType="VARCHAR" />
    <result column="updateTime" property="updatetime" jdbcType="VARCHAR" />
    <result column="level" property="level" jdbcType="INTEGER" />
  </resultMap>

  <!-- 得到所有菜单 -->
    <select id="getAllMenu" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
   select 
     <include refid="Base_Column_List" />
   from menu t order by  t.pid asc 

  </select>

package com.bas.util;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.bas.bean.Menu;


public class TreeUtil {


/**
* 使用占位符拼接字符串  
* 建立等级——等级对应的实体个数
* @param menu
* @return
*/
public static String getTreeJson(List<Menu> menu){

//默认最顶级
StringBuffer sb=new StringBuffer();
Map<String,Menu> param=new HashMap<String,Menu>();

//存放 pid——pid个数
List<String> numlist=new ArrayList<String>();
for(Menu vo: menu){
param.put(vo.getId().toString(), vo);
if(numlist.contains(vo.getPid())){
continue;
}else{
numlist.add(vo.getPid());
}
}
String pid="0";
Boolean flag=true;
int i=1;
for(String num:numlist){
i=1;
flag=true;
String len="&"+num+"&";
StringBuffer sbf=new StringBuffer();
if(num.equals("0")){
Menu pvo=param.get("1");
sb=sb.append("{ text :'"+pvo.getName()+"',id :'"+pvo.getId()+"'&"+pvo.getId()+"&}");
param.remove("1");
}else{
for(Menu vo: param.values()){
pid=vo.getPid();
if(num.equals(pid)){
if(sb.indexOf(len)>0){
if(flag){
sb.insert(sb.indexOf(len), ",nodes: [");
sb.insert( sb.indexOf(len)+len.length(), "]");
flag=false;
}
if(i==1){
String s="{ text :'"+vo.getName()+"',id :'"+vo.getId()+"'&"+vo.getId()+"&}";
sbf.append(s);
i=i+1;
continue;
}if(i>1){
String s=",{ text :'"+vo.getName()+"',id :'"+vo.getId()+"'&"+vo.getId()+"&}";
sbf.append(s);
i=i+1;
continue;
}
}
}
}
if(sb.indexOf(len)>0){
sb.replace(sb.indexOf(len),sb.indexOf(len)+len.length() , sbf.toString());
}
}
}
while(sb.indexOf("&")>0){
sb.delete(sb.indexOf("&"), sb.indexOf("&", sb.indexOf("&")+1)+1);
}
return sb.toString();
}

}

  @RequestMapping("/menu")
  public void  getMenu(HttpServletRequest request,HttpServletResponse response) throws Exception{
  List<Menu> tree = menuService.getAllMenu();
  String json = TreeUtil.getTreeJson(tree);
  response.setCharacterEncoding("utf-8");
       response.getWriter().write(json);
       response.getWriter().close();
   }

<title>首页</title>
    <script src="${rc.contextPath}/static/util/js/jquery-3.3.1.min.js"></script>
    <link href="${rc.contextPath}/static/util/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="${rc.contextPath}/static/util/css/bootstrap-treeview.css" rel="stylesheet"/>
<script src="${rc.contextPath}/static/util/js/bootstrap.min.js"></script>
<script src="${rc.contextPath}/static/util/js/bootstrapValidator.js"></script>
<script src="${rc.contextPath}/static/util/js/bootstrap-treeview.js"></script>
 <script type="text/javascript">
 $(function () {
     $.ajax({
         type: "Post",
         url: "${rc.contextPath}/login/menu",
         dataType:"text",
         success: function (data) {
        var tree =  eval('['+data+']');                
             $('#tree').treeview({
                 data: tree,         // 数据源
                 showCheckbox: true,   //是否显示复选框
                 highlightSelected: true,    //是否高亮选中
                 //nodeIcon: 'glyphicon glyphicon-user',    //节点上的图标
                 nodeIcon: 'glyphicon glyphicon-globe',
                 emptyIcon: '',    //没有子节点的节点图标
                 multiSelect: false,    //多选
                 onNodeChecked: function (event,data) {
                     alert(data.nodeId);
                 },
                 onNodeSelected: function (event, data) {
                     alert(data.nodeId);
                 }
             });
         },
         error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(XMLHttpRequest.status);
             alert(XMLHttpRequest.readyState);
             alert(textStatus);
             alert("树形结构加载失败!")
         }
     });
 })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java在窗口中添加树形菜单TreeView源代码,分享给JAVA新手的一个例子,JTextField jtfInfo; //文本域,用于显示点击的节点名称   public JTreeDemo(){    super("森林状的关系图"); //调用父类构造函数       DefaultMutableTreeNode root = new DefaultMutableTreeNode("设置"); //生成根节点    DefaultMutableTreeNode node1=new DefaultMutableTreeNode("常规"); //生成节点一    node1.add(new DefaultMutableTreeNode("默认路径")); //增加新节点到节点一上    node1.add(new DefaultMutableTreeNode("保存选项"));    root.add(node1); //增加节点一到根节点上    root.add(new DefaultMutableTreeNode("界面"));    root.add(new DefaultMutableTreeNode("提示声音"));    root.add(new DefaultMutableTreeNode("打印"));       JTree tree = new JTree(root); //得到JTree的实例    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)tree.getCellRenderer(); //得到JTree的Renderer    renderer.setLeafIcon(null); //设置叶子节点图标为空    renderer.setClosedIcon(null); //设置关闭节点的图标为空    renderer.setOpenIcon(null); //设置打开节点的图标为空       tree.addTreeSelectionListener(new TreeSelectionListener() { //选择节点的事件处理    public void valueChanged(TreeSelectionEvent evt) {    TreePath path = evt.getPath(); //得到选择路径    String info=path.getLastPathComponent().toString(); //得到选择的节点名称    jtfInfo.setText(info); //在文本域中显示名称    }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值