前端:
在ruoyi-ui/src/api/system/dept.js文件中加入:
// 查询子部门列表
export function listDeptChild(deptId) {
return request({
url: '/system/dept/list/child/' + deptId,
method: 'get'
})
在需要获取的.vue文件中加入:
import { listDeptChild } from "@/api/system/dept";
然后在data中加入:
const deptList = ref([]);
并加入函数:
function getDeptList() {
loading.value = true;
listDeptChild('102').then(response => {
deptList.value = response.rows;
loading.value = false;
});
}
其中,我这里需要获取的是父节点ID为102的所有部门。
如果没定义过loading和proxy,需要定义:
const { proxy } = getCurrentInstance();
const loading = ref(true);
前端就能获取到子部门的信息到deptList中。
后端:
在ruoyi-admin/src/main/java/com/ruoyi/web/system/SysDeptController文件中加入:
/**
* 根据父节点获取子部门列表
*/
@PreAuthorize("@ss.hasPermi('system:dept:list')")
@GetMapping("/list/child/{deptId}")
public TableDataInfo getchilddeptlist(@PathVariable(value = "deptId", required = false) Long deptId)
{
List<SysDept> depts = deptService.selectChildrenDeptById(deptId);
return getDataTable(depts);
}
在ruoyi-system/src/main/java/com/ruoyi//system/mapper/SysDeptMapper中加入:
List<SysDept> selectChildrenDeptById(Long deptId);
在ruoyi-system/src/main/java/com/ruoyi//system/service/ISysDeptService中加入:
List<SysDept> selectChildrenDeptById(Long deptId);
在ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl中加入:
/**
* 获取子节点所有部门信息
*/
@Override
public List<SysDept> selectChildrenDeptById(Long deptId) {
return deptMapper.selectChildrenDeptById(deptId);
}
在resources中对应的SysDeptMapper.xml中有了对应的查询语句,所以不需要再加入。
<select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
select * from sys_dept where find_in_set(#{deptId}, ancestors)
</select>