el-table动态增减列(记录一下~)

<el-table :data="dailyPlanData" 
    :height="tableHeight"
    ref="dailyPlanTable"
    border
    style="width:100%; height:100% fontSize:12px;marginTop:50px;"
    :header-cell-style="{'text-align':'center'}"
    :cell-style="{'text-align':'center'}">
    <el-table-column label="序号" prop="index" width="50" show-overflow-tooltip/>
    <el-table-column label="任务数量" prop="qtyPlan"/>
    <el-table-column label="产品编码" prop="proCode" show-overflow-tooltip/>
    <el-table-column label="产品名称" prop="proName" show-overflow-tooltip/>
    <!--根据后端数据渲染多级表头(!!!重点)-->
    <el-table-column v-for="(col,index) in cols" :key="index" :prop="col.prop" :label="col.label">
        <el-table-column label="当日计划"  width="70">
            <template slot-scope="scope">
            <!--outputSrc是后端返回的一个数组,qtyPlan为数组里的属性,完工数量同理 -->
                {{scope.row.outputSrc[index].qtyPlan}}
            </template>
        </el-table-column>
        <el-table-column label="完工数量" width="70">
            <template slot-scope="scope">
                {{scope.row.outputSrc[index].qtyOutput}}
            </template>
        </el-table-column>
    </el-table-column>
</el-table>

处理逻辑

1.data里的数据

data(){
    return{
        dailyPlanData:[], // 表格数据
            cols:[
                // {prop:'list0',label:"第一列"},
                // {prop:'list1',label:"第二列"},
            ],//动态增加列的表头数据
            screenHeight:"",//屏幕高度
            tableHeight:"", //表格高度
    }
}

2.方法

created(){
        window.addEventListener('resize', this.getSeren)
        this.getSeren()
    },
methods:{
        //获取屏幕及表格高度(表格高度按实际情况计算)
        getSeren(){
            this.screenHeight = window.innerHeight;
            this.tableHeight = (this.screenHeight-150);
        },

        // 查询
        queryForDailyPlan(){
            let startDate = this.value1[0];
            let endDate = this.value1[1];
            let createStartDate = this.value2[0];
            let createEndDate = this.value2[1];
            // console.log(createStartDate+","+createEndDate);
            this.$axios.post("api/MESAPI/Report/GetDailyPlanAndOutput",{
                fromPlan:startDate,
                toPlan:endDate,
                factory:this.plant,
                lineCode:this.proClassLine,
                teamName:this.proTeam,
                taskNo:this.taskCode,
                fromTask:createStartDate,
                toTask:createEndDate
            }).then(res=>{
                // console.log(res);
                if(res.data.code == 200){
                    this.dailyPlanData = res.data.data;
                    for(let i = 0 ; i < this.dailyPlanData.length;i++){
                        this.listDate = this.dailyPlanData[i].outputSrc;
                    }
                    // 循环有多少要增加的列
                    let arr = [];
                    for(let i=0; i< this.listDate.length;i++){
                        //存储二级表头中第一行的日期数据
                        arr.push({prop:'list'+i,label:this.listDate[i].day})
                    }
                    this.cols = arr;
                    // 解决动态增加列后,表格高度变化问题,对表格进行重加载
                    this.$nextTick(() => {
                        this.$refs.dailyPlanTable.doLayout();
                        // el-table添加ref="dailyPlanTable"
                    });
                }else{
                    alert(res.data.msg)
                }
            })
        }
    }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值