IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> Vue3分页插件的封装 -> 正文阅读

[JavaScript知识库]Vue3分页插件的封装

分页组件的封装

分页的本质:分批次查询数据(基于页码page和每页条数pagesize),后端接收到分页参数后,会基于这些参数查询数据库,然后基于数据库进行分页,基于SQL语句

分页组件实现的步骤

  • 分页基础的布局,依赖数据分析。
  • 分页内容逻辑,完成切换效果。
  • 接收外部数据,提供分页事件。

落地的代码

  • 分页的基础布局,在src/component/libray中新建一个pagination.vue的文件,基本结构
<template>
  <div class="xtx-pagination">
    <a href="javascript:;" class="disabled">上一页</a>
    <span>...</span>
    <a href="javascript:;" class="active">3</a>
    <a href="javascript:;">4</a>
    <a href="javascript:;">5</a>
    <a href="javascript:;">6</a>
    <a href="javascript:;">7</a>
    <span>...</span>
    <a href="javascript:;">下一页</a>
  </div>
</template>
<script>
export default {
  name: 'XtxPagination'
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>

然后从父组件中传入数据每页的总数据,或者先给一个假的数据

export default {
    name:'XtxPagination',
    props:{
   //一共多少条评论数据
     total: {
      type: Number,
      default: 80
    },
   // 每页有多少条评论的数据
    pagesize: {
      type: Number,
      default: 10
    }
    }
}

然后再setup(){}中导入props,在计算中页数

//Math.ceil()向上取整
const pages=computed(()=>Math.ceil(props.total/props.pagesize))

然后再动态计算一下页码的列表

//获取到当前的页码值
const currentPage = ref(attrs.page || 1)
const list =component(()=>{
    //当父组件传递的total的值发生变化的时候,计算属性会重新计算
    //页码的列表
    const result=[]
    //判断如果总页码小于等于5,大于5就添加到页码列表里面
    if(pages.value<=5){
        //总页码小于等于5的情况下
        for(let i=1;i<pages.value;i++){
            result.push(i)
        }
    }else{
    	//页码大于5的情况下
        if(currentPage.value<=2){
           //左侧的临界值
            for(let i=1;i<=5;i++){
                result.push(i)
            }
        }else if(currentPage.value>=page.value-1){
            //右侧的临界值
            for(let i=pages.value-4;i<=pages.value;i++){
                result.push(i)
            }
        }else{
            //中间的状态
            for(let i=currentPage.value-2;i<=currentPage.value+2;i++){
                result.push(i)
            }
        }
    }
    return result 
})

然后在页面中循环出来,就可以了,代码如下

<template>
  <div class="xtx-pagination">
    <a @click='changePage(false)' href="javascript:;" :class="{disabled: currentPage===1}">上一页</a>
    <span v-if='currentPage > 3'>...</span>
    <a @click='changePage(item)' href="javascript:;" :class='{active: currentPage===item}' v-for='item in list' :key='item'>{{item}}</a>
    <span v-if='currentPage < pages - 2'>...</span>
    <a @click='changePage(true)' href="javascript:;" :class='{disabled: currentPage===pages}'>下一页</a>
  </div>
</template>

点击左右的按钮切换当前的页面

const changePage=(type)=>{
    if(type===false){
        //上一页
        //页面是第一页的时候,禁止点击操作
        if(currentPage.value===1) return
        if(currentPage.value>1){
            currentPage.value-=1
        }
    }else if(type===true){
        //下一页
        //页面是最后一页是,禁止点击操作
        if(currentPAge.value===pages.value)return
        if(currentPage.value<pages.value){
            currentPage.value+=1
        }
    }else{
        //点击页码
        currentPage.value=type
    }
}

然后把currentPage传给父组件,并改变page的值

<!--子组件-->
<script>
	emit('change-page', currentPage.value)
</script>
<!--父组件-->
<XtxPagination @change-page="changePage" />
<script>
	 const changePage = page => {
      // console.log(page)
      // 修改分页参数,重新调用接口
      reqParams.page = page
    }
</script>

详细的代码,再最下面

<template>
  <div class="xtx-pagination">
    <a @click="changePage(false)" href="javascript:;" :class="{ disabled: currentPage === 1 }">上一页</a>
    <span v-if="currentPage > 3">...</span>
    <a
      @click="changePage(item)"
      :class="{ active: currentPage === item }"
      v-for="item in list"
      :key="item"
      href="javascript:;"
      >{{ item }}</a
    >
    <span v-if="currentPage < pages - 1">...</span>
    <a @click="changePage(true)" href="javascript:;" :class="{ disabled: currentPage === pages }">下一页</a>
  </div>
</template>
<script>
import { computed, ref } from 'vue'
export default {
  name: 'XtxPagination',
  props: {
    total: {
      type: Number,
      default: 80
    }
  },
  setup(props, { emit }) {
    // 每页的条数
    const pageSize = 8
    // 总页数
    const pages = Math.ceil(props.total / pageSize)
    // 当前页码
    const currentPage = ref(1)
    // 动态计算中期的页面信息
    const list = computed(() => {
      const result = []
      // 总页码小于5,大于5
      if (pages <= 5) {
        // 页码小于等于5的情况
        for (let i = 1; i <= pages; i++) {
          result.push(i)
        }
      } else {
        // 总页码大于5
        // 判断临界值
        if (currentPage.value <= 2) {
          // 左侧的临界值
          for (let i = 1; i <= 5; i++) {
            result.push(i)
          }
          // 如果当前页码小于等于总页数
        } else if (currentPage.value >= pages - 1) {
          // 右侧的临界值
          for (let i = pages - 4; i <= pages; i++) {
            result.push(i)
          }
        } else {
          // 中间的状态
          for (let i = currentPage.value - 2; i < currentPage.value + 2; i++) {
            result.push(i)
          }
        }
      }
      return result
    })
    const changePage = type => {
      if (type === false) {
        // console.log(n)
        // 上一页
        // 页面是第一页时,禁止点击操作
        if (currentPage.value === 1) return
        if (currentPage.value > 1) {
          currentPage.value -= 1
        }
      } else if (type === true) {
        // 下一页
        // 页面是最后页是,禁止点击操作
        if (currentPage.value === pages) return
        if (currentPage.value < pages) {
          currentPage.value += 1
        }
      } else {
        // 点击页码
        currentPage.value = type
      }
      emit('change-page', currentPage.value)
    }
    return { list, currentPage, changePage, pages }
  }
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333;
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>

父组件

<template>
  <div class="goods-comment">
    <div class="head" v-if="conditions">
      <div class="data">
        <p>
          <span>{{ conditions.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ conditions.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说:</div>
        <div class="dd">
          <a
            @click="toggle(index)"
            :class="{ active: currentIndex === index }"
            v-for="(item, index) in conditions.tags"
            :key="index"
            href="javascript:;"
            >{{ item.title }}({{ item.tagCount }})</a
          >
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序:</span>
      <a :class="{ active: reqParams.sortField === null }" @click="changeSort(null)" href="javascript:;">默认</a>
      <a :class="{ active: reqParams.sortField === 'createTime' }" @click="changeSort('createTime')" href="javascript:;"
        >最新</a
      >
      <a
        :class="{ active: reqParams.sortField === 'praiseCount' }"
        @click="changeSort('praiseCount')"
        href="javascript:;"
        >最热</a
      >
    </div>
    <!-- 评论列表 -->
    <div class="list">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="star in item.score" :key="star" class="iconfont icon-wjx01"></i>
            <i v-for="unstar in 5 - item.score" :key="unstar" class="iconfont icon-wjx02"></i>
            <span class="attr">{{ formatSpec(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 评价图片预览 -->
          <GoodsCommentImage v-if="item.pictures.length" :pictures="item.pictures" />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- <XtxPagination  :total="total" /> -->
    <XtxPagination @change-page="changePage" />
  </div>
</template>
<script>
import { ref, inject, reactive, watch } from 'vue'
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/product.js'
import GoodsCommentImage from './goods-comment-image.vue'

export default {
  name: 'GoodsComment',
  components: { GoodsCommentImage },
  setup () {
    // 筛选条件的列表
    const conditions = ref(null)
    // 需要使用这个数据的字辈组件或者子孙组件中注入数据
    const goods = inject('goods')
    // 获取筛选条件的数据
    findCommentInfoByGoods(goods.value.id).then(ret => {
      // 添加两个假的数据
      ret.result.tags.unshift({
        title: '有图',
        type: 'pic',
        tagCount: ret.result.hasPictureCount
      })
      ret.result.tags.unshift({
        title: '全部评价',
        type: 'all',
        tagCount: ret.result.evaluateCount
      })
      // 然后把从后台获取到的数据赋值给筛选条件列表
      conditions.value = ret.result
    })
    // 当前选中的条件的索引
    const currentIndex = ref(0)
    // 点击事件,并传入索引值
    const toggle = index => {
      currentIndex.value = index
      // 根据索引,获取当前点击的标签的对象信息
      const info = conditions.value.tags[index]
      if (info.type === 'pic') {
        // 点击有图的话,就显示图片,筛序条件为空
        reqParams.hasPicture = true
        reqParams.tag = null
      } else if (info.type === 'all') {
        // 点击全部评价,显示图片为空,筛序条件也为空
        reqParams.hasPicture = null
        reqParams.tag = null
      } else {
        // 点击其他四个标签之一,显示图片为空,把从后台获取到的筛选条件赋值给筛序条件
        reqParams.hasPicture = null
        reqParams.tag = info.title
      }
      // 每次修改条件,重置页码
      reqParams.page = 1
    }
    // 获取评论列表数据
    const list = ref([])
    const total = ref(0)
    // 筛选条件准备
    const reqParams = reactive({
      // 当前页码
      page: 1,
      // 每页的条数
      pageSize: 10,
      // 是否有图片
      hasPicture: null,
      // 筛选条件
      tag: null,
      // 排序的字段
      sortField: null
    })
    // 侦听参数的变化,当获取评论列表数据发生变化的时候
    watch(
      reqParams,
      () => {
        findCommentListByGoods(goods.value.id, reqParams).then(ret => {
          // 把获取到的总页数赋值给total
          total.value = ret.result.counts
          // 把获取到的评论列表赋值给列表数据
          list.value = ret.result.items
        })
      },
      {
        immediate: true
      }
    )
    // 控制排序条件的切换
    const changeSort = type => {
      // console.log(type)
      // 把传入的排序字段赋值给排序的字段
      reqParams.sortField = type
      // 每点击一下排序,必须返回到第一页
      reqParams.page = 1
    }
    // 格式化规格信息
    const formatSpec = specs => {
      return specs.reduce((ret, item) => ret + item.name + ':' + item.nameValue + ' ', '')
    }
    // 格式化昵称
    const formatNickname = nickname => {
      return nickname.substr(0, 1) + '****' + nickname.substr(-1)
    }
    // 控制页码的配置
    const changePage = page => {
      // console.log(page)
      // 修改分页参数,重新调用接口
      reqParams.page = page
    }
    return {
      changePage,
      total,
      formatNickname,
      formatSpec,
      list,
      conditions,
      currentIndex,
      toggle,
      reqParams,
      changeSort
    }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-24 10:26:52  更:2021-09-24 10:29:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 22:06:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码