Home> Web Front-end> Vue.js> body text

Vue component practice: paging component development

王林
Release: 2023-11-24 08:56:05
Original
1319 people have browsed it

Vue component practice: paging component development

Vue component practice: paging component development

Introduction

In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use.

In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples.

Technology Stack

  • Vue.js 2.x
  • JavaScript (ES6)
  • HTML5 and CSS3

Development environment

  • Node.js v8.9.3
  • npm v5.5.1
  • Vue.js v2.5.2

Paging component Requirements

  • Receive the total number of pages (total) and the current number of pages (current) through props
  • You can configure the maximum number of pages displayed (maxShown)
  • Yes Configure the text (prevText and nextText) and button style displayed by the button
  • Click the page number to switch to the corresponding page
  • The current page number is highlighted
  • The current page has no previous page When, ignore the click event of the previous page button
  • When the current page does not have the next page, ignore the click event of the next page button

Design ideas and code implementation

Based on requirements, we split the paging component into multiple small components for implementation. We need to create the following 3 small components:

  1. Pagination.vue

The main paging component is responsible for processing paging data and logic. Pass paging information to subcomponents and respond to subcomponent events.

  1. Button.vue

This component is a button component, used to create paging buttons.

  1. Page.vue

This component is used to create a single page block, including page label and status. Page blocks can be the current page or non-current pages.

Next, let us use code to implement the above 3 components.

  1. Pagination.vue
 
Copy after login

In the above code, we first imported the ButtonPrev, ButtonNext and Page components. Next, the total, current, maxShown, prevText and nextText attributes are obtained using props, and the calculated attribute pages is defined. Based on the current page number (current) and the maximum page number (maxShown), an array containing the page number is obtained to use in the component. Present.

We also define the selectPage method, in which if the page number (page) is the same as the current page number (current), return or do nothing. Otherwise, the new page number is emitted to the parent component.

The prev() and next() methods are used to handle previous page and next page events and prevent events from being responded to.

  1. ButtonPrev.vue
  
Copy after login

In the above code, we first obtain the current page number (current) and the text (prevText) attributes of the previous page button through props. In the template, use class binding (disabled) to control the button usage state. An onPrev method is defined, which triggers the onPrev event of the parent component.

  1. ButtonNext.vue
  
Copy after login

In the above code, we copied the code of ButtonPrev.vue and slightly changed the text and judgment conditions.

  1. Page.vue
  
Copy after login

In the above code, we obtain the value of the page number (page) and the isSelected attribute of the button through props. In the template, use class binding ("current") to highlight the selected page.

We also define an onPageSelected method, which triggers the onPageSelected event of the parent component.

Finally, these components can be used in a template in any Vue.js application, as shown below:

 
Copy after login

In the above code, we introduced the Pagination component and used it as a template parent component in . We also bind total, current and maxShown to the component to get their values. In the onPageChanged method, we can handle the page change event and request the corresponding data based on the current page number.

The above is the detailed content of Vue component practice: paging component development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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