el-table实现部分表格列动态展示

本文介绍了如何在Vue的ElementUI中使用el-table实现部分表格列的动态展示,包括数据绑定、列配置和使用列模板处理复杂内容

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

el-table实现部分表格列动态展示

 <el-table v-loading="loading" :data="floorAreaPlanList" @selection-change="handleSelectionChange"
                    ref="singleTable"
                    :header-cell-style="{ fontSize: '14px', height: '48px', padding: '0px', background: 'white', }"
                    :row-style="{ height: 56 + 'px' }" height="100%" @sort-change="sortChange">
                    <template slot="empty">
                        <h-empty v-if="isEmpty(floorAreaPlanList)" type="table" />
                    </template>
                    <el-table-column type="selection" width="55"> </el-table-column>
                    <el-table-column label="序号" align="center" width="70px" fixed="left">
                        <template slot-scope="scope">
                            <el-tooltip class="item" effect="dark" content="安全区" placement="top-start">
                                <i v-if="scope.row.plan.isSafety === 1" class="el-icon-warning-outline" style="font-size:14px;float: left;line-height: 23px;"></i> 
                            </el-tooltip>
                            {{  queryParams.pageSize * (queryParams.pageNum - 1) + (scope.$index + 1)}}
                        </template>
                    </el-table-column>
                    <el-table-column label="VIN" align="left" prop="plan.vinCode" fixed="left" width="170px" sortable
                        :show-overflow-tooltip="true" />
                    <el-table-column label="计划订单号" align="left" prop="plan.planOrderNo" fixed="left" width="120px"
                        :show-overflow-tooltip="true" sortable />
                    <el-table-column label="车型" align="left" prop="plan.modelCode" fixed="left" width="120px" sortable
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <div v-for="(item, index) in weldingCartypeList" :key="index">
                                <span v-if="scope.row.plan.modelCode == item.itemValue">{{ item.itemKey }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="计划下发类型" align="left" prop="plan.momType" fixed="left" width="130px" sortable
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <div v-for="(item, index) in orderTypeList" :key="index">
                                <span v-if="scope.row.plan.momType == item.itemValue">{{ item.itemKey }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="计划状态" align="left" prop="plan.planStatus" fixed="left" width="130px" sortable
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <div v-for="(item, index) in planStatusList" :key="index">
                                <span v-if="scope.row.plan.planStatus == item.code">{{ item.name }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="计划规则描述" align="left" prop="plan.planTypeName" fixed="left" width="130px"
                        :show-overflow-tooltip="true" sortable />
                    <el-table-column label="计划开始时间" align="left" prop="plan.planDate" fixed="left" width="130px" sortable
                        :show-overflow-tooltip="true" />
                    <el-table-column label="拉出原因" align="left" prop="plan.bypassReason" width="100px" sortable
                        :show-overflow-tooltip="true" />
                    <el-table-column label="主线拉出位置" align="left" prop="plan.bypassLocation" width="130px" sortable
                        :show-overflow-tooltip="true" />
                    <el-table-column v-for="(item, index) in tableLabel" :key="index" :label="item.workStationName"
                        width="160px">
                        <template slot-scope="scope">
                            <div v-for="(item1, index) in  scope.row.tasks ">
                                <span v-if="item.workStationCode == item1.stationCode">
                                    <span v-for=" item2 in taskStatusList">
                                        <span v-if="item2.code == item1.status">
                                            {{ item2.name }}
                                        </span>
                                    </span>
                                </span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" fixed="right" width="200px"
                        class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <h-button type="text" icon="hv-edit" @click="handelUpdate(scope.row)">修改
                            </h-button>
                            <h-button type="text" class="delete_button" style="color: #f56c6c"
                                @click="handleDetail(scope.row)">详情
                            </h-button>
                        </template>
                    </el-table-column>
                </el-table>

数据格式


在这里插入图片描述

JS代码

后端返回的数据为所显示数据的编码,所以需要重新匹配,使用了cloneDeep,

import { isEmpty, cloneDeep } from "lodash";
      //地板区域管理计划列表
        async getFloorAreaPlanList() {
            this.loading = true
            listFloorArea(this.queryParams).then((res) => {
                this.floorAreaPlanList = res.rows
                this.total = res.total
                this.loading = false
            })
            //获取工位列表
            await listWorkStation().then((res) => {
                this.workStationList = res
            })
            await this.$nextTick(() => {
                this.tableLabel = cloneDeep(this.workStationList)

            })
            this.$nextTick(() => {
                this.$refs.singleTable.doLayout();
            });
        },
                
<think>嗯,用户的问题是想要解决element-ui中el-table-column表格标题过长时的自动换行或省略号处理。我之前看过相关的引用资料,里面提到了内容溢出的处理,比如使用show-overflow-tooltip属性或者Scoped Slots自定义渲染。不过用户现在问的是标题,不是内容,可能需要不同的方法。 首先,标题是表头部分,和内容的处理方式可能不一样。引用中提到的show-overflow-tooltip主要针对单元格内容,但标题可能需要用CSS样式来控制。例如,设置标题的white-space、overflow和text-overflow属性,强制不换行并显示省略号。另外,可能还需要调整表头的高度或使用tooltip来显示完整标题。 然后,用户提到的自动换行,可能需要通过CSS的word-break或white-space属性来实现,比如设置white-space: normal;允许换行。不过这样可能会影响表格的布局,需要适当调整宽或表头高度。 另外,element-ui的组件可能有自带的类名,比如.el-table__header-wrapper或.el-table__header-cell,可以通过深度选择器来覆盖默认样式。例如,在scoped样式中使用::v-deep或/deep/来修改表头单元格的样式。 还需要考虑用户可能遇到的层级问题,比如tooltip被其他元素遮挡,这时候需要调整z-index的值,确保tooltip能够正常显示。引用3中提到过类似的问题,可能同样的解决方法适用于标题的tooltip。 总结一下,可能的解决方案有两种:一种是使用CSS强制标题单行显示并省略,另一种是允许标题自动换行。同时,使用tooltip来显示完整标题信息,提升用户体验。需要根据具体需求选择合适的方法,并注意样式覆盖和层级问题。</think>关于element-ui中el-table-column标题过长时的处理,可通过以下两种方式实现自动换行或省略号: --- ### 一、标题自动换行 通过CSS强制表头文本换行: ```css /* 在全局或scoped样式中添加 */ .el-table .el-table__header-wrapper th { white-space: normal !important; word-break: break-all; line-height: 1.5; /* 调整行高保证美观 */ } ``` 此方式会让所有标题自动换行,但可能影响表格布局紧凑性[^3] --- ### 二、标题省略号显示 #### 方式1:纯CSS实现 ```css /* 限制标题单行显示 */ .el-table .el-table__header-wrapper th.el-table__cell { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } ``` #### 方式2:标题Tooltip提示(推荐) ```vue <el-table-column prop="name" label="这是一个非常非常长的标题文本需要省略号处理"> <template slot="header"> <span class="header-text">{{ $t('long_column_title') }}</span> </template> </el-table-column> <style> /* 添加Tooltip效果 */ .header-text { display: inline-block; max-width: 150px; /* 根据宽调整 */ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style> ``` 通过自定义header插槽实现精确控制,同时可配合el-tooltip组件增强交互[^2][^4] --- ### 三、注意事项 1. 如果使用Tooltip时提示被遮挡,需调整CSS层级: ```css :deep(.el-tooltip__popper) { z-index: 9999 !important; } ``` 2. 固定(fixed)需要单独处理样式 3. 多语言场景需确保翻译文本长度适配[^3][^4] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值