慕林彬 發表於 2025-7-1 15:11:00

vue vxe-table 自适应列宽,根据内容自适应宽度的2种使用方式

<p>vue vxe-table 自适应列宽,根据内容自适应宽度的2种使用方式</p>
<p>查看官网:https://vxetable.cn<br>
gitbub:https://github.com/x-extends/vxe-table<br>
gitee:https://gitee.com/x-extends/vxe-table</p>
<h2 id="第一种整列根据内容自适应宽度">第一种,整列根据内容自适应宽度</h2>
<p>整列根据内容自适应宽度,包括表头、表体、表尾同时自定义宽度,适用需要完整显示的场景</p>
<p><img src="https://img2024.cnblogs.com/blog/3563285/202507/3563285-20250701151107458-1224862103.png"></p>
<pre><code class="language-html">&lt;template&gt;
&lt;div&gt;
    &lt;vxe-grid v-bind="gridOptions"&gt;&lt;/vxe-grid&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
import { reactive } from 'vue'

const gridOptions = reactive({
border: true,
showFooter: true,
columns: [
    { type: 'seq', width: '80' },
    { field: 'name', title: 'Name', width: 300 },
    { field: 'age', title: 'Age', width: 'auto' },
    { field: 'sex', title: '头部宽度 头部宽度 头部', width: 'auto' },
    { field: 'address', title: 'Address', width: 'auto' }
],
data: [
    { id: 10001, name: 'Test1', role: 'Develop Develop Develop ', sex: 'Man', age: 28, address: '内容宽度' },
    { id: 10002, name: 'Test2', role: 'Test Test Test Test Test Test Test', sex: 'Women', age: 22, address: '内容宽度 内容宽度 内容宽度 内容宽度 内容宽度 内容宽度' },
    { id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: '内容宽度 内容宽度 内容宽度 内容' }
],
footerData: [
    { age: '9999999' }
]
})
&lt;/script&gt;
</code></pre>
<h2 id="第二种仅表体单元格根据内容自适应宽度">第二种,仅表体单元格根据内容自适应宽度</h2>
<p>仅表体单元格根据内容自适应宽度,表头和表尾不根据内容自适应宽度,适用内容不宽的字段就非常合适了<br>
通过 columnConfig.autoOptions 来配置</p>
<p><img src="https://img2024.cnblogs.com/blog/3563285/202507/3563285-20250701151115220-1700804019.png"></p>
<pre><code class="language-html">&lt;template&gt;
&lt;div&gt;
    &lt;vxe-grid v-bind="gridOptions"&gt;&lt;/vxe-grid&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
import { reactive } from 'vue'

const gridOptions = reactive({
border: true,
showFooter: true,
columnConfig: {
    autoOptions: {
      isCalcHeader: false,
      isCalcBody: true,
      isCalcFooter: false
    }
},
columns: [
    { type: 'seq', width: '80' },
    { field: 'name', title: 'Name', width: 300 },
    { field: 'age', title: 'Age', width: 'auto' },
    { field: 'sex', title: '头部宽度 头部宽度 头部', width: 'auto' },
    { field: 'address', title: 'Address', width: 'auto' }
],
data: [
    { id: 10001, name: 'Test1', role: 'Develop Develop Develop ', sex: 'Man', age: 28, address: '内容宽度' },
    { id: 10002, name: 'Test2', role: 'Test Test Test Test Test Test Test', sex: 'Women', age: 22, address: '内容宽度 内容宽度 内容宽度 内容宽度 内容宽度 内容宽度' },
    { id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: '内容宽度 内容宽度 内容宽度 内容' }
],
footerData: [
    { age: '9999999' }
]
})
&lt;/script&gt;
</code></pre>
<p>https://gitee.com/x-extends/vxe-table</p><br><br>
来源:https://www.cnblogs.com/qaz666/p/18959689
頁: [1]
查看完整版本: vue vxe-table 自适应列宽,根据内容自适应宽度的2种使用方式