阅读 86

Project(9)-职位部门的数据处理

数据库

branch表

  • id主键
  • first一级
  • second二级
  • third三级
  • fourth四级
  • fifth五级
  • status状态(是否启用)

处理后返回的数据形式

形式:不同的一级作为元素存放一个数组,后面的下一级都是在上一级的children中作为元素

list:[
       {
           id,
           name:first,
           status,
           children:[
               {
                   id,
                   name:second,
                   status,
                   children:[
                       
                   ]
               }
           ]
       }
    ]
复制代码

第一版代码实现

let list = [];
// 一级目录
for(const item of roleList){
    if( item.second === "" ){
       list.push({id:item.id,name:item.first,status:item.status,children:[]})
    }
}
// 二级目录
for(const item of roleList){
    if( item.third === "" &&item.second !==""){
        list.map((items)=>{
            if(items.name === item.first){
                 items.children.push({id:item.id,name:item.second,status:item.status,children:[]})
            }
            return items;
        })
    }
}
// 三级目录
for(const item of roleList){
    if( item.fourth === "" && item.third !== "" ){
        list.map((items)=>{
            if(items.name === item.first){
                items.children.map((itemm)=>{
                    if(itemm.name === item.second){
                         itemm.children.push({id:item.id,name:item.third,status:item.status,children:[]})
                    }
                    return itemm;
                })
            }
            return items;
        })
    }
}
// 四级目录
for(const item of roleList){
    if( item.fifth === "" && item.fourth !== "" ){
        list.map((items)=>{
            if(items.name === item.first){
                items.children.map((itemm)=>{
                    if(itemm.name === item.second){
                        itemm.children.map((iteml)=>{
                            if(iteml.name === item.third){
                                 iteml.children.push({id:item.id,name:item.fourth,status:item.status,children:[]})
                            }
                            return iteml;
                        })
                    }
                    return itemm;
                })
            }
            return items;
        })
    }
}
复制代码

第一版代码我自己看着都嫌弃,而且有了回调地狱的感觉

第二版代码实现

function deleteObj(item){
    delete item.first;
    delete item.second;
    delete item.third;
    delete item.fourth;
    delete item.fifth;
};
function screen(data){
    let firstList = [];
    let secondeList = [];
    let thirdList = [];
    let fourthList = [];
    let fifthList = [];
    data.forEach((item)=>{
        if(item.second === ""){
            firstList.push({...item.dataValues,children:[]});
        }else if(item.third === ""){
            secondeList.push({...item.dataValues,children:[]});
        }else if(item.fourth === ""){
            thirdList.push({...item.dataValues,children:[]});
        }else if(item.fifth === ""){
            fourthList.push({...item.dataValues,children:[]});
        }else{
            fifthList.push({...item.dataValues,children:[]});
        }
    });
    firstList.map((firstItem) => {
        const f = firstItem.first;
        secondeList.map((secondItem) => {
            const sf = secondItem.first;
            const s = secondItem.second;
            thirdList.map((thirdItem) => {
                const tf = thirdItem.first;
                const ts = thirdItem.second;
                const t = thirdItem.third;
                fourthList.map((fourthItem) => {
                    const ff = fourthItem.first;
                    const fs = fourthItem.second;
                    const ft = fourthItem.third;
                    const fo = fourthItem.fourth;
                    fifthList.map((fifthItem) => {
                        const fif = fifthItem.first;
                        const fis = fifthItem.second;
                        const fit = fifthItem.third;
                        const fio = fifthItem.fourth;
                        const fi = fifthItem.fifth;
                        if(fif === f&&fis === s&&fit === t&& fio === fo){
                            fifthItem.name = fi;
                            deleteObj(fifthItem);
                            fourthItem.children.push(fifthItem);
                        }
                    })
                    if(ff === f&&fs ===s&&ft === t){
                        fourthItem.name = fo;
                        deleteObj(fourthItem);
                        thirdItem.children.push(fourthItem);
                    }
                })
                if(tf === f&&ts === s){
                    thirdItem.name = t;
                    deleteObj(thirdItem);
                    secondItem.children.push(thirdItem);
                }
            })
            if(sf === f){
                secondItem.name = s;
                deleteObj(secondItem);
                firstItem.children.push(secondItem);
            }
        })
        firstItem.name = f;
        deleteObj(firstItem);
        return firstItem
    })
    return firstList;
};
复制代码

返回的数据

看不惯name在最下面的可以,在创建的时候给name属性为空,功能上没啥影响

{
    "retCode": true,
    "resultMsg": null,
    "errorCode": null,
    "data": {
        "time": 34,
        "data": [
            {
                "id": 5,
                "status": 0,
                "children": [
                    {
                        "id": 6,
                        "status": 0,
                        "children": [],
                        "name": "总经理助手"
                    },
                    {
                        "id": 7,
                        "status": 0,
                        "children": [],
                        "name": "外部专家顾问"
                    },
                    {
                        "id": 19,
                        "status": 0,
                        "children": [
                            {
                                "id": 8,
                                "status": 0,
                                "children": [
                                    {
                                        "id": 9,
                                        "status": 0,
                                        "children": [],
                                        "name": "财务审计部"
                                    },
                                    {
                                        "id": 10,
                                        "status": 0,
                                        "children": [],
                                        "name": "工程审计部"
                                    },
                                    {
                                        "id": 11,
                                        "status": 0,
                                        "children": [],
                                        "name": "招标办"
                                    }
                                ],
                                "name": "审计部"
                            },
                            {
                                "id": 20,
                                "status": 0,
                                "children": [
                                    {
                                        "id": 12,
                                        "status": 0,
                                        "children": [],
                                        "name": "资金管理部"
                                    },
                                    {
                                        "id": 13,
                                        "status": 0,
                                        "children": [],
                                        "name": "财务管理部"
                                    },
                                    {
                                        "id": 14,
                                        "status": 0,
                                        "children": [],
                                        "name": "综合会计部"
                                    },
                                    {
                                        "id": 15,
                                        "status": 0,
                                        "children": [],
                                        "name": "工程核算部"
                                    },
                                    {
                                        "id": 16,
                                        "status": 0,
                                        "children": [],
                                        "name": "工业财务管理室"
                                    }
                                ],
                                "name": "财务中心"
                            },
                            {
                                "id": 21,
                                "status": 0,
                                "children": [],
                                "name": "人力资源"
                            }
                        ],
                        "name": "架构"
                    }
                ],
                "name": "总经理"
            }
        ]
    }
}
复制代码
文章分类
前端
文章标签