前端基于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 + Element UI是一个非常流前端开发组合,它结合了Vue.js作为轻量级的MVVM框架和Element UI作为功能强大的UI组件库。下面是如何完善一个基于VueElement UI的前端页面的基本步骤: 1. **安装依赖**: - 首先确保已安装Node.js和npm(Node包管理器)。然后运`npm install vue-element-admin` 或 `yarn add vue-element-admin` 来初始化项目并安装必要的工具。 2. **设置基础结构**: - 创建基本的文件结构,如index.html、main.js、App.vue等,并引入VueElement UI的库文件。 3. **创建组件**: - 使用Element UI提供的各种预设组件,如`el-button`、`el-form`、`el-table`等,构建页面布局和交互控件。可以自定义组件或者使用Element UI的自定义主题进美化。 4. **数据绑定和状态管理**: - 使用Vue的数据双向绑定机制将组件的视图与模型关联起来。对于复杂的业务逻辑,可以考虑使用Vuex进状态管理。 5. **路由配置**: - 引入vue-router,配置路由跳转以及对应的组件显示。Element UI提供了路由懒加载的便捷支持。 6. **样式管理**: - 可能会用到CSS预处理器如Sass或Less,或者使用Vue CLI的 scoped属性配合CSS Modules进样式隔离。 7. **响应式设计**: - 确保页面在不同屏幕尺寸上都能正常工作,这可通过Element UI的响应式组件或第三方库完成。 8. **测试和优化**: - 使用单元测试库如Jest对关键功能进测试,同时关注性能优化,比如图片懒加载、CDN资源引用等。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值