vue el-table 自适应表格内容宽度
由于表头和列是分开渲染的,通过el-table 设置fit属性,只能撑开表头,但是没有办法根据列的内容去适应宽度。网上找了一些使用根据表格内容计算表头宽度的文章,记个笔记。
代码逻辑是通过vue 的watch 监控表格的数据data,计算每列的内容和表头的最大宽度,计算的时候把表格内容使用span标签包裹,然后计算span标签的宽度width:px,然后再加上表格的内边距,
就得到了列的最大宽度.
前端
<el-table :key="key" :data="data" v-loading="loading" style="width: 100%;" >
<el-table-column v-for="fruit in formThead"
:key="fruit.prop"
:label="fruit.label"
:width="fruit.width" <!-- 设置宽度 -->
show-overflow-tooltip>
<template slot-scope="scope">
{{ scope.row[fruit.prop] }}
</template>
</el-table-column>
</el-table> <script>
const fields = [
{label:"ID", prop:"id"},
{label:"资产名称", prop:"asset_name"},
{label:"OA工单号", prop:"oa_jon_num"},
{label:"IP", prop:"ipaddress"},
{label:"SN号", prop:"sn"},
{label:"CPU(核)", prop:"cpu"},
{label:"内存(G)", prop:"memory"},
{label:"存储(G)", prop:"disk"},
{label:"资产类型", prop:"device_type_name"},
{label:"资产状态", prop:"device_status_name"},
{label:"所属环境", prop:"device_env_type_name"},
{label:"房间", prop:"root"},
{label:"设备用途", prop:"purpose"},
{label:"机架位", prop:"rack_position"},
{label:"U位", prop:"u_position"},
{label:"所属IDC机房", prop:"idc_name"},
{label:"设备型号", prop:"equipment_type"},
{label:"采购日期", prop:"purchase_date"},
{label:"所属系统类别", prop:"sys_name"},
{label:"所属二级产品线", prop:"second_name"},
{label:"所属物理机", prop:"parent_asset_name"},
{label:"创建时间", prop:"create_at"}
] export default {
data() {
return {
url: "asset",
key: 1, // table key
formThead: fields, // 默认表头 Default header
data: []
};
}, methods: {
/**
* 遍历列的所有内容,获取最宽一列的宽度
* @param arr
*/
getMaxLength (arr) {
return arr.reduce((acc, item) => {
if (item) {
let calcLen = this.getTextWidth(item)
if (acc < calcLen) {
acc = calcLen
}
}
return acc
}, 0)
},
/**
* 使用span标签包裹内容,然后计算span的宽度 width: px
* @param valArr
*/
getTextWidth(str) {
let width = 0;
let html = document.createElement('span');
html.innerText = str;
html.className = 'getTextWidth';
document.querySelector('body').appendChild(html);
width = document.querySelector('.getTextWidth').offsetWidth;
document.querySelector('.getTextWidth').remove();
return width;
}
}, watch: {
/**
* 监控表格的数据data,自动设置表格宽度
* @param valArr
*/
data(valArr) {
const _this = this
this.formThead = fields.map(function (value) {
const arr = valArr.map(x => x[value.prop]) // 获取每一列的所有数据
arr.push(value.label) // 把每列的表头也加进去算
value.width = _this.getMaxLength(arr) + 20 // 每列内容最大的宽度 + 表格的内间距(依据实际情况而定)
return value
})
}
}
}
</script>
后端返回的表格数据结构
[
{
asset_name: "交换机"
cpu: null
create_at: "2019-12-03"
device_env_type_id: 1
device_env_type_name: "准生产环境"
device_status_id: 1
device_status_name: "上架"
device_type_id: 1
device_type_name: "物理服务器"
disk: null
equipment_type: null
id: 10
idc: null
ipaddress: null
latest_date: null
memory: null
oa_jon_num: null
parent_asset: null
purchase_date: null
purpose: null
rack_position: null
root: null
second_business: null
sn: null
switch: null
sys_type: null
},
...
...
]
vue el-table 自适应表格内容宽度的更多相关文章
- input文本框自适应文本内容宽度
input文本框自适应文本内容宽度 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- UIWebView中加载HTML的Table,td设置百分比宽度并且宽度不被里面的内容撑开
正常情况下,iOS使用WebView加载HTML的Table时,为了让Table适应屏幕宽度,会使用百分比设置td的宽度,但是由于td中的内容过多,td会被撑开,导致整个Table的宽度会变宽,超出屏 ...
- div 内容宽度自适应、超出后换行
div 内容宽度自适应,超出后换行 { max-width:100%;width: fit-content;width: -webkit-fit-content;width: -moz-fit-con ...
- Vue 表格内容根据后台返回状态位填充文字
本文地址:http://www.cnblogs.com/veinyin/p/8534365.html Vue 做表格时我们常用的就是 v-for ,直接把 prop 绑定上去,但是如果表格内容需要我 ...
- Vue. 之 Element table 单元格内容隐藏
Vue. 之 Element table 单元格内容隐藏 在table显示数据时,若某个单元格的内容过多,需要进行隐层,在这一列的单元格属性添加::show-overflow-tooltip=&quo ...
- iview table表格内容为数组或者对象的子元素时问题讨论
正常情况下,iview框架table表格内容只需配置好 key 就OK, 稍微复杂点就是用一个reder函数进行操作(params.row 为本行数据) . 以上问题都很好解决,无需太动脑筋. 开发中 ...
- css 固定HTML表格的宽度
在网页中插件表格时,就算你有时定义了宽度,默认的也会根据里面内容的来自动拉伸.有时候自动拉伸是好,但是如果你表格里面的内容太长,表格就会拉伸的特别难看. 像下面的表格,正常的显示应该如下: 但是如果里 ...
- 制作Html标签以及表单、表格内容
制作Html一般用DW(......),Html全称为(Hyper Text Markup Language 超文本标记语言) 文本就是平常电脑上的文本文档,只能存储文字,而超文本文档可以存储音乐 ...
- CSS设置表格TD宽度布局
使用表格布局时,对单元格的宽度控制很伤脑筋,所以查阅资料整理如下: 一.表格布局table-layout 语法: table-layout : auto | fixed 取值: auto : 大多数 ...
随机推荐
- saltstack 基础模块
Salt 在 linux 系统下 基础操作 1.更改权限 # salt 2.更改用户 # salt '172.16.3.9' file.chown /root/test test test 3.复制文 ...
- Resolving EACCES permissions errors when installing packages globally(npm 遇到 write access的问题)
If you see an EACCES error when you try to install a package globally, you can either: Reinstall npm ...
- go type别名和定义类型区别
package main import ( "fmt" ) type person struct { age int name string } func (p person)te ...
- ROS里程计
gmapping导航建图包里建图需要里程计信息,且导航也需要. 整个移动机器人的控制结构如下图所示,其中base_controller节点将订阅的cmd_vel信息通过串口或其它通信接口发送给下位机( ...
- Java 基础:Map的一家
0.Java中的集合框架 1.Map--接口 public interface Map<K,V> 包:java.util.Map Map提供了一种映射关系,其中的元素是以键值对(key-v ...
- 浅析TCP三次握手及四次挥手
1. 三次握手 1. TCP为什么相较于UDP是可靠连接? 可靠连接是指,待通信的两个实体,能够满足通信数据包的有序性.完整性以及可靠性.对于UDP来说, 它的连接过程不需要握手,忽略丢失的数据包,并 ...
- vmware exsi安装部署
本文章参考:https://blog.csdn.net/fishinhouse/article/details/80980051 1.VMware-ESXi-6.5.0镜像下载 网盘链接:https: ...
- java输出程序运行时间
做了一个MapReduce的小练习,想测试一下程序运行时间: 代码: long start = System.currentTimeMillis(); /*运行的程序主体*/ long end = S ...
- JVM 类加载器命名空间深度解析与实例分析
一.创建Sample 1.创建实例 public class MyPerson { private MyPerson myPerson; public void setMyPerson(Object ...
- php异步处理
<?php namespace Index\Controller; use Core\Controller; class test extends Controller { public fun ...