node+mysql+vue+element实现后台分页

11 篇文章 0 订阅
7 篇文章 0 订阅

有两万多条数据 之前用的前端分页 数据量一大就不好办了 挺卡的 速度就慢很多了

 

所以这边就考虑后端分页 一次请求10次数据

上代码吧

后端对应上接口

app.post('/Work/List1/page=:page&pageNum=:pageNum', async (req, res) => {

    const {page,pageNum} = req.params;

    let sql = `SELECT * FROM kmp_url LIMIT ${pageNum} offset ${page+0} `;

    // console.log(sql);

    connection.query(sql, function (err, result) {

        if (err) {

            console.log('[SELECT ERROR] - ', err.message);

            return;

        }

        res.send(result);

    });

});

前端使用了element分页器

直接上代码

<template>
  <div>
    <el-table :data="WorkList2" border style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="ywid" label="业务id"></el-table-column>
      <el-table-column prop="gdname" label="工单名称"></el-table-column>
      <el-table-column prop="ucode" label="网址" width="280"></el-table-column>
      <el-table-column prop="host" label="iP"></el-table-column>
      <el-table-column prop="uurl" label="App"></el-table-column>
      <el-table-column prop="status" label="关键字"></el-table-column>
      <el-table-column prop="province" label="公众号"></el-table-column>
      <el-table-column prop="city" label="省份"></el-table-column>
      <el-table-column label="创建时间" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.time | date }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="$router.push(`/WorkEdit/${scope.row.gdid}`)"
          >编辑</el-button>
          <el-button type="danger" size="small" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row :span="24">
      <div class="pagination-list">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="paginations.page_index"
          :page-size="paginations.page_size"
          layout="total, prev, pager, next, jumper"
          :total="length"
          style="margin-top: 20px;text-align: center;"
        ></el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  filters: {
    date(val) {
      // 过滤器过滤时间格式
      return dayjs(val).format("YYYY-MM-DD HH:mm");
    }
  },
  data() {
    return {
      WorkList: [], //数据存放
      WorkList2: [],
      length: 0,
      paginations: {
        page_index: 1,
        total: 0,
        page_size: 10, //一页显示几条
        layout: "prev, pager, next"
      }
    };
  },
  methods: {
    async handleCurrentChange(page) {
      const list = await this.$http.post(
        `/Work/List1/page=${page}&pageNum=${this.paginations.page_size}`
      );
      this.WorkList2 = list.data;
      //   let index = this.paginations.page_size * (page - 1);
      //   let items_num = this.paginations.page_size * page;
      //   let tables = [];
      //   for (let i = index; i < items_num; i++) {
      //     if (this.WorkList2[i]) {
      //       tables.push(this.WorkList2[i]);
      //     }
      //     this.WorkList = tables;
      //   }
    },
    async fetch() {
      const list = await this.$http.post(`/Work/List1/page=1&pageNum=10`);
      this.WorkList2 = list.data;
      //   const res = await this.$http.get("/Work/List1");
      //   this.WorkList2 = res.data;
      const lengths = await this.$http.get("/Work/List/Count");
      this.length = lengths.data;
    }
  },
  created() {
    this.fetch();
  },
  watch: {
    search: function(new_v) {
      if (new_v != "") {
        this.searchItem();
      } else {
        this.fetch();
      }
    }
  }
};
</script>

<style>
.search {
  width: 250px;
  margin-right: 20px;
  margin-bottom: 20px;
}
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值