当前位置:首页 » 《我的小黑屋》 » 正文

前端基于VUE+ElementUI实现table行上移或下移(支持跨页移动)

8 人参与  2024年04月25日 10:59  分类 : 《我的小黑屋》  评论

点击全文阅读


最近在做后台管理遇见了一个这样的需求:table列表需要支持上下移动数据,并且也需要满足跨页移动,前端把数据移动整理之后,提交给后端进行保存(平常这种数据移动都是调用后端的接口,然后在查询数据就可以完成了,但是这次显然不能这么做,因为后端只有一个保存数据的接口,所以这就需要前端自己处理数据了,废话少说,上效果图和源码!

静态效果图

在这里插入图片描述

动态效果图
在这里插入图片描述实现源码(HTML)
  <el-table :data="paginatedData">    <el-table-column label="操作" prop="operate">      <template slot-scope="scope">        <el-button-group>          <el-button            title="下移"            :disabled="isDown(scope.row)"            @click="moveupOrmovedown(scope.row, scope.$index, 'down')"          >          </el-button>          <el-button            title="上移"            :disabled="scope.$index === 0 && currentPage === 1"            @click="moveupOrmovedown(scope.row, scope.$index, 'up')"          >          </el-button>        </el-button-group>      </template>    </el-table-column>  </el-table>  <!-- 页码参考(此处不涉及该功能的任何逻辑,可忽略 -->  <el-pagination     background     :page-size="pageSize"     :current-page="currentPage"     layout="total, prev, pager, next, jumper"     :total="totalSize"     @current-change="(val) => (currentPage = val)"   > </el-pagination>
实现源码(JS)
   moveupOrmovedown(row, index, type) {     let arr = this.filteredData     const findIndex = this.filteredData.findIndex(       (item) => item.date === row.date     )     index = findIndex > this.pageSize - 1 ? findIndex : index     type === 'up'       ? arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1]))       : arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index]))   }
详情源码(仅展示参数)
<script>export default {  data() {    return {      totalSize: 0,      currentPage: 1,      pageSize: 10,      filteredData: [],      paginatedData: [],      tableData: []    }  },  methods: {    isDown(row) {      const findIndex = this.filteredData.findIndex(        (item) => item.date === row.date      )      return findIndex === this.filteredData.length - 1     },    moveupOrmovedown(row, index, type) {      let arr = this.filteredData      const findIndex = this.filteredData.findIndex(        (item) => item.date === row.date      )      index = findIndex > this.pageSize - 1 ? findIndex : index      type === 'up'        ? arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1]))        : arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index]))    },    handleCurrentChange(val) {      this.currentPage = val    },    selectCheckBox(selectCheckBox) {      const newFilterData = this.filterDataByDate(        this.tableData,        selectCheckBox      )      this.filteredData = [...this.filteredData, ...newFilterData]    },    paginateData(data, pageSize, currentPage) {      if (data.length < 11) return data      const startIndex = (currentPage - 1) * pageSize      const endIndex = startIndex + pageSize      const dataToShow = data.slice(startIndex, endIndex)      return dataToShow    },    updatePaginatedData() {      this.totalSize = this.filteredData.length      // 分页(前端处理)     // this.paginatedData = this.$util.paginateData(      this.paginatedData = this.paginateData(        this.filteredData,        this.pageSize,        this.currentPage      )    }  },  created() {    // 调后端接口返回的全部数据(后面前端自己分页)    this.tableData = tableData  },  mounted() {},  watch: {    currentPage: {      handler(newPage) {        this.updatePaginatedData()      },      immediate: true,    },    filteredData: {      handler(newArray) {        this.updatePaginatedData()      },      immediate: true,    }  },  computed: {},  filters: {}}</script>

点击全文阅读


本文链接:http://zhangshiyu.com/post/99914.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 热文推荐很快,他们都自由了:全文+番外(叶浅秋陆凌川)书荒必看列表
  • (新欢闹上门,明小姐消失后司总悔疯了):+后续+番外司锦修明悦后续全本浏览阅读
  • 夕景昨日无心怜冷秋语洛斯言后续(冷秋语洛斯言)全文在线下载在线+纯净版结局
  • 落青梅,佛前几度相思泪:+后续***(宋佳音陆景琛)终章阅读无广告
  • 很快,他们都自由了:结局+番外新上热文(叶浅秋陆凌川)最近章节列表
  • 养妹被豪门认回后做成女体盛,缅北杀神的我杀疯了完结全文_妹妹苏月月宾客优质全文_小说后续在线阅读_无删减免费完结_
  • 京圈太子爷扫黄被抓,我改嫁八块腹肌风流俏奶爸列表_京圈太子爷扫黄被抓,我改嫁八块腹肌风流俏奶爸(许知意顾云夕)
  • (书荒必看)夏晚歌陆秋震惊!玄学大佬被读心了:+后续热血十足
  • 老公逼我给女大献血补身后,他悔疯了前言+后续(沈佳音顾时安)_老公逼我给女大献血补身后,他悔疯了前言+后续
  • 攻略对象害我绝嗣后,她悔疯了节选_林知夏阿宇方宇热文_小说后续在线阅读_无删减免费完结_
  • 如愿娶到假巫女养妹,他却悔疯了超长版_渺渺洛景修沈含霜连载_小说后续在线阅读_无删减免费完结_
  • 完结文京圈太子爷扫黄被抓,我改嫁八块腹肌风流俏奶爸完结列表_完结文京圈太子爷扫黄被抓,我改嫁八块腹肌风流俏奶爸完结(许知意顾云夕)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1