后台学员列表部门显示数量

This commit is contained in:
禺狨 2023-04-17 10:11:55 +08:00
parent facbaad8f5
commit a475e693dc
3 changed files with 60 additions and 3 deletions

View File

@ -11,6 +11,7 @@ interface Option {
interface PropInterface {
type: string;
text: string;
showNum: boolean;
onUpdate: (keys: any, title: any) => void;
}
@ -18,14 +19,26 @@ export const TreeDepartment = (props: PropInterface) => {
const [treeData, setTreeData] = useState<any>([]);
const [loading, setLoading] = useState<boolean>(true);
const [selectKey, setSelectKey] = useState<any>([]);
const [total, setTotal] = useState(0);
useEffect(() => {
setLoading(true);
department.departmentList().then((res: any) => {
const departments = res.data.departments;
const departCount = res.data.dep_user_count;
if (JSON.stringify(departments) !== "{}") {
const new_arr: Option[] = checkArr(departments, 0);
setTreeData(new_arr);
if (props.showNum) {
const new_arr: any = checkNewArr(departments, 0, departCount);
setTreeData(new_arr);
let num = 0;
for (let item in departCount) {
num = num + Number(item);
}
setTotal(num);
} else {
const new_arr: Option[] = checkArr(departments, 0);
setTreeData(new_arr);
}
} else {
const new_arr: Option[] = [
{
@ -40,6 +53,38 @@ export const TreeDepartment = (props: PropInterface) => {
});
}, []);
const checkNewArr = (departments: any[], id: number, counts: any) => {
const arr = [];
for (let i = 0; i < departments[id].length; i++) {
if (!departments[departments[id][i].id]) {
arr.push({
title: getNewTitle(
departments[id][i].name,
departments[id][i].id,
counts
),
key: departments[id][i].id,
});
} else {
const new_arr: any = checkNewArr(
departments,
departments[id][i].id,
counts
);
arr.push({
title: getNewTitle(
departments[id][i].name,
departments[id][i].id,
counts
),
key: departments[id][i].id,
children: new_arr,
});
}
}
return arr;
};
const checkArr = (departments: any[], id: number) => {
const arr = [];
for (let i = 0; i < departments[id].length; i++) {
@ -60,6 +105,15 @@ export const TreeDepartment = (props: PropInterface) => {
return arr;
};
const getNewTitle = (title: any, id: number, counts: any) => {
if (counts) {
let value = counts[id] || 0;
return title + "(" + value + ")";
} else {
return title;
}
};
const onSelect = (selectedKeys: any, info: any) => {
let label = "全部" + props.text;
if (info) {
@ -89,6 +143,7 @@ export const TreeDepartment = (props: PropInterface) => {
onClick={() => onSelect([], "")}
>
{props.text}
{props.showNum && total ? "(" + total + ")" : ""}
</div>
{treeData.length > 0 && (
<Tree

View File

@ -89,6 +89,7 @@ const CoursePage = () => {
children: (
<div className="float-left">
<TreeDepartment
showNum={false}
type="no-course"
text={"部门"}
onUpdate={(keys: any, title: any) => {

View File

@ -200,6 +200,7 @@ const MemberPage = () => {
<div className="tree-main-body">
<div className="left-box">
<TreeDepartment
showNum={true}
type=""
text={"部门"}
onUpdate={(keys: any, title: any) => {