赞
踩
目的:
思路:
html
<el-table id="watermarkBox" :data="dataPreviewTableData" stripe > <el-table-column v-for="(item, index) in dataListTableHeadAll" :key="index" :width="totalWidth > 1000 ? item.width : ''" :prop="item.columnCode" :label="item.columnShowName" show-overflow-tooltip align="center" > </el-table-column> </el-table>
js
//表格头部信息 this.totalWidth = 0 for (let i in res.data) { let columnShowName = res.data[i].columnShowName; if (columnShowName) { const columnWidth = getTextWidth(columnShowName,'17px Microsoft Yahei') + 30 this.totalWidth += columnWidth res.data[i].width = columnWidth } } /** * @description: canvas方式计算字符串宽度 * @param {*} text '文本内容' * @param {*} font '14px Microsoft Yahei' * @return {*} */ getTextWidth(text, font = '14px Microsoft Yahei') { let canvas = (document.createElement("canvas")); let context = canvas.getContext("2d"); context.font = font; let metrics = context.measureText(text); return metrics.width; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。