Home >Web Front-end >uni-app >How to implement pull-down refresh and pull-up loading functions in uniapp

How to implement pull-down refresh and pull-up loading functions in uniapp

PHPz
PHPzOriginal
2023-04-14 15:34:147301browse

With the continuous upgrading of mobile terminal development, developers’ demand for mobile applications is also getting higher and higher. In many mobile applications, pull-down to refresh and pull-up to load are more essential functions. In order to improve user experience, many mobile applications will add these two functions. Here, we will introduce how to implement pull-down to refresh and pull-up to load more functions in uniapp.

1. Basic content introduction

uniapp is a cross-platform application development framework based on the Vue.js framework, which supports one-time writing and multi-end publishing. Due to its good cross-platform features and built-in many native APIs, developers can use these APIs directly in uniapp without having to learn other development languages.

In uniapp, pull-down refresh and pull-up loading are more built-in components, that is, <uni-scroll-view>, through this control we can realize pull-down refresh and pull-up Load functionality without involving other modules.

2. Pull-down refresh

Pull-down refresh means that when the page content is pulled down, an event is triggered, the latest data is obtained from the server, and the data of the current page is replaced. In this chapter, we will introduce how to use the <uni-scroll-view> component to implement the pull-down refresh function in uniapp.

  1. Open the page

Before developing the uniapp page, you first need to enter the project tool Hbuilder X and create a new uniapp project , since we want to implement the pull-down refresh and pull-up loading functions, we need to first confirm that we are using the uni-ui component library, so we must select uni-ui when creating a new project selection page.

  1. Write code

The following is the specific implementation method:

Note: The sample code is only for reference and does not guarantee 100% correctness .

index.vue code:

<!-- 下拉刷新 -->
<uni-scroll-view
    class="content"
    :enable-back-to-top="true"
    @downRefresh="onDownRefresh" 
    refresher-default-style
    :refresher-triggered="isRefreshing"
    refresher-loading="{{isLoading}}"
    refresher-enabled="{{true}}"
    style="height:100vh;"
>
    <!--该区域可以放置需要下拉刷新的内容-->
    <!--......-->
    <div class="list-view">
        <ul>
            <li v-for="item in items" :key="item.id">{{item.text}}</li>
        </ul>
    </div>
</uni-scroll-view>

<script>
export default {
    data() {
        return {
            items: [
                {text: 'item1', id:1},
                {text: 'item2', id:2},
                {text: 'item3', id:3},
                {text: 'item4', id:4},
                {text: 'item5', id:5},
                {text: 'item6', id:6},
                {text: 'item7', id:7},
                {text: 'item8', id:8},
                {text: 'item9', id:9},
                {text: 'item10', id:10}
            ],
            isRefreshing: false,
            isLoading: false,
        }
    },
    methods: {
        onDownRefresh() {
            this.isRefreshing = true;
            setTimeout(() => {
                this.isRefreshing = false;
                uni.stopPullDownRefresh()
            }, 2000)
        },
    }
}
</script>

<style>
.content {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
</style>

After the above steps, we can implement the pull-down refresh function. Specifically, it is implemented through the downRefresh event of the <uni-scroll-view> component. When the pull-down triggers this event, we set isRefreshing to true, the refreshed status is displayed at this time. After the time arrives, we set isRefreshing to false and close uni.stopPullDownRefresh().

3. Pull-up loading

Pull-up loading more means that when the page content is pulled upward, a function is triggered to obtain more data from the server and add it to the end of the page. In this chapter, we will introduce how to use the <uni-scroll-view> component in uniapp to achieve more pull-up loading functions.

  1. Writing code

The above code already includes the pull-down refresh function, so we only need to add the code for the pull-up loading function to the above code.

<uni-scroll-view
    class="content"
    :enable-back-to-top="true"
    @downRefresh="onDownRefresh" 
    refresher-default-style
    :refresher-triggered="isRefreshing"
    refresher-loading="{{isLoading}}"
    refresher-enabled="{{true}}"
    @scrolltolower="loadMore" 
    :onLoadmore="false"
    style="height: 100vh;"
>
    <!--该区域可以放置需要下拉刷新的内容-->
    <!--......-->
    <div class="list-view">
        <ul>
            <li v-for="item in items" :key="item.id">{{item.text}}</li>
        </ul>
        <!--上拉加载-->
        <div v-if="isLoadMore">
            <span>loading...</span>
        </div>
    </div>
</uni-scroll-view>

<script>
export default {
    data() {
        return {
            items: [
                {text: 'item1', id:1},
                {text: 'item2', id:2},
                {text: 'item3', id:3},
                {text: 'item4', id:4},
                {text: 'item5', id:5},
                {text: 'item6', id:6},
                {text: 'item7', id:7},
                {text: 'item8', id:8},
                {text: 'item9', id:9},
                {text: 'item10', id:10}
            ],
            isRefreshing: false,
            isLoading: false,
            isLoadMore: false,
        }
    },
    methods: {
        onDownRefresh() {
            this.isRefreshing = true;
            setTimeout(() => {
                this.isRefreshing = false;
                uni.stopPullDownRefresh()
            }, 2000)
        },
        loadMore() {
            this.isLoadMore = true;
            setTimeout(() => {
                // 模拟从服务器获取了10个新数据
                for (let i=0; i<10; i++) {
                    const item = {
                        id: this.items.length + i + 1,
                        text: `item${this.items.length + i + 1}`
                    };
                    this.items.push(item);
                }
                this.isLoadMore = false;
            }, 2000)
        }
    }
}
</script>

<style>
.content {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
</style>

As shown in the above code, we implement pull-up loading through the @scrolltolower event in the <uni-scroll-view> component. When the page content slides to the bottom, we call the this.loadMore() method, in which we can load more data and update the page.

Finally, we set true for isLoadMore, and the "loading" status is displayed on the page. After waiting for 2 seconds, add 10 new data to the items data and close the isLoadMore state.

4. Summary

Through the methods introduced in this article, we can quickly implement more pull-down refresh and pull-up loading functions in uniapp. If you haven’t tried this feature yet, you can follow the steps in this article to get started quickly and make your mobile app even better. If you have any questions or queries, please leave them in the comment area.

The above is the detailed content of How to implement pull-down refresh and pull-up loading functions 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