vue3中的el-table-column实现拖动调整列宽,并且可以保存调整后的列宽。

首先先把表单给写出来,我是通过循环出的表单,如果你们不是还需要按照自己的写法来改一下。

<el-table>标签中添加@header-dragend事件,用于监听列宽调整的结束事件。

<el-table
        :data="tableData"
        border
        @header-dragend="handleHeaderDragend"
    >
      <el-table-column type="selection" width="55"/>
      <el-table-column
          v-for="column in columnConfig"
          :key="column.property"
          :prop="column.property"
          :label="column.label"
          :width="column.width"
          :sortable="column.sortable"
          :show-overflow-tooltip="column.showOverflowTooltip"
      >
        <!-- 使用具名插槽来自定义 category_id 列的显示 -->
        <template v-if="column.property === 'category_id'" #default="scope">
          <el-tag>{{ scope.row.category_id }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="right" label="操作" width="70">
        <template #default="scope">
          <el-icon class="hand-cursor mr-1" @click="handleEdit(scope.row.id)">
            <View/>
          </el-icon>
          <el-icon @click="handleDelete(scope.row.id)" class="hand-cursor">
            <Delete/>
          </el-icon>
        </template>
      </el-table-column>
    </el-table>

再写出一个被循环的数组(我没有使用TS,原因可以看我以前写的:TS已被抛弃):

这个比较简单就不多说了,只是一个被循环的数据。

const columnConfig = ref([
  // 示例列配置,你需要根据你的实际需求来设置这些值
  {property: 'name', width: 'defaultWidth', label: '资产', sortable: true, showOverflowTooltip: false},
  {property: 'category_id', width: 'defaultWidth', label: '分类', sortable: true, showOverflowTooltip: false},
  {property: 'vest_name', width: 'defaultWidth', label: '主体', sortable: true, showOverflowTooltip: false},
  {property: 'area', width: 'defaultWidth', label: '区域', sortable: true, showOverflowTooltip: true},
  {property: 'description', width: 'defaultWidth', label: '描述', sortable: false, showOverflowTooltip: true},
  {property: 'location', width: 'defaultWidth', label: '位置', sortable: false, showOverflowTooltip: true},
]);

然后,添加handleHeaderDragend方法,用于保存调整后的列宽。

const handleHeaderDragend = (newWidth, oldWidth, column, event) => {
  console.log(newWidth, oldWidth, column, event)
  const columnWidths = JSON.parse(localStorage.getItem('columnWidths')) || {};
  columnWidths[column.property] = newWidth;

  // 保存列宽度到本地存储
  localStorage.setItem('columnWidths', JSON.stringify(columnWidths));
}

接下来,在onMounted中,获取之前保存的列宽,并将其应用到相应的列上。

import {onMounted} from "vue";//一定记得要引入一下

onMounted(async () => {
  // 获取本地存储中保存的列宽度
  const columnWidths = JSON.parse(localStorage.getItem('columnWidths')) || {};

  // 遍历本地的表格列配置,将保存的列宽度应用到对应的列上
  columnConfig.value.forEach(column => {
    if (columnWidths[column.property]) {
      column.width = columnWidths[column.property];
    }
  });

  await getData()//这里是我获取列表参数的方法,需要按照自己的方式改一下!(不要全抄!)
});

目前到这里就结束了。但是如果你觉得这样对你来说有点丑,你既要可以拖动的功能,又要下面的表格不要边框。你可以在css中加入这段代码:

.el-table--border .el-table__body td,
.el-table__header th { //不要数据的边框
//.el-table--border .el-table__header th {//什么边框都不要
  //border-color: transparent;
  border-right: transparent;
  border-left: transparent;
}

实现 el-table拖动排序,可以借助 Vue3 新增的 Composition API 和 el-table 提供的 sortable 属性。 首先,需要在 el-table 设置 sortable 属性为 true,表示开启排序功能: ```html <el-table :data="tableData" :sortable="true"> <!-- 表头和表格内容 --> </el-table> ``` 然后,在 setup 函数使用 ref 和 reactive 创建响应式数据,并监听 el-table 的排序事件,将排序后的数据更新到响应式数据: ```js import { ref, reactive } from 'vue'; export default { setup() { // 创建响应式数据 const tableData = reactive({ list: [ { name: '张三', age: 20, score: 90 }, { name: '李四', age: 22, score: 85 }, { name: '王五', age: 21, score: 95 }, ], }); // 监听 el-table 的排序事件 const handleSortChange = ({ prop, order }) => { tableData.list.sort((a, b) => { const valA = a[prop]; const valB = b[prop]; if (order === 'ascending') { return valA > valB ? 1 : -1; } else { return valA < valB ? 1 : -1; } }); }; return { tableData, handleSortChange, }; }, }; ``` 最后,在 el-table-column 设置 sortable 属性为 'custom',并绑定 handleSortChange 方法,即可实现拖动排序: ```html <el-table :data="tableData.list" :sortable="true" @sort-change="handleSortChange"> <el-table-column label="姓名" prop="name" sortable="custom"></el-table-column> <el-table-column label="年龄" prop="age" sortable="custom"></el-table-column> <el-table-column label="分数" prop="score" sortable="custom"></el-table-column> </el-table> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值