左右移动数据vue+elementui

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

 

页面

<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
    评论
Vue Element UI提供了el-pagination组件来实现表格数据的分页功能。以下是实现步骤: 1. 在Vue组件中引入el-pagination组件并定义分页属性 ``` <template> <div> <el-table :data="tableData" border> //表格内容 </el-table> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination> </div> </template> <script> export default { data() { return { tableData: [], //表格数据 total: 0, //总记录数 pageSize: 10, //每页记录数 currentPage: 1 //当前页码 }; }, methods: { handleSizeChange(val) { this.pageSize = val; this.getData(); }, handleCurrentChange(val) { this.currentPage = val; this.getData(); }, getData() { //获取数据,并更新this.tableData和this.total } } }; </script> ``` 2. 在el-pagination组件中绑定事件和属性 - @size-change:当每页显示条数改变时触发的事件,调用handleSizeChange方法更新pageSize并重新获取数据。 - @current-change:当当前页码改变时触发的事件,调用handleCurrentChange方法更新currentPage并重新获取数据。 - :current-page:当前页码,绑定到currentPage属性。 - :page-sizes:每页显示条数数组,可以自定义,默认为[10, 20, 30, 40]。 - :page-size:每页显示条数,绑定到pageSize属性。 - layout:分页组件布局。其中total表示总记录数,sizes表示每页显示条数选择器,prev表示上一页按钮,pager表示页码按钮,next表示下一页按钮,jumper表示跳转输入框和确定按钮。 - :total:总记录数,绑定到total属性。 3. 在getData方法中获取数据并更新表格数据和总记录数 ``` getData() { //计算分页查询的参数 const start = (this.currentPage - 1) * this.pageSize; const limit = this.pageSize; //发起分页查询请求 axios.get('/api/data', { params: { start, limit } }).then(response => { this.tableData = response.data.rows; //更新表格数据 this.total = response.data.total; //更新总记录数 }); } ``` 以上就是使用Vue Element UI实现表格数据分页功能的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值