当前位置: 首页 > wzjs >正文

网站开放培训结构设计网站推荐

网站开放培训,结构设计网站推荐,上海定制网站建设推广,网站域名怎么改在 Vue3 Element Plus 中实现动态设置表格列宽&#xff0c;可以通过以下几种方式实现&#xff1a; 方法 1&#xff1a;动态绑定 width 属性&#xff08;推荐&#xff09; vue 复制 下载 <template><el-table :data"tableData" style"width: 100%…

在 Vue3 + Element Plus 中实现动态设置表格列宽,可以通过以下几种方式实现:

方法 1:动态绑定 width 属性(推荐)

vue

复制

下载

<template><el-table :data="tableData" style="width: 100%"><!-- 动态绑定 width --><el-table-columnprop="name"label="姓名":width="dynamicWidth"/><!-- 其他列 --></el-table><el-slider v-model="dynamicWidth" :min="100" :max="300" />
</template><script setup>
import { ref } from 'vue';const dynamicWidth = ref(150); // 初始宽度
const tableData = ref([{ name: '张三', age: 30 },{ name: '李四', age: 25 }
]);
</script>

方法 2:使用响应式对象管理列配置

vue

复制

下载

<template><el-table :data="tableData"><el-table-columnv-for="col in columns":key="col.prop":prop="col.prop":label="col.label":width="col.width"/></el-table><el-button @click="resizeColumn">调整第一列宽度</el-button>
</template><script setup>
import { ref } from 'vue';const columns = ref([{ prop: 'name', label: '姓名', width: 120 },{ prop: 'age', label: '年龄', width: 80 }
]);const resizeColumn = () => {columns.value[0].width = Math.floor(Math.random() * 200 + 100);
};
</script>

方法 3:结合拖拽事件动态调整(高级)

vue

复制

下载

<template><el-table :data="tableData"@header-dragend="handleResize"><el-table-columnprop="name"label="姓名(可拖拽)"width="150"resizable/><!-- 其他列 --></el-table>
</template><script setup>
const handleResize = (newWidth, oldWidth, column) => {console.log('列宽变化:', {column: column.label,oldWidth,newWidth});// 这里可以保存新的宽度到数据库或状态管理
};
</script>

方法 4:响应式设置最小/最大宽度

vue

复制

下载

<el-table-columnprop="email"label="邮箱":min-width="100":width="emailWidth"
/>

注意事项:

  1. 单位处理

    • 数字值默认单位为 px

    • 可使用字符串指定单位::width="'20%'"

  2. 性能优化

    vue

    复制

    下载

    <el-table :data="tableData" table-layout="fixed"><!-- 固定布局模式下宽度分配更精确 -->
    </el-table>
  3. 响应式断点

    js

    复制

    下载

    import { useWindowSize } from '@vueuse/core';const { width } = useWindowSize();
    const dynamicWidth = computed(() => {return width.value < 768 ? 100 : 200;
    });
  4. 动态隐藏列

    vue

    复制

    下载

    <el-table-columnv-if="showColumn"prop="address"label="地址"width="200"
    />

完整示例(动态调整 + 保存配置):

vue

复制

下载

<template><el-table :data="tableData" @header-dragend="saveColumnWidth"><el-table-columnv-for="col in columns":key="col.prop"v-bind="col"resizable/></el-table>
</template><script setup>
import { ref, onMounted } from 'vue';// 列配置(从本地存储加载或使用默认值)
const columns = ref([{ prop: 'name', label: '姓名', width: loadWidth('name', 120) },{ prop: 'age', label: '年龄', width: loadWidth('age', 80) },{ prop: 'email', label: '邮箱', width: loadWidth('email', 200) }
]);// 加载保存的宽度
function loadWidth(prop, defaultValue) {const saved = localStorage.getItem(`colWidth_${prop}`);return saved ? parseInt(saved) : defaultValue;
}// 保存列宽
const saveColumnWidth = (newWidth, oldWidth, column) => {const prop = column.property;localStorage.setItem(`colWidth_${prop}`, newWidth);
};// 表格数据
const tableData = ref([...]);
</script>

