本项目简介。
老大让我们在页面左侧做一颗查询树。看了很多文档再结合自己项目最终做了出来。点击节点可查询数据。
效果如下
组件依赖
在JS和html内需要加载所用组件的一些依赖项。可以去Tree官网查询,或者layui查看。
Ztree
layui
JS
html
Controller
@RequestMapping(value = "/tree")
@ResponseBody
public synchronized List<ZTreeNode> tree() {
List<ZTreeNode> tree = new ArrayList<ZTreeNode>();
ShiroUser user = ShiroKit.getUser();
List<SrPerspective> srPerspectiveList = this.srPerspectiveService.GetTree(user.getId());
String temp ="";
String sum="";
String fullparentPath="";
ZTreeNode node = new ZTreeNode();
node.setChecked(true);
//子节点
node.setId(0L);
//节点名称
node.setName("顶级");
node.setOpen(true);
//父节点
node.setPId(0L);
//路径
node.setFullPath("");
tree.add(node);
for(SrPerspective srPerspective : srPerspectiveList){
try{
String[] aftersplit = srPerspective.getReqPath().split("\\\\");
for(int i = 0; i <aftersplit.length; i++) {
temp = aftersplit[i] + "\\";
sum += temp;
//fullparentPath这个路径会比FULLPATH少一层
fullparentPath = "";
for(int j=0;j<i;j++){
fullparentPath += (aftersplit[j] + "\\");
}
if (nodefound(sum, tree) == true) {
} else {
ZTreeNode zTreeNode = new ZTreeNode();
zTreeNode.setFullPath(sum);
zTreeNode.setName(aftersplit[i]);
//size加一是因为有顶级节点
zTreeNode.setId((long)(tree.size()+1));
zTreeNode.setPId((long)getNodeId(fullparentPath,tree));
tree.add(zTreeNode);
}
}
sum="";
}catch (Exception e){
e.printStackTrace();
}
}
return tree;
}
//判断节点是否重复
//思路就是把完整for(i)循环的路径是否存在作为判断
private boolean nodefound(String fullPath,List<ZTreeNode> zTreeNodeList){
for(ZTreeNode node : zTreeNodeList){
if(node.getFullPath().equals(fullPath)){
return true;
}
}
return false;
}
//获取当前节点ID,少一层目录的fullparentPath(for(j)循环的)与完整fullPath相同获取其ID作为父节点
private int getNodeId(String fullparentPath,List<ZTreeNode> zTreeNodeList){
for (ZTreeNode node : zTreeNodeList){
if (node.getFullPath().equals(fullparentPath)){
return node.getId().intValue();
}
}
return 0;
}
Service
提取数据库中的字段,并加权限控制。
public List<SrPerspective> GetTree(Long userId) {
List<SrPerspective> srPerspectiveList = this.baseMapper.selectReqPath(userId);
return srPerspectiveList;
}
Mapper
mapper.java
同样的,权限控制加一下。
List<SrPerspective> selectReqPath(@Param("userId") Long userId);
mapper.xml
查找切割数据。
<select id="selectReqPath" resultMap="BaseResultMap">
select V_SJ_SR_PERSPECTIVE.REQ_PATH
from V_SJ_SR_PERSPECTIVE , SJ_PP_MEMBER
WHERE V_SJ_SR_PERSPECTIVE.PROJECT_NO = SJ_PP_MEMBER.PROJECT_NO
and SJ_PP_MEMBER.USER_ID = #{userId}
</select>
查询页面数据,tempath做对比
<select id="selectPage" resultType="map">
select distinct
<include refid="Base_Column_List"/>
from V_SJ_SR_PERSPECTIVE, SJ_PP_MEMBER
WHERE V_SJ_SR_PERSPECTIVE.PROJECT_NO = SJ_PP_MEMBER.PROJECT_NO
and SJ_PP_MEMBER.USER_ID = #{userId}
<if test="name != null and name != ''">
and V_SJ_SR_PERSPECTIVE.FUNC_MODULE like CONCAT(CONCAT('%',#{name}),'%')
</if>
<if test="tempPath != null and tempPath != ''">
and CONCAT(V_SJ_SR_PERSPECTIVE.REQ_PATH ,'\\')like CONCAT(#{tempPath},'%')
</if>
</select>