左右移动数据vue+elementui

该博客介绍了一个Vue.js实现的页面,展示了如何在两个表格间移动数据,以及上下移动表格内行的功能。用户可以通过点击按钮实现数据在表格间的添加和删除,并在表格内进行上移和下移操作。此外,还提供了选择和保存数据的功能。
摘要由CSDN通过智能技术生成

功能:将两个表格中的数据进行移动,针对内容较多的表格+上下移动的功能。

 

页面

<template>
  <div>
    <el-button  @click="openDialog" icon="el-icon-s-tools" size="small" type="text" class="button-hob"
                style="font-size: 14px;padding: 12px 0;margin-right: 10px" >设置</el-button>
    <el-dialog :visible.sync="dialog.dialogFormVisible" width="1280px" :close-on-click-modal="false">
      <el-form style="height: 400px;text-align: center">
        <el-col :span="9" style="padding-right: 5px;text-align: center">
          <el-table :data="leftData" style="width: 100%" height="400px" border  @selection-change="saveLeft" ref="leftData">
            <el-table-column type="selection"></el-table-column>
            <el-table-column prop="标题一" label="标题一"  width="180px"></el-table-column>
            <el-table-column  prop="标题二" label="标题二"></el-table-column>
          </el-table>
        </el-col>

        <el-col :span="2" style="margin-top: 10%">
          <el-button type="primary" icon="el-icon-arrow-left" style="margin-bottom: 10px" @click="remove" ></el-button>
          <el-button type="primary" @click="add" style="margin-right: 7px"><i class="el-icon-arrow-right" ></i></el-button>
        </el-col>

        <el-col :span="13" style="padding-left: 5px;text-align: center">
          <el-table :data="rightData" style="width: 100%" height="400px" border  ref="rightData" @selection-change="saveRight">
            <el-table-column type="selection"></el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="标题一" label="标题一" width="180px"></el-table-column>
            <el-table-column  prop="标题二" label="标题二" ></el-table-column>
            <el-table-column  prop="标题三" label="标题三"  >
              <template slot-scope="scope">
                <!--这一列中只能选中一个-->
                <el-checkbox v-model="scope.row.standard"  :checked="scope.row.standard ==1? true : false" @change="selectRow(scope.$index,scope.row)"
                             :true-label="1" :false-label="0"></el-checkbox>
              </template>
            </el-table-column>
            <el-table-column prop="标题四" label="标题四" >
              <template>
                <el-checkbox></el-checkbox>
              </template>
            </el-table-column>
            <el-table-column label="操作" prop="operate">
              <template slot-scope="scope">
                <img src="./上移.png"  @click="update(scope.$index,scope.row)"
                     width="16px" height="16px" style="cursor: pointer;margin-left: 10px"/>
                <img src="./下移.png"  size="small" @click="letDown(scope.$index,scope.row)"
                     width="16px" height="16px" style="cursor: pointer;margin-left: 20px"/>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="dialog.dialogFormVisible = false">保存</el-button>
        <el-button @click="dialog.dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

js操作部分

