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

 更新时间:2024年07月12日 09:35:08   作者:斗帝蓝电霸王龙  
有时候需要前端实现上移和下移功能,下面这篇文章主要给大家介绍了关于如何基于前端VUE+ElementUI实现table行上移或下移(支持跨页移动)的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

最近在做后台管理遇见了一个这样的需求: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>

总结 

到此这篇关于前端VUE+ElementUI实现table行上移或下移功能的文章就介绍到这了,更多相关VUE+ElementUI实现table行上移或下移内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue-cli实现多页面多路由的示例代码

    vue-cli实现多页面多路由的示例代码

    本篇文章主要介绍了vue-cli实现多页面多路由的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Vue组合式API的特点及使用方法

    Vue组合式API的特点及使用方法

    在Vue.js 3.0中,推出了新的组合式API,使得开发者能够更加方便灵活地编写Vue组件,这也是组合式 API 成为了 Vue 新的开发范式,与传统的选项 API 相比,组合式 API 更加灵活、易于维护的原因,在本文中,我们将详细介绍 Vue 组合式API的风格及使用
    2023-06-06
  • Vue SSR 即时编译技术的实现

    Vue SSR 即时编译技术的实现

    这篇文章主要介绍了Vue SSR 即时编译技术的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Vue如何获取new Date().getTime()时间戳

    Vue如何获取new Date().getTime()时间戳

    在Web开发中,前端使用Vue.js获取的是毫秒级时间戳,而PHP后端则是秒级时间戳,处理此类问题时,需要将PHP的时间戳乘以1000转换为毫秒级,以保证数据的一致性和正确的逻辑判断
    2024-10-10
  • vue如何在用户要关闭当前网页时弹出提示的实现

    vue如何在用户要关闭当前网页时弹出提示的实现

    这篇文章主要介绍了vue如何在用户要关闭当前网页时弹出提示的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 如何理解Vue的v-model指令的使用方法

    如何理解Vue的v-model指令的使用方法

    这篇文章主要介绍了如何理解Vue的v-model指令的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Vues中使用JavaScript实现路由跳转的步骤详解

    Vues中使用JavaScript实现路由跳转的步骤详解

    在Vue应用中,利用Vue Router进行页面间的导航是一个常见需求,本篇博客将通过示例代码详细介绍如何在Vue组件中使用JavaScript实现路由跳转,需要的朋友可以参考下
    2024-05-05
  • 如何实现双向绑定mvvm的原理实现

    如何实现双向绑定mvvm的原理实现

    这篇文章主要介绍了vue双向数据绑定原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Element table 上下移需求的实现

    Element table 上下移需求的实现

    本文主要介绍了Element table 上下移需求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 关于Vue项目使用scss终端发出警告解决方法

    关于Vue项目使用scss终端发出警告解决方法

    这篇文章主要介绍了关于Vue项目使用scss终端发出警告的解决方法,出现这个问题的原因是项目中使用了DartSass旧版的JavaScriptAPI,这些API已被弃用,文章将解决的办法介绍的非常详细,需要的朋友可以参考下
    2025-04-04

最新评论