el-transfer 穿梭框

这篇博客详细介绍了如何在Vue.js应用中使用Element UI库的穿梭框组件(Transfer)。通过示例代码展示了如何初始化数据,监听转移变化,并在用户选择后保存选中项。文章还包含了组件的样式设置和事件处理,如对话框的显示与隐藏、数据的获取与渲染等。
摘要由CSDN通过智能技术生成

效果图如下:

代码如下:

<el-dialog title="下级选择"
      :visible.sync="chooseUser"
      width="60%"
      center>
        <div style="text-align: center;">
          <el-transfer
            ref="roleTransfer"
            style="text-align: left; display: inline-block;height:500px"
            v-model="Selected"
            filterable
            :titles="['可选人员', '已选人员']"
            :format="{
              noChecked: '${total}',
              hasChecked: '${checked}/${total}'
            }"
            @change="handleChange"
            :data="datalist">
            <span slot-scope="{ option }">{{ option.label }}</span>
          </el-transfer>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="reBack()" style="margin:0px 30px 0px 30px">返回</el-button>
          <el-button type="success" @click="saveChoose()">保存</el-button>
        </span>
      </el-dialog>

获取穿梭框列表,渲染到页面:

choose(id){
      this.chooseUser = true
      this.chooseId = id
      setTimeout(async () => {
          let params = {
            roleId:id
          };
          let res = await req.post("/accountLogin/select", params)
          console.log("ress",res.t)
          let data = [];
            let allData = res.t;
            this.Selected = JSON.parse(allData.Selected)
            allData.Optional.forEach((member, index) => {
              data.push({
                label: member.name,
                key:  member.id,
                id: member.id,
              });
            });
          this.datalist = data
          console.log("this.data",this.datalist)
        }, 200);
    },

Methods:

handleChange(val, direction, movedKeys) { //当前变化的值 , 穿梭框左右方向 
      console.log(val, direction, movedKeys)
      console.log("val",val)
      this.selectUser = val
      if (direction === "right") {
        movedKeys.forEach((key) => {
          let index = this.leftvalue.findIndex((item) => item === key);
          this.leftvalue.splice(index, 1);
        });
        movedKeys.forEach((key) => {
          this.rightvalue.push(key);
        });
      } else {
        movedKeys.forEach((key) => {
          let index = this.rightvalue.findIndex((item) => item === key);
          this.rightvalue.splice(index, 1);
        });
        movedKeys.forEach((key) => {
          this.leftvalue.push(key);
        });
      }
      console.log("this.leftvalue",this.leftvalue);
      console.log("this.rightvalue",this.rightvalue);
    },

将val的值用一个变量存起来,点击保存,将存放val的变量通过接口传过去

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值