vue3+ts+element-plus穿梭框

7 篇文章 0 订阅
<template>
  <el-dialog
    v-model="dialogVisible"
    title="选址因子配置"
    width="60%"
    :close-on-click-modal="false"
    @close="$emit('close')"
  >
    {{ rightValue }}
    <el-transfer
      v-model="rightValue"
      :props="{ key: 'id', label: 'label' }"
      :data="leftValue"
      filterable
      :filter-method="filterMethod"
      filter-placeholder="请输入关键词搜索"
      @change="getObject"
      :button-texts="['移除因子', '添加因子']"
      :titles="['因子列表', '已选因子']"
    >
    </el-transfer>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm">保存修改</el-button>
        <el-button @click="cancelEdit">取消修改</el-button>
        <el-button @click="resetReason">重置因子</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import Pagination from "../../../components/Pagination/pagination.vue";
import { defineComponent, ref, reactive, toRefs, watch, onMounted } from "vue";
interface dataobj {
  //定义为接口类型,更加详细的定义对象里的每个属性值类型
  leftValue: any[];
  rightValue: number[];
  editLayoutData: any[];
  indexOne: number;
  dialogVisible: boolean;
  confirm: () => any; //确认
  cancelEdit: () => any; //取消修改
  resetReason: () => any; //重置因子
  getObject: (value: any, direction: any, movedKeys: any) => void; //定义为函数类型,入参为any类型:无返参的普通函数
  filterMethod: (query: any, item: any) => boolean;
  //   handle: (item: string) => void;
  //   [propName: string]: any; //该对象还可加入任意属性值类型
}
export default defineComponent({
  components: { Pagination },
  props: {
    visible: {
      type: Boolean,
      dedault: false,
    },
  },
  setup(props) {
    let queryParams = reactive({ pageNum: 1, pageSize: 0 });
    let data = reactive({
      dialogVisible: false,
      leftValue: [],
      rightValue: [],
      editLayoutData: [],
      indexOne: 0,
      //搜索
      confirm: () => {
        data.dialogVisible = false;
        console.log("搜索", data.rightValue);
      },
      //搜索
      filterMethod: (query: any, item: any): boolean => {
        return item.label.indexOf(query) > -1;
      },
      //取消修改
      cancelEdit: (): any => {
        data.dialogVisible = false;
        data.leftValue = [];
        data.rightValue = [];
        data.editLayoutData = [];
        data.indexOne = 0;
      },
      //重置因子
      resetReason: (): any => {
        data.leftValue = [];
        data.rightValue = [];
        data.editLayoutData = [];
        data.indexOne = 0;
      },
      // 右侧列表元素变化时触发
      // value右边框里面所有的key值  direction是方向(left和right),movedKeys是移动的key值
      getObject: (value: any, direction: any, movedKeys: any): void => {
        // 判断移动方向
        if (direction === "right") {
          // 找出key值,然后根据key值找到下标,进行对控制删除显示的键进行修改状态
          movedKeys.map((item: number, index: number) => {
            data.editLayoutData.filter((i, k: number) => {
              if (i.id === item) {
                data.indexOne = k;
              }
            });
            // isUsed是删除,未删除的状态
            data.editLayoutData[data.indexOne].isUsed = 0;
          });
        } else {
          movedKeys.map((item: any) => {
            data.editLayoutData.filter((i, k: number) => {
              if (i.id === item) {
                data.indexOne = k;
              }
            });
            data.editLayoutData[data.indexOne].isUsed = 1;
          });
        }
      },
      //请求
      getList: () => {
        console.log("请求", 914326);
      },
    }) as dataobj;
    watch(
      () => props.visible,
      (val) => {
        if (val) {
          data.dialogVisible = val;
          data.editLayoutData = [
            { key: 1, label: "测试1", id: 1, isUsed: 1 },
            { key: 2, label: "测试2", id: 2, isUsed: 0 },
            { key: 3, label: "测试3", id: 3, isUsed: 1 },
            { key: 4, label: "测试4", id: 4, isUsed: 0 },
          ];
          const allData = data.editLayoutData;
          console.log(allData, "allData");
          const data1 = [];
          for (let i = 0; i < allData.length; i++) {
            data1.push({
              key: allData[i].id,
              label: allData[i].label,
              id: allData[i].id,
              isUsed: allData[i].isUsed,
            });
          }
          console.log(data, "data");
          data1.map((item) => {
            if (item.isUsed === 0) {
              data.rightValue.push(item.key);
            }
          });
          (data.leftValue as any) = data1;
        } else {
          data.editLayoutData = [];
          data.rightValue = [];
          data.leftValue = [];
          //请求
        }
      }
    );
    return {
      ...toRefs(data),
      ...toRefs(queryParams),
    };
  },
});
</script>
<style lang="less" scoped>
/deep/.el-pagination {
  display: flex;
  justify-content: center;
}
.el-dialog {
  width: 100%;
  .el-dialog__footer {
    .dialog-footer {
      display: flex;
      justify-content: center;
    }
  }
  .el-transfer {
    display: flex;
    justify-content: center;
  }
  /* display: inline-block; */
}
/deep/.el-transfer__buttons {
  display: flex;
  flex-direction: column;
}
/deep/.el-transfer-panel {
  width: 40%;
}
/deep/.el-transfer__button:nth-child(2) {
  margin: 0;
}
</style>

在这里插入图片描述

可以使用Element Plus的Transfer组件结合Table组件来实现镶嵌表格穿梭组件。具体步骤如下: 1. 引入Transfer和Table组件 ```javascript import { Transfer, TableColumn } from 'element-plus'; ``` 2. 定义需要穿梭的数据和列配置 ```javascript // 穿梭左侧表格数据 const tableData = [ { id: 1, name: 'John Brown', age: 18, address: 'New York No.1 Lake Park' }, { id: 2, name: 'Jim Green', age: 24, address: 'London No.1 Lake Park' }, { id: 3, name: 'Joe Black', age: 30, address: 'Sydney No.1 Lake Park' }, { id: 4, name: 'Jon Snow', age: 26, address: 'Winterfell No.1 Castle' } ]; // 穿梭左侧表格列配置 const tableColumns = [ { label: '姓名', prop: 'name' }, { label: '年龄', prop: 'age' }, { label: '地址', prop: 'address' } ]; ``` 3. 在模板中使用Transfer和Table组件 ```html <el-transfer v-model="selectedData" :data="tableData" :titles="['可选数据', '已选数据']" > <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> <el-table-column label="地址" prop="address"></el-table-column> </el-transfer> ``` 4. 完整示例代码 ```html <template> <div> <el-transfer v-model="selectedData" :data="tableData" :titles="['可选数据', '已选数据']" > <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> <el-table-column label="地址" prop="address"></el-table-column> </el-transfer> </div> </template> <script> import { Transfer, TableColumn } from 'element-plus'; export default { components: { Transfer, TableColumn }, data() { return { tableData: [ { id: 1, name: 'John Brown', age: 18, address: 'New York No.1 Lake Park' }, { id: 2, name: 'Jim Green', age: 24, address: 'London No.1 Lake Park' }, { id: 3, name: 'Joe Black', age: 30, address: 'Sydney No.1 Lake Park' }, { id: 4, name: 'Jon Snow', age: 26, address: 'Winterfell No.1 Castle' } ], selectedData: [] }; } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值