el-table + el-pagination 前端实现分页操作

el-table + el-pagination 前端实现分页操作

后端返回全部列表数据,前端进行分页操作

html代码
javascript 复制代码
<div>
  <el-table :data="tableData" border>
    <el-table-column label="序号" type="index" width="50" />
    <el-table-column prop="name" label="礼品名称">
      <template slot-scope="scope">
        {{ scope.row.orderInfo.itemName }}
      </template>
    </el-table-column>
    <el-table-column prop="orderNum" label="订单编号" width="120">
      <template slot-scope="scope">
        {{ scope.row.orderInfo.orderNum }}
      </template>
    </el-table-column>
    <el-table-column prop="name" label="物流编号" width="130">
      <template slot-scope="scope">
        {{ scope.row.orderInfo.expressCode }}
      </template>
    </el-table-column>
    <el-table-column prop="name" label="礼品单价" width="80">
      <template slot-scope="scope">
        {{ scope.row.orderInfo.unitPrice }}
      </template>
    </el-table-column>
    <el-table-column prop="name" label="礼品数量" width="80">
      <template slot-scope="scope">
        {{ scope.row.orderInfo.quantity }}
      </template>
    </el-table-column>
    <el-table-column prop="name" label="订单金额" width="80">
      <template slot-scope="scope">
        {{ scope.row.orderInfo.price }}
      </template>
    </el-table-column>
    <el-table-column prop="name" label="订单日期" width="130">
      <template slot-scope="scope">
        {{ scope.row.orderInfo.orderDate }}
      </template>
    </el-table-column>
    <el-table-column prop="isError" label="是否成功" width="80">
      <template slot-scope="scope">
        {{ scope.row.isError ? '否' : '是' }}
      </template>
    </el-table-column>
    <el-table-column prop="errorInfo" label="错误信息">
      <template slot-scope="scope">
        {{ scope.row.errorInfo }}
      </template>
    </el-table-column>
  </el-table>

  <el-pagination
    :total="total"
    :current-page="page"
    :page-size="size"
    :page-sizes="pageSizes"
    layout="total, sizes, prev, pager, next, jumper"
    @size-change="sizeChange"
    @current-change="currentChange"
  />
</div>
js代码

两种方式:

① slice

② splice

javascript 复制代码
<script>
export default {
  name: 'ImportLog',
  data() {
    return {
      page: 1, // 第几页
      size: 20, // 一页多少条
      total: 0, // 总条目数
      pageSizes: [20, 40, 60, 80, 100, 120, 140, 160, 180, 200], // 可选择的一页多少条
      tableData: [], // 表格绑定的数据
      allData: [], // 全部数据
    }
  },
  created() {
    this.getTabelData2()
  },
  methods: {
    // 获取表格数据,自行分页(slice)
    getTabelData() {
      // allData为全部数据
      this.tableData = this.allData.slice(
        (this.page - 1) * this.size,
        this.page * this.size
      )
      this.total = this.allData.length
    },
    // 获取表格数据,自行分页(splice)
    getTabelData2() {
      const data = JSON.parse(JSON.stringify(this.allData))
      this.tableData = data.splice(
        (this.page - 1) * this.size,
        this.size
      )
      this.total = this.allData.length
    },
    // page改变时的回调函数,参数为当前页码
    currentChange(val) {
      this.page = val
      this.getTabelData2()
    },
    // size改变时回调的函数,参数为当前的size
    sizeChange(val) {
      this.size = val
      this.page = 1
      this.getTabelData2()
    }
  }
}
</script>
相关推荐
光影少年16 分钟前
vue中,created和mounted两个钩子之间调用时差值受什么影响
前端·javascript·vue.js
青苔猿猿19 分钟前
node版本.node版本、npm版本和pnpm版本对应
前端·npm·node.js·pnpm
Ten peaches34 分钟前
Selenium-Java版(操作元素)
java·selenium·测试工具·html
一只码代码的章鱼1 小时前
Spring的 @Validate注解详细分析
前端·spring boot·算法
zimoyin1 小时前
Kotlin 协程实战:实现异步值加载委托,对值进行异步懒初始化
java·前端·kotlin
cdcdhj1 小时前
vue用通过npm的webpack打包编译,这样更适合灵活配置的项目
vue.js·webpack·npm
恋猫de小郭1 小时前
如何查看项目是否支持最新 Android 16K Page Size 一文汇总
android·开发语言·javascript·kotlin
赵大仁2 小时前
React Native 与 Expo
javascript·react native·react.js
程序员与背包客_CoderZ3 小时前
Node.js异步编程——Callback回调函数实现
前端·javascript·node.js·web
非凡ghost3 小时前
Pale Moon:速度优化的Firefox定制浏览器
前端·firefox