以树-tree的形式返回数据
—后台
实体类:
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* @author Administrator
*/
@Data
@TableName("sys_org")
public class SysOrgEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 组织id
*/
@TableId
private Long orgId;
/**
* 组织名称
*/
private String name;
/**
* 组织父id
*/
private Long parentId;
/**
* 父菜单名称
*/
@TableField(exist=false)
private String parentName;
/**
* 组织编号
*/
private String orgNum;
/**
* 组织负责人
*/
private String orgLeader;
/**
* 手机号
*/
private String phone;
/**
* 创建人名称
*/
private String createUser;
/**
* 修改人名称
*/
private String updateUser;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date updateTime;
/**
* 组织备注
*/
private String orgRemark;
/**
* ztree属性
*/
@TableField(exist=false)
private Boolean open;
@TableField(exist=false)
private List<?> list;
}
业务类
/**
* 获取树的组织
* @return
*/
List<SysOrgEntity> getTreeList();
业务层实现类
@Override
public List<SysOrgEntity> getTreeList() {
List<SysOrgEntity> list = this.list();
if (list == null || list.size() <= 0){
return null;
}
LinkedList<SysOrgEntity> linkedList = new LinkedList<>();
list.forEach(orgEntity -> {
if (orgEntity.getParentId() == 0){
linkedList.add(orgEntity);
}
});
linkedList.forEach(orgEntity -> {
orgEntity.setList(getChildren(orgEntity.getOrgId(),list));
});
return linkedList;
}
private List<SysOrgEntity> getChildren(Long parentId, List<SysOrgEntity> orgList){
//孩子集合
LinkedList<SysOrgEntity> linkedList = new LinkedList<>();
orgList.forEach(orgEntity -> {
if (parentId.equals(orgEntity.getParentId())){
linkedList.add(orgEntity);
}
});
// 递归退出条件
if (linkedList.size() == 0){
return null;
}
// 把子菜单的子菜单再递归循环一遍
linkedList.forEach(orgEntity -> {
orgEntity.setList(getChildren(orgEntity.getOrgId(),orgList));
});
return linkedList;
}
前台---级联
<el-form-item placeholder="学院名称">
<el-cascader expand-trigger="hover" :options="options" :props="defaultParams" change-on-select v-model="selectedOptions"
@change="handleMenu" ref="cascaderMenu" change-on-select :show-all-levels="false" clearable>
</el-cascader>
</el-form-item>
export default {
data() {
return {
defaultParams: {
label: 'name',
value: 'orgId',
children: 'list'
},
options: [],
selectedOptions: [],
selectedOrgId: ''
}
},
handleMenu() {
this.selectedOrgId = this.$refs['cascaderMenu'].currentValue
console.log(this.selectedOrgId)
},
mounted: function() {
// 调用方法获取所有数据
this.getOrgList()
},
getOrgList() {
this.$http({
url: this.$http.adornUrl('/sys/org/tree'),
method: 'get',
params: this.$http.adornParams()
}).then(({
data
}) => {
this.options = data
})
}