element表格穿梭框

<el-row :gutter="20" class="mt-20">
     <el-col :span="11">
         <el-table
         :row-key="getRowKey"
             ref="staffTable"
             v-loading="listLoading"
             :data="staffList"
             border
             fit
             highlight-current-row
             @selection-change="handleStaffChange"
         >
             <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
             <el-table-column label="手机" align="center">
             <template slot-scope="{row}">
                 <span>{{ row.phone }}</span>
             </template>
             </el-table-column>
 
             <el-table-column label="昵称" align="center">
             <template slot-scope="{row}">
                 <span>{{ row.nickName }}</span>
             </template>
             </el-table-column>
         </el-table>
     </el-col>
     <el-col :span="1" style="text-align:center;">
         <el-button
             @click="addStaff"
             type="primary"
             :disabled="!staffData.length"
             icon="el-icon-arrow-right"
             circle
         ></el-button>
         <el-button
             @click="removeStaff"
             type="primary"
             :disabled="!selectedStaffData.length"
             icon="el-icon-arrow-left"
             circle
             style="margin-left: 0;margin-top: 10px;"
         ></el-button>
     </el-col>
     <el-col :span="11">
         <el-table
         :row-key="getRowKey"
             ref="selectedStaffTable"
             v-loading="listLoading"
             :data="selectedStaffList"
             border
             fit
             highlight-current-row
             @selection-change="handleSelectedStaffChange"
         >
             <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
             <el-table-column label="手机" align="center">
             <template slot-scope="{row}">
                 <span>{{ row.phone }}</span>
             </template>
             </el-table-column>
 
             <el-table-column label="昵称" align="center">
             <template slot-scope="{row}">
                 <span>{{ row.nickName }}</span>
             </template>
             </el-table-column>
 
              
         </el-table>
     </el-col>
 </el-row>
 <el-button  @click="modifyStaff()"  type="primary"  size="small">提交</el-button>

data() {
            return {
                listLoading: true,
                staffTemp: {
                    phone: "",
                    nickName: "",
                    staffTypeId: ""
                },
                staffList: [
                    {
                        "phone":'12723456789',
                        "nickName":"dsgshrt"
                    },
                    {
                        "phone":'98765432',
                        "nickName":"dsgshrt"
                    }  
                ],
                selectedStaffList: [],
                staffData: [],
                selectedStaffData: [],
                tableKey: 0,
                rowKey: "rowKey",
                staffOptions: [
                    { key: 28, display_name: "补货员" },
                    { key: 29, display_name: "测试员" }
                ],
          }
   }
getRowKey(row){
                return row.id;
            }, 
            // 将左边表格选择项存入staffData中
            handleStaffChange(rows) {
                this.staffData = [];
                if (rows) {
                    rows.forEach(row => {
                    if (row) {
                        this.staffData.push(row);
                    }
                    });
                }
            },
            // 左边表格选择项移到右边
            addStaff() {
                setTimeout(() => {
                    this.$refs["staffTable"].clearSelection();
                    this.$refs["selectedStaffTable"].clearSelection();
                }, 0);
                let repeat = false;
                this.selectedStaffList.forEach(item => {
                    if (this.staffData[0] && item.phone === this.staffData[0].phone) {
                    repeat = true;
                    alert("此员工已添加");
                    return;
                    }
                });
                if (repeat === false) {
                    this.staffData.forEach(item => {
                    this.selectedStaffList.push(item);
                    });
                    for (let i = 0; i < this.staffList.length; i++) {
                    for (let j = 0; j < this.staffData.length; j++) {
                        if (
                        this.staffList[i] &&
                        this.staffData[j] &&
                        this.staffList[i].phone === this.staffData[j].phone
                        ) {
                        this.staffList.splice(i, 1);
                        }
                    }
                    }
                }
            },
            // 右边表格选择项移到左边
            removeStaff() {
                setTimeout(() => {
                    this.$refs["staffTable"].clearSelection();
                    this.$refs["selectedStaffTable"].clearSelection();
                }, 0);
                this.selectedStaffData.forEach(item => {
                    this.staffList.push(item);
                });
                for (let i = 0; i < this.selectedStaffList.length; i++) {
                    for (let j = 0; j < this.selectedStaffData.length; j++) {
                    if (
                        this.selectedStaffList[i] &&
                        this.selectedStaffData[j] &&
                        this.selectedStaffList[i].phone === this.selectedStaffData[j].phone
                    ) {
                        this.selectedStaffList.splice(i, 1);
                    }
                    }
                }
            },
            // 将右边表格选择项存入selectedStaffData中
            handleSelectedStaffChange(rows) {
                this.selectedStaffData = [];
                if (rows) {
                    rows.forEach(row => {
                    if (row) {
                        this.selectedStaffData.push(row);
                    }
                    });
                }
            },
            // 提交
            modifyStaff() {
                console.log("this.selectedStaffList",this.selectedStaffList)
            },
