vue+elementUI穿梭框

  • 穿梭框的实现思路,实现了可分页的表格穿梭框,同时涉及到了表格多选与表格里添加表单等知识点
<template>
    <div>

        <h1 style="color: #F56C6C">部门模块</h1>

      <el-form :inline="true" :model="staffTemp">
        <el-form-item label="手机号">
          <el-input v-model="staffTemp.phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getStaffList">查找</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="20">
        <el-col :span="11">
          <el-table
            ref="staffTable"
            v-loading="listLoading"
            :key="tableKey"
            :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="2" 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
            ref="selectedStaffTable"
            v-loading="listLoading"
            :key="tableKey"
            :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-column label="类型" align="center">
              <template slot-scope="{row}">
                <el-select class="filter-item" placeholder="请选择" v-model="row.staffTypeId">
                  <el-option
                    v-for="item in staffOptions"
                    :key="item.key"
                    :label="item.display_name"
                    :value="item.key"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>



    </div>
</template>

<script>

export default {
    name:'department',
  data() {
    return {
      listLoading: false,
      staffTemp: {
        phone: "",
        nickName: "",
        staffTypeId: ""
      },
      staffList: [],
      selectedStaffList: [],
      staffData: [],
      selectedStaffData: [],
      tableKey: 0,
      rowKey: "rowKey",
      staffOptions: [
        { key: 28, display_name: "补货员" },
        { key: 29, display_name: "测试员" }
      ],
    }
  },
  methods: {
    // 从后台获取左边表格的数据
    getStaffList() {
      fetchStaffList(this.staffTemp).then(res => {
        if (res.value.staff.length === 0) {
          alert("查无此人");
        }
        this.staffList = res.value.staff;
      });
    },
    // 将左边表格选择项存入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() {
      let isEmpty = false;
      this.selectedStaffList.forEach(item => {
        if (!item.staffTypeId) {
          alert("请选择类型");
          isEmpty = true;
          return;
        }
      });
      if (isEmpty === false) {
        editStaff(this.selectedStaffList, this.deviceQuery.id).then(res => {
          this.staffListVisible = false;
          this.$notify({
            title: "成功",
            message: "修改成功",
            type: "success",
            duration: 2000
          });
        });
      }
    }
  }
}
</script>

<style>
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YD_1989

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值