当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue+iview实现分页及查询功能

vue+iview实现分页及查询功能

2020年11月17日  | 移动技术网IT编程  | 我要评论
vue+iview 分页及删、查功能实现首先要想实现分页功能必须得知道 当前页码、每页大小、总数目。iview对分页的功能支持还是很强大的,有很多钩子函数具体实现看后端返回的数据<templat

vue+iview 分页及删、查功能实现

首先要想实现分页功能必须得知道 当前页码、每页大小、总数目。

iview对分页的功能支持还是很强大的,有很多钩子函数

具体实现看后端返回的数据

<template>
 <div v-if="this.$store.state.user.usertype == 0 || this.$store.state.user.usertype == 1">
  <input type="text" search enter-button placeholder="根据施工人员姓名查找" v-model="peoplename" @input="search"/>
  <table width="100%" :columns="peoplecol" :data="peopledat"></table>
  <!--通过sync修饰符可以动态获取页码-->
  <page :total="datacount" :current.sync="current" :page-size="pagesize" show-total class="paging" @on-change="changepage"></page>
  
  <!--该modal是删除提醒框-->
  <modal v-model="confirmdelete" width="360">
   <p slot="header" style="color:#f60;text-align:center">
    <icon type="ios-information-circle"></icon>
    <span>删除确认</span>
   </p>
   <div style="text-align:center">
    <p>此操作不可恢复,确定要删除吗?</p>
   </div>
   <div slot="footer">
    <button size="large" @click="canceldelete">取消</button>
    <button type="error" size="large" @click="deleteconfirm">删除</button>
   </div>
  </modal>
 </div>
</template>
<script>
 export default {
  components: {
   addworker,
   updateworker
  },
  data () {
   return {
    selectedid:'',//删除选中的id
    confirmdelete:false,//删除提示框
    current:1,
    isshow:false,
    selectedlist:{},//选中施工人员的id值
    peoplename:'',
    datacount:0,//总条数
    pagesize:2,//每页显示数据条数
    peopledat: [],
    peoplecol: [
     {
      title: '操作',
      key: 'action',
      width: 120,
      render: (h, params) => {
       return h('button', {
         props: {
          type: 'error',
          size: 'small'
         },
         on:{
          click: ()=>{
           this.confirmdelete=true
           this.delete(params.row.peopleid)
          }
         }
        }, '删除')
      }
     }
    ],
   }
  },
  mounted() {
   this.getworkerlist()
  },
  methods:{
   getworkerlist(){//组件初始化显示的数据
    const currpage=1
    const pagesize=this.pagesize
    //下面是向后台发送请求
    settimeout(async()=>{
     const r=await getworkers(currpage,pagesize)
     if(r.data.success){
      this.datacount=r.data.list.count//初始化总条数
      this.peopledat=r.data.list.data//默认页列表渲染数据
      console.log(r)
     }
    })
   },
   changepage(index){//页码改变触发的函数
    //index当前页码
    const currpage=index
    const pagesize=this.pagesize
    settimeout(async ()=>{
     const r=await changepage(currpage,pagesize)
     if(r.data.success){
      this.peopledat=r.data.list.data//当前页列表数据
     }
    })
   },
   search(){
    const peoplename=this.peoplename
    const pagesize=this.pagesize
    settimeout(async()=>{
     const r=await search(peoplename,pagesize)
     if(r.data.success){
      this.peopledat=r.data.list.data
      this.datacount=r.data.list.count//如果不设置总条数那么当精确查询时每页都会有数据这得看后端返回的数据有没有这些数据
     } else {
      this.$message.warning('查询失败!')
     }
    })
   },
   delete(peopleid){
    this.selectedid=peopleid
   },
   deleteconfirm(){
    const id=this.selectedid
    settimeout(async ()=>{
     const r=await deleteworker(id)
     if(r.data.success){
      //这里做的一个功能是当你删除某页数据后立即刷新当前页的数据
      this.changepage(this.current)//更新当前页码的数据
      this.$message.success('删除成功!')
     } else{
      this.$message.warning('删除失败!')
     }
    })
    this.confirmdelete=false
   },
   canceldelete(){
    this.confirmdelete=false
    this.$message.info('你取消了删除操作')
   }
  }
 }
</script>
<style scoped>
 .paging{
  float:left;
  margin-top:10px;
 }
</style>

关于vue.js的学习教程,请大家点击专题vue.js组件学习教程vue.js前端组件学习教程进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网