jstree 树默认选中子节点
程序员文章站
2022-07-13 16:10:34
...
相信很多人都在为无法默认选中树节点而苦恼,弄出来了特意分享出来
public Object Menu(String roleId, HttpServletRequest req) {
List<SysMenu> list = menuService.query(Cnd.orderBy().asc("location").asc("path"));
List<SysMenu> datas = roleService.getDatas();
List<NutMap> menus = new ArrayList<>();
List<SysMenu> roleMenus = roleService.getMenusAndButtons(roleId);//当前角色的权限
for (SysMenu menu : list) {
NutMap map = new NutMap();
for (SysMenu bt : datas) {
if (menu.getPath().equals(bt.getPath().substring(0, bt.getPath().length() - 4))) {
menu.setHasChildren(true);
break;
}
}
for(SysMenu rm:roleMenus){
if (menu.getId().equals(rm.getId())){
map.put("state", new State(false,false,true));//是否允许编辑-是否展开-是否选中树节点
break;
}
}
map.put("id", menu.getId());
map.put("text", menu.getName());
map.put("icon", Strings.sBlank(menu.getIcon()));
map.put("parent", "".equals(Strings.sNull(menu.getParentId())) ? "#" : menu.getParentId());
map.put("data", menu.getHref());
menus.add(map);
}
req.setAttribute("menus", Json.toJson(menus));
return Strings.isBlank(roleId) ? null : roleService.fetch(roleId);
}
public class State { private boolean disabled; private boolean opened; private boolean selected; /** * 树节点勾选 * @param disabled 编辑 * @param opened 是否展开 * @param selected 是否选中 */ public State(boolean disabled,boolean opened,boolean selected){ this.disabled=disabled; this.opened=opened; this.selected=selected; } public boolean isDisabled() { return disabled; } public void setDisabled(boolean disabled) { this.disabled = disabled; } public boolean isOpened() { return opened; } public void setOpened(boolean opened) { this.opened = opened; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } }
<header class="header navbar bg-white shadow"> </header> <div class="content-wrap"> <div class="wrapper"> <div class="row mb15"> <div class="col-lg-12"> <form id="addRoleForm" class="form-horizontal stepy" method="post" action="url"> <input type="hidden" id="menuIds" name="menuIds"/> <div class="form-group no-b"> <div class="col-sm-8"> <div class="panel"> <div class="panel-heading no-b"> <h5>选择<b>菜单</b></h5> </div> <div class="panel-body"> <div id="jsTreeMenu"></div> </div> </div> </div> <div class="col-sm-4"> <div class="panel"> <div class="panel-heading no-b"> <h5>菜单<b>描述</b></h5> </div> <div id="menuDescript" class="panel-body"> <div class='alert alert-info'>请勾选每一个需要分配的菜单</div> </div> </div> </div> </div> </form> </div> </div> </div> </div> <script language="JavaScript"> var jsTreeMenu; $(function () { jsTreeMenu = $("#jsTreeMenu").jstree({ plugins: ["wholerow", "checkbox"], core: { data: ${menus!} }, checkbox: { three_state: false, cascade: 'down' } }).on("hover_node.jstree", function (e, data) { if (data.node.data) $("#menuDescript").text(data.node.data); }).on("dehover_node.jstree", function (e, data) { $("#menuDescript").html("<div class='alert alert-info'>请勾选每一个需要分配的菜单</div>");//清空菜单描述 }).bind("loaded.jstree", function (e, data) { //立即加载树下所有节点 }); }); function each(node, tree, nodes) { var p = tree.get_parent(node); if (p.length > 1) { if (nodes&&p&&nodes.indexOf(p)<0) nodes.push(p); each(p, tree, nodes); } } function complete() { var tree = $.jstree.reference("#jsTreeMenu"); var ids = tree.get_selected(); //查询所选节点的所有父节点 for (var i = 0; i < ids.length; i++) { var node = ids[i]; each(node, tree, ids); } if (ids.length > 0) { $.post("url", {menuIds: ids.toString(), roleid: '${obj.id}'}, function (data) { if (data.code == 0) { Toast.success(data.msg); setTimeout(function () { $("#goback").trigger("click"); }, 1000); } else { Toast.error(data.msg); } }, "json"); } else { Toast.warning("请先选择菜单!"); } } </script> <%}%>