当前位置:优学网  >  在线题库

这段判断 内执行共同的循环代码怎么优化

发表时间:2022-06-23 20:34:10 阅读:60

这段代码 如何进一步优化,例如进一步提炼封装,请教各位大神,谢谢!

filterColumn(){               
    if(this.searchFilter.asinType === 'sku'){       
        this.vxeGridTableColumnList.map(item => {
            (item.field === 'msku' || item.field === 'asin') && this.columnDynamicShow(item,1) 
            item.field === 'sku' && this.columnDynamicShow(item,3)
            item.field === 'parentAsin' && this.columnDynamicShow(item,2)
            item.field === 'devName' && (item.title = '开发员')
        })
    }
    if(this.searchFilter.asinType === 'parentAsin'){                           
        this.vxeGridTableColumnList.map(item => {
            if(item.field === 'msku' || item.field === 'sku')  this.columnDynamicShow(item,2)
            if(item.field === 'asin' || item.field === 'parentAsin')  this.columnDynamicShow(item,1)
            item.field === 'parentAsin' && this.columnDynamicShow(item,3)
            item.field === 'devName' && (item.title = '业务员')
        })
    }
    if(this.searchFilter.asinType === 'msku'){                           
        this.vxeGridTableColumnList.map(item => {
            item.field === 'msku'  && this.columnDynamicShow(item,3)
            if(item.field === 'asin' || item.field === 'sku')  this.columnDynamicShow(item,1)
            item.field === 'parentAsin' && this.columnDynamicShow(item,2)
            item.field === 'devName' && (item.title = '业务员')
        })
    }
    if(this.searchFilter.asinType === 'asin'){                           
        this.vxeGridTableColumnList.map(item => {
            (item.field === 'msku' || item.field === 'parentAsin') && this.columnDynamicShow(item,2) 
            item.field === 'asin' && this.columnDynamicShow(item,3)
            item.field === 'sku' && this.columnDynamicShow(item,2)
            item.field === 'devName' && (item.title = '业务员')
        })
    }
    // this.initVTCConfig()
},
columnDynamicShow(item,type){
    switch (type){
        case 1 : //显示 不固定
                item.fixed = ''
                item.params.visibleNot = false
        break;
        case 2 : //不显示 不固定
                item.fixed = ''
                item.params.visibleNot = true
        break;
        case 3 : //显示 固定
                item.fixed = 'left'
                item.params.visibleNot = false
        break;
        default:  break;
    }
},
🎖️ 优质答案
  • // 业务描述,如果需求有变动改这里
    const setting = {
        sku: {
            devName: '开发员', msku: 1, asin: 1, sku: 3, parentAsin: 2
        }, 
        parentAsin: {
            devName: '业务员', msku: 2, asin: 1, sku: 2, parentAsin:3
        }, 
        msku: {
            devName: '业务员', msku: 3, asin: 1, sku: 1, parentAsin:2
        }, 
        asin: {
            devName: '业务员', msku: 2, asin: 3, sku: 2, parentAsin:2
        }
    };
    // 逻辑处理,遍历数据,与setting匹配的部分做替换
    filterColumn(){               
            this.vxeGridTableColumnList.map(item => {
                if (setting[this.searchFilter.asinType]) {
                    const settingByType = setting[this.searchFilter.asinType];
                    if (settingByType[item.field]) {
                        if (item.field = 'devName') {
                            item.title = settingByType.devName;
                        } else {
                            this.columnDynamicShow(item, settingByType[item.field]);
                        }
                    }
                }
        }
        // this.initVTCConfig()
    },
    columnDynamicShow(item,type){
        switch (type){
            case 1 : //显示 不固定
                    item.fixed = ''
                    item.params.visibleNot = false
            break;
            case 2 : //不显示 不固定
                    item.fixed = ''
                    item.params.visibleNot = true
            break;
            case 3 : //显示 固定
                    item.fixed = 'left'
                    item.params.visibleNot = false
            break;
            default:  break;
        }
    },
  • 相关问题