search
HomeWeb Front-endJS TutorialPaging component implemented using vue2.0
Paging component implemented using vue2.0Jun 22, 2017 pm 02:29 PM
Paginationaccomplishcomponents

Recently using vue2.0 to reconstruct the project, I needed to implement a paging table. I didn’t find a suitable paging component, so I wrote one myself. The effect is as follows:

This project is built using vue-cli. If you do not use webpack in your project, please adjust it according to the code:

First create a new pagination.vue file. The codes for all components are written here. Write like this The component is not too difficult. It mainly solves the problem of value transfer between parent and child components.

<template>
  <nav>
    <ul>
      <li><a> « </a></li>
      <li><a> 首页 </a></li>
      <li>
<a> {{ p.text }} </a>
      </li>
      <li><a> 尾页 </a></li>
      <li><a> »</a></li>
    </ul>
  </nav>
</template>

<script>
  export default{
    data(){      return {
        current: this.currentPage
      }
    },
    props: {
      total: {// 数据总条数        type: Number,default: 0  },
      display: {// 每页显示条数        type: Number,default: 10  },
      currentPage: {// 当前页码        type: Number,default: 1  },
      pagegroup: {// 分页条数        type: Number,default: 5,
        coerce: function (v) {
          v = v > 0 ? v : 5;          return v % 2 === 1 ? v : v + 1;
        }
      }
    },
    computed: {
      page: function () { // 总页数return Math.ceil(this.total / this.display);
      },
      grouplist: function () { // 获取分页页码var len = this.page, temp = [], list = [], count = Math.floor(this.pagegroup / 2), center = this.current;if (len <= this.pagegroup) {          while (len--) {
            temp.push({text: this.page - len, val: this.page - len});
          }
          ;          return temp;
        }while (len--) {
          temp.push(this.page - len);
        }
        ;var idx = temp.indexOf(center);
        (idx < count) && ( center = center + count - idx);
        (this.current > this.page - count) && ( center = this.page - count);
        temp = temp.splice(center - count - 1, this.pagegroup);do {          var t = temp.shift();
          list.push({
            text: t,
            val: t
          });
        } while (temp.length);if (this.page > this.pagegroup) {
          (this.current > count + 1) && list.unshift({text: &#39;...&#39;, val: list[0].val - 1});
          (this.current < this.page - count) && list.push({text: &#39;...&#39;, val: list[list.length - 1].val + 1});
        }return list;
      }
    },
    methods: {
      setCurrent: function (idx) {if (this.current != idx && idx > 0 && idx < this.page + 1) {          this.current = idx;          this.$emit(&#39;pagechange&#39;, this.current);
        }
      }
    }
  }</script>

<style>
  .pagination {
    overflow: hidden;
    display: table;
    margin: 0 auto;/*width: 100%;*/height: 50px;

  li {float: left;
    height: 30px;
    border-radius: 5px;
    margin: 3px;
    color: #666;  &
  :hover {
    background: #000;

  a {
    color: #fff;
  }

  }
  a {
    display: block;
    width: 30px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    font-size: 12px;
    border-radius: 5px;
    text-decoration: none
  }

  }
  .active {
    background: #000;

  a {
    color: #fff;
  }

  }
  }</style>

When used, introduce it in the parent component. The code is as follows:

<template>
        <v-pagination></v-pagination>
</template>

<script>
  import pagination from &#39;@/components/common/pagination/pagination&#39;export default{
    data(){ return {
        total: 150,     // 记录总条数display: 10,   // 每页显示条数current: 1,   // 当前的页数},
 methods: {
     pagechange:function(currentPage){
       console.log(currentPage);       // ajax请求, 向后台发送 currentPage, 来获取对应的数据 }
   },
components: {      &#39;v-pagination&#39;: pagination,
    }
}</script>

At this point, a paging component based on vue2.0 is completed

The above is the detailed content of Paging component implemented using vue2.0. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
VUE3开发基础:使用extends继承组件VUE3开发基础:使用extends继承组件Jun 16, 2023 am 08:58 AM

Vue是目前最流行的前端框架之一,而VUE3则是Vue框架的最新版本,相较于VUE2,VUE3具备了更高的性能和更出色的开发体验,成为了众多开发者的首选。在VUE3中,使用extends继承组件是一个非常实用的开发方式,本文将为大家介绍如何使用extends继承组件。extends是什么?在Vue中,extends是一个非常实用的属性,它可以用于子组件继承父

如何使用 Vue 实现日历组件?如何使用 Vue 实现日历组件?Jun 25, 2023 pm 01:28 PM

Vue是一款非常流行的前端框架,它提供了很多工具和功能,如组件化、数据绑定、事件处理等,能够帮助开发者构建出高效、灵活和易维护的Web应用程序。在这篇文章中,我来介绍如何使用Vue实现一个日历组件。1、需求分析首先,我们需要分析一下这个日历组件的需求。一个基本的日历应该具备以下功能:展示当前月份的日历页面;支持切换到前一月或下一月;支持点击某一天,

聊聊Vue怎么通过JSX动态渲染组件聊聊Vue怎么通过JSX动态渲染组件Dec 05, 2022 pm 06:52 PM

Vue怎么通过JSX动态渲染组件?下面本篇文章给大家介绍一下Vue高效通过JSX动态渲染组件的方法,希望对大家有所帮助!

VSCode插件分享:一个实时预览Vue/React组件的插件VSCode插件分享:一个实时预览Vue/React组件的插件Mar 17, 2022 pm 08:07 PM

在VSCode中开发Vue/React组件时,怎么实时预览组件?本篇文章就给大家分享一个VSCode 中实时预览Vue/React组件的插件,希望对大家有所帮助!

VUE3开发入门教程:使用组件实现分页VUE3开发入门教程:使用组件实现分页Jun 16, 2023 am 08:48 AM

VUE3开发入门教程:使用组件实现分页分页是一个常见的需求,因为在实际开发中,我们往往需要将大量的数据分成若干页以展示给用户。在VUE3开发中,可以通过使用组件实现分页功能,本文将介绍如何使用组件实现简单的分页功能。1.创建组件首先,我们需要创建一个分页组件,使用“vuecreate”命令创建VUE项目,并在src/components目录下创建Pagin

如何使用 Vue 实现仿照片墙组件?如何使用 Vue 实现仿照片墙组件?Jun 25, 2023 am 08:19 AM

在现代Web开发中,组件化是一个极受欢迎的开发模式。而Vue.js则是一个非常适合组件化的前端框架。在这篇文章中,我们将介绍如何使用Vue.js创建一个仿照片墙组件。在开始之前,我们需要明确一些准备工作。首先,我们需要安装Vue.js。安装的方法非常简单,只需在终端中输入以下命令:npminstallvue安装完成后,我们可以创建一个名为

VUE3初学者入门:使用Vue.js组件组合实现可复用组合VUE3初学者入门:使用Vue.js组件组合实现可复用组合Jun 15, 2023 pm 08:53 PM

Vue.js是一款流行的前端JavaScript框架,它提供了一种简单易用的方式来构建动态网页应用程序。Vue.js的主要特点是其模块化的设计和可插拔的组件系统。这使得开发者可以轻松地创建可复用的组件,从而提高了应用程序的重用性和可维护性。在本文中,我们将重点介绍VUE3初学者如何使用Vue.js组件组合实现可复用组合。Vue.js组件是一个完整的封装元素,

常用 Vue UI 组件的使用技巧常用 Vue UI 组件的使用技巧Jun 25, 2023 am 08:31 AM

在现代Web开发中,UI组件是不可或缺的一部分。Vue.js框架中有许多优秀的UI组件库,如Element-UI、Vuetify、AntDesignVue等。这些组件库提供了许多易于使用的组件,可以帮助我们更加高效地创建Web应用程序。本文将介绍一些常用的VueUI组件,以及使用这些组件的技巧和实用技能。一、El-TableEl-Table是Eleme

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools