基于element-ui的穿梭框(el-transfer)实现点击上下移动(按钮写入每行)

transfer(行内)

<!-- 字段筛选组件 -->
<template>
  <div class="k-field-filter" style="z-index: 99;">
    <div class="field-filter">
      <header>
        <span class="text">设置列表显示字段</span>
        <span class="el-icon-close" @click="closeWindow"></span>
      </header>
      <main class="transfer">
        <el-transfer
                filterable
                filter-placeholder="请输入关键词搜索"
                v-model="value"
                target-order="unshift"
                :titles="['选项合集','已选项合集']"
                :data="data"
                v-loading="loading"
                @right-check-change="choose">
          <div slot-scope="{ option }">
            <div class="default-tranfer-item" @mouseenter="indexKey = option.key"
                 @mouseleave="indexKey = null">
              <span>{{ option.label }}</span>
              <div v-show="value.includes(option.key) && indexKey === option.key" class="button-group">
                <!--  阻止事件冒泡  -->
                <!-- 自定义数据项,将上、下、底部、顶部的排序功能放在数据项里面 -->
                <em class="el-icon-top" @click.stop.prevent="publicMobileMethod('handleUp', option.key)"></em>
                <em class="iconfont icon-huidaodingbu"
                    @click.stop.prevent="publicMobileMethod('handleTop', option.key)"></em>
                <em class="el-icon-bottom" @click.stop.prevent="publicMobileMethod('handleDown', option.key)"></em>
                <em class="el-icon-download" @click.stop.prevent="publicMobileMethod('handleBottom', option.key)"></em>
              </div>
            </div>
          </div>
        </el-transfer>
      </main>
      <footer class="footer">
        <div></div>
        <div>
          <el-button class="cancel" size="mini" @click="cancel">取消</el-button>
          <el-button class="determine" size="mini" @click="determine">确定</el-button>
        </div>
      </footer>
    </div>
  </div>
</template>

<script>

export default {
  name: 'KFieldFilter',
  /**
   * 初始化数据
   * @returns {{item: *[], data: *[], indexKey: null, value: *[]}}
   */
  data () {
    return {
      data: [], // 全部数据
      value: [], // 选中数据
      item: [], // 右侧勾选数据
      indexKey: null, // 高亮显示
    }
  },
  props: {
    cities: {
      type: Array,
      /**
       * 全部数据
       * @returns {*[]}
       */
      default: () => [],
    },
    filterSelected: {
      type: Array,
      /**
       * 右侧数据
       * @returns {*[]}
       */
      default: () => [],
    },
    loading: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    /**
     * 初始数据集
     * @returns {*[]}
     */
    async generateData () {
      const data = []
      this.value = []
      await this.cities.forEach((city) => {
        data.push({
          label: city.field,
          key: city.fieldName,
        })
      })
      this.data = data
      this.value = JSON.parse(JSON.stringify(this.filterSelected))
    },
    /**
     * 关闭弹窗
     */
    closeFilter () {
      this.$emit('closeFilter', false)
    },
    /**
     * 点击x号
     */
    closeWindow () {
      this.cancel()
    },
    /**
     * 确定选择
     */
    determine () {
      this.$emit('determine', this.value)
      this.closeFilter()
    },
    /**
     * 取消选择
     */
    cancel () {
      this.closeFilter()
      this.generateData()
    },
    /**
     * 监听右侧选中
     */
    choose (value) {
      this.item = value
    },
    /**
     * 右侧数据点击排序
     */
    publicMobileMethod (direction, key) {
      const self = this
      let index
      // 判断是否超出一条
      const item = self.item
      if (item.length === 1 || item.length === 0) {
        self.value.forEach((val, indexs) => {
          // 获取需移动数据的下标
          if (val === key) {
            index = indexs
          }
        })
        if (index === 0 && direction !== 'handleBottom' && direction !== 'handleDown') {
          return self.$message('没有上移的空间了')
        }
        if (index === self.value.length - 1 && direction !== 'handleUp' && direction !== 'handleTop') {
          return self.$message('没有下移的空间了')
        }
        // 改变的数组
        const changeItem = self.value[index]
        // 根据下标在数组中删除该数据
        self.value.splice(index, 1)
        // 判断加入数组位置
        if (direction) {
          // 置顶
          direction === 'handleTop' && self.value.unshift(changeItem)
          // 置底
          direction === 'handleBottom' && self.value.push(changeItem)
          // 上移
          direction === 'handleUp' && self.value.splice(index - 1, 0, changeItem)
          // 下移
          direction === 'handleDown' && self.value.splice(index + 1, 0, changeItem)
        }
      } else {
        self.$message.error('只能选择一条数据进行上下移动')
      }
    },
  },
  watch: {
    cities: {
      deep: true,
      immediate: true,
      handler () {
        this.generateData()
      },
    },
  },
  mounted () {
    this.generateData()
  },
}
</script>