<script>

  async function openDialog() {
    this.dialog.dialogFormVisible = true;
  }

  function update(index) {    //index: 第几个
    console.log(index)
    let that = this;
    if (index > 0) {
      let upDate = that.rightData[index - 1];  //上一行的数据
      that.rightData.splice(index - 1, 1);
      that.rightData.splice(index, 0, upDate)     //表示从index那个位置插入 upDate
    } else {
      this.$message.warning("已经是第一条,不可上移!")
    }
  }

  function letDown(index,row) {    //index 所在行的下标
    let that = this;
    if ((index +1) === this.rightData.length){
      this.$message.warning("已经是最后一条,不可下移!")
    }else{
      let downDate = that.rightData[index + 1]
      that.rightData.splice(index + 1,1)         //  加入数据之后  下一行会多出来一条上一行的数据   所以把下一行的数据去掉
      that.rightData.splice( index ,0 ,downDate)   //0表示加入数据  必须为0
    }
  }


  function saveLeft(rows) {
    this.left = [];
    if (rows) {
      rows.forEach(row => {
        if (row) {
          this.left.push(row);
        }
      });
    }
  }

  function saveRight(rows) {
    console.log(rows)
    this.right = [];
    if (rows) {
      rows.forEach(row => {
        if (row) {
         this.right.push(row);
        }
      });
    }
  }

  // 左边表格选择项移到右边
  function add() {
    setTimeout(() => {
      this.$refs["leftData"].clearSelection();
      this.$refs["rightData"].clearSelection();
    }, 0);
    this.left.forEach((value) => {
      value.standard = 0;
      value.delete = 0;
      value.position = '';
      console.log(value)
        this.rightData.push(value);
        this.leftData.forEach((item, index) => {
          if (item.headId == value.headId) {
            this.leftData.splice(index, 1)
          }
        })

    })
  }

  // 右边表格选择项移到左边
  function remove() {
    setTimeout(() => {
      this.$refs["leftData"].clearSelection();
      this.$refs["rightData"].clearSelection();
    }, 0);
    this.right.forEach(value=> {
      if (value.standard ==0){
        this.leftData.push(value);
        this.rightData.forEach((item,index)=>{
          if (item.headId == value.headId){
            this.rightData.splice(index,1)
          }
        })
      }else{
        this.$message.warning("标题三选中行无法左移!")
      }
    });
  }

  function selectRow(index,val){  //  val  所在行的信息
      this.rightData.forEach(item=>{
        if (val.headId == item.headId){   //点击的那一行的id和遍历的数据中的id对应   确定是哪一行
          item.standard= 1;
        }else{
          item.standard=0;
        }
    })
  }

  export default {
    data(){
      return{
        rightData: [{标题一: "标题一1",标题二: "标题二1",标题三: "标题三1",standard: 0,delete:0,position:1,headId:111},
          {标题一: "标题一2", 标题二: "标题二2", 标题三: "标题三2", standard: 0, delete:1, position:2, headId: 222},
          {标题一: "标题一3", 标题二: "标题二3", 标题三: "标题三3", standard: 0, delete:0, position:3, headId: 333},
          {标题一: "标题一4", 标题二: "标题二4", 标题三: "标题三4", standard: 1, delete:1, position:4, headId: 444}],
        leftData: [],
        right: [],   //右边选中的数据
        left: [],  //左边选中的数据
        dialog: {
          dialogFormVisible: false,
        },
      }
    },
    methods:{
      openDialog,
      update,
      letDown,
      remove,
      add,
      selectRow,
      saveLeft,
      saveRight,
    }
  }

</script>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现左右列表联动,可以使用 ElementUI 的 Transfer 组件。该组件提供了两个列表,左边为未选中的数据列表,右边为已选中的数据列表,可以通过拖拽或者点击按钮来进行数据移动。 具体实现步骤如下: 1. 引入 ElementUI 的 Transfer 组件和相关样式文件。 ``` import { Transfer } from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' ``` 2. 在模板中使用 Transfer 组件,并传入需要展示的数据列表和相关配置项。 ``` <template> <el-transfer :data="dataList" :left-default-checked="[1, 3]" :right-default-checked="[2, 4]" :titles="['未选中', '已选中']" :button-texts="['>', '<']" :filterable="true" filter-placeholder="请输入关键字" :format="format" @change="handleChange" /> </template> ``` 其中,dataList 表示数据列表,left-default-checked 和 right-default-checked 表示默认选中的数据项,titles 表示左右两个列表的标题,button-texts 表示操作按钮的文字,filterable 表示是否支持搜索过滤,filter-placeholder 表示搜索框的提示文本,format 是一个函数,用来格式化每个选项的展示文本,handleChange 是组件的 change 事件回调函数。 3. 在脚本中定义 dataList 数据列表,并实现 format 和 handleChange 函数。 ``` <script> export default { data() { return { dataList: [ { key: 1, label: '选项1' }, { key: 2, label: '选项2' }, { key: 3, label: '选项3' }, { key: 4, label: '选项4' } ] } }, methods: { format({ label, key }) { return `${label} (${key})` }, handleChange(targetKeys, direction, moveKeys) { console.log(targetKeys, direction, moveKeys) } } } </script> ``` 其中,format 函数接收一个参数,该参数包含当前选项的 label 和 key,需要返回一个字符串,用来展示在列表中。handleChange 函数接收三个参数,分别为目标列表的 key 数组,操作类型和移动的 key 数组。 这样,就可以实现左右列表联动的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值