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

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

View File

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

View File

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