<style lang='scss' scoped>
.k-field-filter {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .6);

  .field-filter {
    position: absolute;
    top: 40%;
    left: 50%;
    width: 600px;
    height: 420px;
    overflow-x: auto;
    transform: translate(-50%, -50%);
    background-color: #FFFFFF;
    border: 1px solid rgba(219, 220, 220, 1);
    border-radius: 4px;

    header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 45px;
      padding: 0 13px;
      border-bottom: 1px solid #DBDCDC;
    }

    .text {
      font-family: "Microsoft YaHei", sans-serif;
      font-size: 14px;
      color: #3C4455;
      line-height: 22px;
      font-weight: 700;
    }

    .el-icon-close {
      cursor: pointer;
      color: #B5B5B5;
    }

    .transfer {
      padding: 13px 13px;
      border-bottom: 1px solid #DBDCDC;
    }

    .footer {
      margin: 8px 8px 0 0;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
<style lang="scss">
.field-filter {
  .determine.el-button--mini {
    background-color: #2A76CD;
    color: #FFFFFF;
  }

  .el-transfer__button.cancel,
  .el-button--mini.cancel {
    &:focus,
    &:hover {
      background: #FFFFFF;
      border-color: #2A76CD;
      color: #2A76CD;
    }
  }

  .el-transfer {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-transfer-panel__item {
      margin-right: 0;
    }

    .default-tranfer-item {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .button-group {
        em {
          margin-right: 8px;
        }
      }
    }

    .el-input.el-input--small {
      .el-input__inner {
        border-radius: 4px;
      }
    }

    .el-transfer__buttons {
      padding: 0;
      margin: 0 17px;

      .el-transfer__button {
        display: block;
        margin: 0 0 5px 0;
        padding: 4px 8px;
      }

      .el-button--primary.el-transfer__button {
        display: flex;
        justify-content: center;
        align-items: center;
        background: #2A76CD;
        border-color: #2A76CD;
      }

      .el-button--primary.is-disabled {
        background-color: #a0cfff;
        border-color: #a0cfff;
      }
    }

    .el-checkbox__input.is-indeterminate {
      .el-checkbox__inner {
        background: #2A76CD;
        border-color: #2A76CD;
      }
    }

    .el-transfer-panel {
      width: 49%;
      border-radius: 0;
    }

    .el-transfer-panel__body {
      .el-checkbox__label {
        &:hover {
          color: #2A76CD;
        }
      }
    }

    .el-transfer-panel__header {
      .el-checkbox {
        .el-checkbox__label {
          font-size: 14px;

          span {
            left: 100px
          }
        }
      }
    }

    .el-transfer-panel__footer {
      top: 0;
      left: 61%;
      background-color: transparent;
      display: flex;
      width: 30%;
      justify-content: right;
      border-color: transparent;

      .el-button--text {
        color: #B5B5B5;
        margin-left: 5px;

        .icon-huidaodingbu {
          font-size: 16px;
        }

        em {
          font-size: 14px;
          font-weight: 600;
        }
      }
    }

    .el-transfer-panel:first-child {
      .el-transfer-panel__header {
        .el-checkbox {
          .el-checkbox__label {
            span {
              left: 84px;
            }
          }
        }
      }
    }
  }
}

</style>

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现这个需求,你可以使用 element-ui 架提供的组件和事件。具体步骤如下: 1. 在页面中引入 element-ui 架的 Table 组件和 Button 组件。 2. 创建多个不同的表格组件,并设置不同的数据源和列定义。 3. 在页面中定义多个按钮组件,并为每个按钮绑定一个点击事件。 4. 在点击事件中,根据不同按钮的标识符,动态切换显示对应的表格组件。 示例代码如下: ```html <template> <div> <el-button @click="showTable(1)">显示表格 1</el-button> <el-button @click="showTable(2)">显示表格 2</el-button> <el-button @click="showTable(3)">显示表格 3</el-button> <el-table v-if="showTable1" :data="tableData1" :columns="tableColumns1"></el-table> <el-table v-if="showTable2" :data="tableData2" :columns="tableColumns2"></el-table> <el-table v-if="showTable3" :data="tableData3" :columns="tableColumns3"></el-table> </div> </template> <script> export default { data() { return { showTable1: false, showTable2: false, showTable3: false, tableData1: [{...}], tableColumns1: [{...}], tableData2: [{...}], tableColumns2: [{...}], tableData3: [{...}], tableColumns3: [{...}], } }, methods: { showTable(index) { this.showTable1 = index === 1; this.showTable2 = index === 2; this.showTable3 = index === 3; } } } </script> ``` 在上述代码中,我们定义了三个按钮,分别绑定了 `showTable` 方法并传入不同的参数。在 `showTable` 方法中,根据参数值动态修改三个表格组件是否显示的状态。这样,点击不同的按钮时,就能显示对应的表格组件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值