django+elementui实现分页

分页样式自己选,主要是对两个事件的处理
在这里插入图片描述

前端

<template>
  <div>
    <div align="right" style="margin: 10px 20px">
      <el-button type="primary" plain @click="addgoods">添加商品</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column align="center" label="序号">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="商品id">
        <template slot-scope="scope">
          {{ scope.row.pk }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称">
        <template slot-scope="scope">
          {{ scope.row.fields.goods_name }}
        </template>
      </el-table-column>
      <el-table-column label="商品价格">
        <template slot-scope="scope">
          {{ scope.row.fields.goods_price }}
        </template>
      </el-table-column>
      <el-table-column label="实际价格">
        <template slot-scope="scope">
          {{ scope.row.fields.goods_tprice }}
        </template>
      </el-table-column>
      <el-table-column label="商品卖点">
        <template slot-scope="scope">
          {{ scope.row.fields.goods_sell }}
        </template>
      </el-table-column>
      <el-table-column label="Actions" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="editData(row.pk)">
            Edit
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="delData(row.pk)">
            Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[ 10,20, 50, 100]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>

export default {
  data() {
    return {
      tableData: null,
      param: {
        'currentPage': 1, // 当前页
        'pagesize': 10 // 默认每页多少张

      },
      total: 0// 共多少页
    }
  },

  mounted() {
    this.getList()
  },

  methods: {
    // fetchData() {
    //   const vm = this
    //   this.$http.get('http://127.0.0.1:8000/api/goodlist/').then(
    //     (res) => {
    //       if (res.data.code == 0) {
    //         console.log(res.data.code)
    //         vm.tableData = res.data.data
    //       }
    //     }
    //   )
    //     .catch(function(error) {
    //       vm.$message.error('错了哦,这是一条错误消息')
    //       console.log(error)
    //     })
    // },
    getList() {
      const vm = this
      this.$http.post('http://127.0.0.1:8000/api/test/', vm.param).then(
        (res) => {
          if (res.data.code == 0) {
            console.log(res.data.code)
            vm.total = res.data.total
            vm.tableData = res.data.data
          }
        }
      )
        .catch(function(error) {
          vm.$message.error('错了哦,这是一条错误消息')
          console.log(error)
        })
    },
    handleSizeChange: function(size) {
      const a = this
      a.param.pagesize = size
      this.getList()
      console.log(a.param.pagesize) // 每页下拉显示数据
    },
    handleCurrentChange: function(currentPage) {
      const b = this
      b.param.currentPage = currentPage
      console.log(this.currentPage) // 点击第几页
      this.getList()
    },
    editData(pid) {
      this.$router.push('/form/index/' + pid)
    },
    delData(id) {
      console.log(id)
      this.$http.get('http://127.0.0.1:8000/api/delgood?id=' + id).then((res) => {
        console.log(res.data)
        this.fetchData()
        this.$router.push('/goods/table/')
        this.$notify({
          title: '成功',
          message: '恭喜你删除成功',
          type: 'success',
          duration: 1000
        })
      })
        .catch(function(error) {
          this.$message.error('错了哦,这是一条错误消息')
          console.log(error)
        })
    },
    addgoods() {
      this.$router.push('/form/add/')
    }

  }
}
</script>

后端

class test(View):
    def post(self, request):
        res = {'code': 0, 'msg': '添加成功', 'data': []}
        param = request.POST
        pageSize = int(param['pagesize'])
        pageNumber = int(param['currentPage'])
        print(pageNumber)
        total = GoodsInfo.objects.all().count()
        print(total)
        res['total'] = total
        good = GoodsInfo.objects.order_by('id')[(pageNumber - 1) * pageSize:(pageNumber) * pageSize]
        good = json.loads(serializers.serialize('json', good))
        res['data'] = good
        print(res)
        return http.JsonResponse(res)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值