常见问题解决:

  1. 宽度不生效

    • 确保父容器有明确宽度

    • 添加 CSS:.el-table { table-layout: fixed; }

  2. 拖拽卡顿

    • 减少表格数据量

    • 使用虚拟滚动:<el-table-v2>

  3. 自适应内容宽度

    js

    复制

    下载

    const autoWidth = ref(0);
    onMounted(() => {// 根据内容计算宽度(示例)autoWidth.value = Math.max(...tableData.value.map(d => d.name.length * 10));
    });

选择合适的方法取决于你的具体需求:

  • 简单动态调整:使用方法 1

  • 复杂表格配置:使用方法 2

  • 需要保存用户设置:结合 localStorage 实现

  • 响应式布局:结合窗口尺寸监听


文章转载自:

http://Ix4HJGUK.LzqdL.cn
http://kL0Q9aFU.LzqdL.cn
http://ZP5K2Wbv.LzqdL.cn
http://ahfJZEbs.LzqdL.cn
http://DlD73K5N.LzqdL.cn
http://BEzaJ5oU.LzqdL.cn
http://QBfmxJYP.LzqdL.cn
http://ex9pOvxH.LzqdL.cn
http://CCQaAd52.LzqdL.cn
http://7RyBcWUe.LzqdL.cn
http://2GRZnzLA.LzqdL.cn
http://1RtayW0p.LzqdL.cn
http://Cz9ZBcOH.LzqdL.cn
http://wCNr9HKg.LzqdL.cn
http://NClG5LlZ.LzqdL.cn
http://O2zumM85.LzqdL.cn
http://kIHVEbYk.LzqdL.cn
http://TpnusbYB.LzqdL.cn
http://IwZEX77x.LzqdL.cn
http://JZGEttyJ.LzqdL.cn
http://J9PBtOWW.LzqdL.cn
http://h5RmEk6Z.LzqdL.cn
http://qCoiVHw5.LzqdL.cn
http://ly1Do5Lj.LzqdL.cn
http://CsxF65Ha.LzqdL.cn
http://QNp2su3u.LzqdL.cn
http://EOzuh4Lp.LzqdL.cn
http://3bMpZxaS.LzqdL.cn
http://TmP8NMEV.LzqdL.cn
http://rfllHXiQ.LzqdL.cn
http://www.dtcms.com/wzjs/642698.html

相关文章:

  • 自己做返利网站宜春做网站 黑酷seo
  • 2002年做网站多少钱搜狗站长工具平台
  • 备案用的网站建设方案书怎么写电商网店代运营
  • dw做的网站链接wordpress 小米商城主题
  • 深圳网站建设公司企业名录搜索软件下载
  • 九江做网站哪家便宜做网站建设公司怎么选
  • 石大远程在线考试 《网页设计与网站建设》做网站能创业吗
  • 谷歌地图嵌入网站php班级网站建设
  • 网站域名注册地址wordpress副标题怎么写
  • 织梦网站installwordpress设置分享
  • 网站建设三原则清远住房和城乡建设部网站
  • 专业做财务公司网站直接网址登录wordpress
  • 网站建设实施方式wordpress4.7.2写文章
  • 网站推广页面设计中小企业网站建设与推广分析
  • 女生网站开发关于asp sql网站开发的书籍
  • 网站如何留住客户上海搜索引擎优化seo
  • 制作网站的视频教程tomcat做的网站打不开了
  • 网站备案太久了滕州网站建设培训
  • 济宁有做企业网站吗装修公司名字
  • 湖北住房和城乡建设厅官方网站三网合一网站报价
  • 做网站都需要准备什么软件上海网站建设开发哪家
  • 陕西省建设网官网综合服务中心知乎推广优化
  • 怎么在网站添加链接怎样做一个自己的网站
  • 自己做本地视频网站好人有好报
  • 大港油田建设官方网站平台搭建心得
  • 怎么做网站不会被屏蔽网站色彩
  • 服务器做视频网站网站会更改吗
  • 网站开发的计划书廊坊网站建设-纵横网络+网站
  • 网站建设用什么语言开发wordpress如何在页首添加登录账号
  • 天津手机网站建设制作网站开发数据库技术