可以考以下步骤将 Vue3 中的 Element Plus 穿梭转换为表格穿梭: 1. 创建表格及其数据 首先,需要创建一个表格及其数据,例如: ```html <el-table :data="tableData" style="width: 100%"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> ``` ```javascript data() { return { tableData: [ { name: 'John', age: 20, address: 'New York' }, { name: 'Mary', age: 22, address: 'Los Angeles' }, { name: 'Tom', age: 25, address: 'Chicago' } ], leftList: [], rightList: [] }; } ``` 注意,这里还定义了 `leftList` 和 `rightList` 数组,用于存储穿梭的选中项。 2. 创建表格穿梭 接下来,需要创建一个包含两个穿梭和操作按钮的组件,例如: ```html <template> <div class="table-transfer"> <div class="table-transfer-left"> <el-table ref="leftTable" :data="leftList" style="width: 100%" @selection-change="handleSelectionChange('left')" > <el-table-column type="selection"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> </div> <div class="table-transfer-buttons"> <el-button type="primary" icon="arrow-right" @click="transferRight"></el-button> <el-button type="primary" icon="arrow-left" @click="transferLeft"></el-button> </div> <div class="table-transfer-right"> <el-table ref="rightTable" :data="rightList" style="width: 100%" @selection-change="handleSelectionChange('right')" > <el-table-column type="selection"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> </div> </div> </template> <script> export default { data() { return { leftList: [], rightList: [] }; }, methods: { transferRight() { const selection = this.$refs.leftTable.getSelection(); this.leftList = this.leftList.filter(item => !selection.includes(item)); this.rightList = this.rightList.concat(selection); }, transferLeft() { const selection = this.$refs.rightTable.getSelection(); this.rightList = this.rightList.filter(item => !selection.includes(item)); this.leftList = this.leftList.concat(selection); }, handleSelectionChange(type) { const table = type === 'left' ? this.$refs.leftTable : this.$refs.rightTable; const selection = table.getSelection(); this.$set(this, type + 'List', selection); } } }; </script> <style scoped> .table-transfer { display: flex; justify-content: space-between; align-items: center; } .table-transfer-left { width: 45%; } .table-transfer-buttons { display: flex; flex-direction: column; justify-content: center; align-items: center; width: 10%; } .table-transfer-right { width: 45%; } </style> ``` 其中,`leftList` 和 `rightList` 分别代表左侧表格和右侧表格的数据,`transferRight` 和 `transferLeft` 分别代表将选中项从左侧移至右侧和从右侧移至左侧的方法,`handleSelectionChange` 方法用于监听选中项的变化。 3. 将穿梭替换为表格穿梭 最后,将原来的穿梭替换为表格穿梭即可。例如,将原来的代码: ```html <el-transfer v-model="value" :data="list"></el-transfer> ``` 替换为: ```html <TableTransfer :leftList="tableData" :rightList="[]"></TableTransfer> ``` 注意,这里的 `TableTransfer` 是前面创建的表格穿梭组件。同时,传入的 `leftList` 应该是表格的数据,`rightList` 初始值为空数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值