vue3表格排序

上移


    const moveUp = async () => {
      if (eleData.choosedIndex === -1) {
        message.error('请先选择一条数据');
      } else if (eleData.choosedIndex === 0) {
        message.warn('已经是第一条数据了');
      } else {
        eleData.dataSource.splice(
          eleData.choosedIndex - 1,
          1,
          ...eleData.dataSource.splice(
            eleData.choosedIndex,
            1,
            eleData.dataSource[eleData.choosedIndex - 1],
          ),
        );
      }
    };

下移

    const moveDown = () => {
      if (eleData.choosedIndex == -1) {
        message.warn('请先选择一条数据');
      } else if (eleData.choosedIndex == eleData.dataSource.length - 1) {
        message.warn('已经是最后一条数据了');
      } else {
        eleData.dataSource.splice(
          eleData.choosedIndex,
          1,
          ...eleData.dataSource.splice(
            eleData.choosedIndex + 1,
            1,
            eleData.dataSource[eleData.choosedIndex],
          ),
        );
      }
    };

保存顺序

  /** 保存顺序 */
    const saveIndex = async () => {
      const data = eleData.dataSource.map((item: any, index) => {
        item.sort_no = index;
        return item;
      });
      console.log('========item==', data);
      const res: any = await saveElementOrder(
        data,
      );
      if (res.status_code == '0000') {
        message.success('保存顺序成功');
        getTableList(null, null);
      } else {
        message.error(res.reason);
      }
    };

 

Vue3 中,表格的筛选和排序功能通常通过组件化和指令结合数据绑定来实现。以下是基本步骤: 1. 定义表格组件 (Table.vue):使用 `v-model` 指令绑定数据源(可以是数组),并为每一列创建 `<th>` 元素,其中包含 `@click` 事件处理函数,用于触发列头点击事件(筛选或排序)。 ```html <template> <table> <thead> <tr> <th v-for="(column, index) in columns" :key="index" @click="sort(column)"> {{ column.label }} <!-- 添加筛选图标等 --> </th> </tr> </thead> <tbody> <tr v-for="item in filteredItems"> <td>{{ item[column.key] }}</td> <!-- column.key 获取对应索引的数据 --> </tr> </tbody> </table> </template> <script> export default { data() { return { items: [], // 数据源 columns: [], // 列配置 currentSort: {}, // 当前排序规则 }; }, methods: { sort(column) { if (!this.currentSort || this.currentSort.field !== column.key) { this.currentSort = { field: column.key, order: &#39;asc&#39; }; // 默认升序 } else { this.currentSort.order = this.currentSort.order === &#39;asc&#39; ? &#39;desc&#39; : &#39;asc&#39;; // 交换排序方向 } this.filteredItems = this.items.sort((a, b) => { const valueA = a[column.key]; const valueB = b[column.key]; // 实现具体的比较逻辑 // ... }); }, filter(items, column, value) { // 根据当前列名和值过滤数据 // 这里仅示例,实际实现依据需求 this.filteredItems = items.filter(item => item[column.key].includes(value)); }, }, }; </script> ``` 2. 使用动态列头和数据过滤:在表头单元格上添加 `v-if` 条件判断,根据列的 `filterable` 属性动态显示筛选按钮。当用户输入筛选条件时,调用相应的过滤方法(如 `filter`)更新 `filteredItems`。 3. 提供搜索框和下拉框等交互元素,让用户选择筛选条件和排序方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值