search
HomeWeb Front-enduni-appHow to implement list paging function in uniapp

How to implement list paging function in uniapp

Jul 04, 2023 pm 06:09 PM
listuniappPagination

How to implement list paging function in uniapp

Overview:
In developing mobile applications, it is often necessary to display a large amount of data. In order to improve the user experience, the data is often loaded in pages to reduce the number of times a single The amount of data loaded increases the response speed. This article will introduce how to implement list paging function in uniapp and provide code examples.

  1. Preparation work:
    First, you need to install and introduce the uni-paging component in the uniapp project. You can install it through npm:

    npm i uni-paging

Then, introduce this component into the page where you need to use the list paging function:

import uniPaging from '@dcloudio/uni-paging'
  1. Use the uni-paging component :
    Next, use the uni-paging component in the template of the page and set the necessary properties and events:
<uni-paging
  ref="paging"
  :total="total"
  :current="current"
  @change="handleChange"
>
  <!-- 数据列表 -->
  <ul>
    <li v-for="item in list" :key="item.id">{{ item.name }}</li>
  </ul>

  <!-- 加载更多 -->
  <view slot="loading" class="loading">
    数据加载中...
  </view>
</uni-paging>

Among them, the total attribute represents the total number of pages , current attribute indicates the current page number. @changeThe event will be triggered when the page number changes. We need to load the data corresponding to the page number in this event.

Define relevant data in data:

data() {
  return {
    list: [],   // 数据列表
    total: 0,   // 总页数
    current: 1  // 当前页码
  }
},

Define the method of loading data in methods, and send an interface request to obtain data according to the page number:

methods: {
  loadData() {
    // 发送请求获取数据,此处为示例代码
    uni.request({
      url: 'https://example.com/data',
      data: {
        page: this.current,
        pageSize: 10  // 每页显示的数据量
      },
      success: (res) => {
        if (res.statusCode === 200) {
          this.list = res.data.list;   // 更新数据列表
          this.total = res.data.total; // 更新总页数
        }
      }
    })
  },

  handleChange(current) {
    this.current = current;   // 更新当前页码
    this.loadData();         // 加载对应页码的数据
  }
},

Start when the page is loaded Paging component and loading the data of the first page:

onLoad() {
  const paging = this.$refs.paging;
  paging.setOptions({
    loadingText: '正在加载...',
    statusTextMap: {
      more: '加载更多',
      noMore: '没有更多'
    }
  });
  this.loadData();
}

At this point, we have successfully implemented the list paging function in uniapp.

Summary:
By introducing the uni-paging component, we can easily implement the list paging function in uniapp. Just set the relevant properties and events, and write the method to load the data. I hope the introduction in this article will be helpful to you in implementing list paging function in uniapp development.

Code example:

<template>
  <view class="container">
    <uni-paging
      ref="paging"
      :total="total"
      :current="current"
      @change="handleChange"
    >
      <ul>
        <li v-for="item in list" :key="item.id">{{ item.name }}</li>
      </ul>

      <view slot="loading" class="loading">
        数据加载中...
      </view>
    </uni-paging>
  </view>
</template>

<script>
import uniPaging from '@dcloudio/uni-paging'

export default {
  components: {
    uniPaging
  },

  data() {
    return {
      list: [],
      total: 0,
      current: 1
    }
  },

  methods: {
    loadData() {
      uni.request({
        url: 'https://example.com/data',
        data: {
          page: this.current,
          pageSize: 10
        },
        success: (res) => {
          if (res.statusCode === 200) {
            this.list = res.data.list;
            this.total = res.data.total;
          }
        }
      })
    },

    handleChange(current) {
      this.current = current;
      this.loadData();
    }
  },

  onLoad() {
    const paging = this.$refs.paging;
    paging.setOptions({
      loadingText: '正在加载...',
      statusTextMap: {
        more: '加载更多',
        noMore: '没有更多'
      }
    });
    this.loadData();
  }
}
</script>

<style>
.container {
  width: 100%;
  height: 100%;
  padding: 20rpx;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  padding: 10rpx;
  border-bottom: 1rpx solid #ddd;
}

.loading {
  text-align: center;
  padding-top: 20rpx;
  padding-bottom: 20rpx;
}
</style>

The above is a simple example of how to implement list paging function in uniapp. By using the uni-paging component, we can easily implement paged loading of lists and improve the user experience. Hope this article is helpful to you.

The above is the detailed content of How to implement list paging function in uniapp. 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
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor