微信小程序实现下拉刷新怎么实现数据的追加

2021-01-20 14:00:59

1.思路

在微信小程序中提供了下拉刷新的api(onPullDownRefresh()),那么实现下拉刷新的业务就是再下拉的时候,在原有的数据中追加新的数据

2.代码实现

2.1 坑一

onPullDownRefresh: function () {
    var _this =this;

    /**显示loading */
    wx.showNavigationBarLoading();

    _this.newsrequest(_this.data.page);

    wx.stopPullDownRefresh();
    wx.hideNavigationBarLoading();   
    // console.log(_this.data.newslists)
  },
  
  
  /**请求数据 */
  newsrequest:function(page){
    var _this=this;
    // console.log(_this.data.page)
    requstFfunc.requestData('https://xxxx/Public/Api/index.php', { service: 'Default.Archives', typeid: '44', orderby: '4', page:page, pagesize: '10' }).then(function (data) {
      var result = _this.newsdatadecorate(data.data.data.list);
      console.log(page)
      console.log(result)
     
      var arr=_this.data.newslists;
      if (result.length>0){
        /*此时会发现数据被添加到原有数据的后边类似上拉加载更多*/
        _this.setData({
          newslists: _this.data.newslists.concat(result)
        });
        _this.data.page++;
      }
    })
  },

我们知道数组的concat方法时讲参数数组拼接到原有数据后边,所以与我们想要的追加在前面不想符合,想想该数组前面追加的方法时unshift()更改代码:

2.2坑二

有人说concat方法时在数据后边追加,那么只要再使用数组反转reverse()将数组反转,仔细想想,这种数据不是我们想要的下拉刷新的

    var arr1 = [1,2,3,4];//原来的数据
    var arr2 = [5,6,7,8];//后来的数据
    var arr3 = arr1.concat(arr2);//[1,2,3,4,5,6,7,8];
    var arr4 = arr3.reverse();//[8,7,6,5,4,3,2,1]
    //但是我们想要的应该是
    var arr5 =[5,6,7,8,1,2,3,4]
newsrequest:function(page){
    var _this=this;
    // console.log(_this.data.page)
    requstFfunc.requestData('https://xxxx/Public/Api/index.php', { service: 'Default.Archives', typeid: '44', orderby: '4', page:page, pagesize: '10' }).then(function (data) {
      var result = _this.newsdatadecorate(data.data.data.list);
      console.log(page)
      console.log(result)
     
      var arr=_this.data.newslists;
      if (result.length>0){
        _this.setData({
          newslists: _this.data.newslists.concat(result).reverse()
        })
        _this.data.page++;
      }
    })
  },

2.2坑三

newsrequest:function(page){
    var _this=this;
    // console.log(_this.data.page)
    requstFfunc.requestData('https://xxxx/Public/Api/index.php', { service: 'Default.Archives', typeid: '44', orderby: '4', page:page, pagesize: '10' }).then(function (data) {
      var result = _this.newsdatadecorate(data.data.data.list);
      console.log(page)
      console.log(result)
     
      var arr=_this.data.newslists;
      if (result.length>0){
        for(var t in result){
          _this.data.newslists.unshift(result[t])
        }
        _this.data.page++;
      }
    })
  },

但是页面的数据不显示,具体原因不详,应该是this.data.方法设置数据,应该和页面的生命周期有关,在数据设置时,页面已渲染,(自己想想的,不知道对不对)

这是我们就可以通过中介,将数据拼接好了之后,再通过this.setData()方法设置数据,这时就可以正常显示

newsrequest:function(page){
    var _this=this;
    // console.log(_this.data.page)
    requstFfunc.requestData('https://xxxx/Public/Api/index.php', { service: 'Default.Archives', typeid: '44', orderby: '4', page:page, pagesize: '10' }).then(function (data) {
      var result = _this.newsdatadecorate(data.data.data.list);
      
      var arr=_this.data.newslists;
      if (result.length>0){
       
        for(var v in result){
          arr.unshift(result[v])
        };
        _this.setData({
          newslists:arr
        });
        
        _this.data.page++;
      }
    })
  },

上述方案虽然成功了实现了数据追加,但是。。。且看下边代码

    var arr1 = [1,2,3,4];//原来的数据
    var arr2 = [5,6,7,8];//后来的数据
    var arr3 = arr2.concat(arr1);//[5,6,7,8,1,2,3,4];
    
    //但是我们想要的应该是
    var arr5 =[5,6,7,8,1,2,3,4]
    //此时两个数据是不是一致的呢
newsrequest:function(page){
    var _this=this;
    // console.log(_this.data.page)
    requstFfunc.requestData('https://xxxx/Public/Api/index.php', { service: 'Default.Archives', typeid: '44', orderby: '4', page:page, pagesize: '10' }).then(function (data) {
      var result = _this.newsdatadecorate(data.data.data.list);
      console.log(page)
      console.log(result)
     
      var arr=_this.data.newslists;
      if (result.length>0){
        _this.setData({
          newslists: result.concat(_this.data.newslists)
        });
        _this.data.page++;
      }
    })
  },

本文转自:https://cloud.tencent.com/developer/article/1349925

发表评论:

  • Nickname_45 :I think the admin of this site is truly working hard in favor of his web page because here every data is quality based information

  • Nickname_120 :I like the helpful info you provide for your articles Ill bookmark your blog and check again right here regularly I am rather sure Ill be told lots of new stuff right here Good luck for the following

  • Nickname_127 :Woah Im really digging the templatetheme of this blog Its simple yet effective A lot of times its difficult to get that "perfect balance" between usability and visual appearance I must say that youve done a amazing job with this Also the blog loads very fast for me on Firefox Outstanding Blog

  • Nickname_177 :Great delivery Great arguments Keep up the amazing effort

  • Nickname_276 :Thanks in support of sharing such a pleasant thinking piece of writing is nice thats why i have read it entirely

  • Nickname_285 :Hey there just wanted to give you a quick heads up The text in your article seem to be running off the screen in Opera Im not sure if this is a formatting issue or something to do with web browser compatibility but I figured Id post to let you know The design look great though Hope you get the problem solved soon Thanks

  • Nickname_318 :I view something truly special in this website

  • Nickname_320 :Someone necessarily help to make severely articles Id state That is the first time I frequented your web page and thus far I surprised with the research you made to create this actual post incredible Wonderful activity

  • Nickname_578 :This post will assist the internet people for creating new webpage or even a blog from start to end

  • Nickname_605 :Hello I’d like to mention that this post is extremely helpful I really enjoyed how you presented the main concepts of the topic One of the things I found most interesting was your analysis of this aspect It really made me think about the realworld implications It would be fantastic if you could explore similar subjects like this in upcoming content Kudos for providing such great insights Eager to read more of your posts Cheers

  • Nickname_635 :I every time emailed this blog post page to all my contacts because if like to read it then my contacts will too

  • Nickname_688 :Having read this I believed it was very enlightening I appreciate you finding the time and energy to put this informative article together I once again find myself personally spending a significant amount of time both reading and posting comments But so what it was still worth it

  • Nickname_730 :Have you ever thought about publishing an ebook or guest authoring on other sites I have a blog based upon on the same ideas you discuss and would love to have you share some storiesinformation I know my readers would enjoy your work If you are even remotely interested feel free to shoot me an email

  • Nickname_751 :Why people still make use of to read news papers when in this technological globe all is existing on net

  • Nickname_791 :We are a group of volunteers and starting a brand new scheme in our community Your site provided us with useful info to work on You have done an impressive activity and our whole neighborhood will probably be grateful to you

  • Nickname_823 :I went over this website and I believe you have a lot of great info saved to my bookmarks :

  • Nickname_1016 :Hi there just became aware of your blog through Google and found that it is really informative Im going to watch out for brussels I will be grateful if you continue this in future A lot of people will be benefited from your writing Cheers

  • Nickname_1062 :I got what you mean thanks for putting up Woh I am thankful to find this website through google

  • Nickname_1322 :Every weekend i used to visit this website because i want enjoyment as this this website conations really pleasant funny material too

  • Nickname_1346 :Appreciating the persistence you put into your site and detailed information you provide Its nice to come across a blog every once in a while that isnt the same out of date rehashed information Wonderful read Ive bookmarked your site and Im including your RSS feeds to my Google account

  • Nickname_1347 :Hi there I am so happy I found your site I really found you by mistake while I was looking on Aol for something else Nonetheless I am here now and would just like to say kudos for a marvelous post and a all round enjoyable blog I also love the themedesign I dont have time to go through it all at the minute but I have bookmarked it and also added in your RSS feeds so when I have time I will be back to read a great deal more Please do keep up the awesome job

  • Nickname_1350 :This page certainly has all of the info I wanted concerning this subject and didnt know who to ask

  • Nickname_1382 :Woah Im really loving the templatetheme of this site Its simple yet effective A lot of times its difficult to get that "perfect balance" between superb usability and visual appeal I must say you have done a fantastic job with this Additionally the blog loads very quick for me on Internet explorer Exceptional Blog

  • Nickname_1495 :I’m not that much of a online reader to be honest but your sites really nice keep it up Ill go ahead and bookmark your website to come back later Cheers

  • Nickname_1520 :Its difficult to find experienced people in this particular subject but you seem like you know what youre talking about Thanks

  • Nickname_1532 :fantastic points altogether you simply won a new reader What would you suggest in regards to your publish that you simply made some days in the past Any positive

  • Nickname_1560 :I like the helpful information you provide to your articles I will bookmark your weblog and check once more right here frequently Im slightly sure I will be told plenty of new stuff proper here Good luck for the next

  • Nickname_1563 :Hi there Dear are you truly visiting this web page daily if so then you will absolutely get pleasant knowledge

  • Nickname_1568 :Hi I understand this is kind of offtopic however I needed to ask Does managing a wellestablished website such as yours require a large amount of work I am completely new to running a blog however I do write in my journal daily Id like to start a blog so I will be able to share my own experience and feelings online Please let me know if you have any ideas or tips for new aspiring blog owners Appreciate it

  • Nickname_1628 :Hmm it appears like your blog ate my first comment it was super long so I guess Ill just sum it up what I submitted and say Im thoroughly enjoying your blog I too am an aspiring blog writer but Im still new to everything Do you have any points for novice blog writers Id definitely appreciate it

  • Nickname_1660 :This piece of writing presents clear idea in support of the new users of blogging that really how to do running a blog

  • Nickname_1661 :When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get several emails with the same comment Is there any way you can remove people from that service Thanks a lot

  • Nickname_1683 :Amazing blog Is your theme custom made or did you download it from somewhere A theme like yours with a few simple tweeks would really make my blog jump out Please let me know where you got your design Cheers

  • Nickname_1713 :I think the admin of this website is genuinely working hard in support of his website as here every stuff is quality based data

  • Nickname_1717 :Hey this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML Im starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience Any help would be greatly appreciated

  • Nickname_1948 :Wonderful blog I found it while surfing around on Yahoo News Do you have any suggestions on how to get listed in Yahoo News Ive been trying for a while but I never seem to get there Many thanks

  • Nickname_1954 :Simply desire to say your article is as surprising The clearness in your post is simply excellent and i could assume you are an expert on this subject Well with your permission allow me to grab your RSS feed to keep up to date with forthcoming post Thanks a million and please carry on the enjoyable work

  • Nickname_1968 :Hi there I could have sworn Ive been to this blog before but after browsing through some of the post I realized its new to me Nonetheless Im definitely glad I found it and Ill be bookmarking and checking back frequently

  • Nickname_1983 :When someone writes an paragraph heshe keeps the plan of a user in hisher mind that how a user can understand it Therefore thats why this post is perfect Thanks

  • Nickname_2082 :Fantastic blog Do you have any helpful hints for aspiring writers Im hoping to start my own blog soon but Im a little lost on everything Would you suggest starting with a free platform like Wordpress or go for a paid option There are so many options out there that Im totally overwhelmed Any tips Kudos

  • Nickname_2190 :This article is truly a fastidious one it helps new internet visitors who are wishing in favor of blogging

  • Nickname_2233 :Please let me know if youre looking for a author for your weblog You have some really good articles and I feel I would be a good asset If you ever want to take some of the load off Id absolutely love to write some material for your blog in exchange for a link back to mine Please blast me an email if interested Many thanks

  • Nickname_2240 :Hey there Someone in my Myspace group shared this website with us so I came to look it over Im definitely enjoying the information Im bookmarking and will be tweeting this to my followers Wonderful blog and fantastic design and style

  • Nickname_2245 :Fantastic blog Do you have any recommendations for aspiring writers Im planning to start my own site soon but Im a little lost on everything Would you suggest starting with a free platform like Wordpress or go for a paid option There are so many choices out there that Im completely overwhelmed Any suggestions Thanks a lot

  • Nickname_2364 :Hi are using Wordpress for your blog platform Im new to the blog world but Im trying to get started and set up my own Do you require any coding knowledge to make your own blog Any help would be greatly appreciated

  • Nickname_2426 :If some one needs to be updated with latest technologies therefore he must be visit this site and be up to date daily

  • Nickname_2449 :Greetings I must say that this article is truly informative I really enjoyed how you presented the main concepts of the topic I particularly liked the way you approached a specific point It really made me think about the realworld implications I’d love to hear more on related topics like this in more detail Appreciate the knowledge Looking forward to more content from you All the best

  • Nickname_2566 :hiI like your writing so much share we communicate more approximately your article on AOL I require a specialist in this space to unravel my problem May be that is you Taking a look ahead to look you

  • Nickname_2870 :This is the perfect site for anyone who really wants to find out about this topic You realize a whole lot its almost tough to argue with you not that I really will need to…HaHa You certainly put a new spin on a topic thats been written about for decades Wonderful stuff just great

  • Nickname_2962 :These are genuinely fantastic ideas in on the topic of blogging You have touched some nice things here Any way keep up wrinting

  • Nickname_3028 :Hi there this weekend is nice in support of me since this time i am reading this wonderful informative paragraph here at my house

  • Nickname_3125 :You made some good points there I looked on the internet for more info about the issue and found most individuals will go along with your views on this web site

  • Nickname_3156 :Remarkable things here I am very satisfied to look your article Thank you so much and I am having a look forward to contact you Will you kindly drop me a mail

  • Nickname_3184 :First of all I want to say fantastic blog I had a quick question which Id like to ask if you dont mind I was interested to know how you center yourself and clear your mind prior to writing Ive had a tough time clearing my mind in getting my thoughts out I truly do take pleasure in writing however it just seems like the first 10 to 15 minutes tend to be wasted simply just trying to figure out how to begin Any recommendations or tips Thanks

  • Nickname_3219 :I know this if off topic but Im looking into starting my own weblog and was curious what all is needed to get set up Im assuming having a blog like yours would cost a pretty penny Im not very internet smart so Im not 100 positive Any suggestions or advice would be greatly appreciated Thank you

  • Nickname_3225 :Hey there exceptional website Does running a blog like this require a massive amount work Ive virtually no understanding of coding however I had been hoping to start my own blog soon Anyways should you have any ideas or techniques for new blog owners please share I understand this is off topic but I simply needed to ask Kudos

  • Nickname_3226 :This design is spectacular You obviously know how to keep a reader entertained Between your wit and your videos I was almost moved to start my own blog well almostHaHa Excellent job I really loved what you had to say and more than that how you presented it Too cool

  • Nickname_3278 :Can I view this unit

  • Nickname_3279 :Hey very nice web site Man Excellent Wonderful I will bookmark your web site and take the feeds also I am glad to search out numerous useful info right here within the post we need work out extra techniques in this regard thanks for sharing

  • Nickname_3290 :Hi there this weekend is fastidious for me for the reason that this point in time i am reading this enormous educational piece of writing here at my residence

  • Nickname_3405 :Hi would you mind stating which blog platform youre working with Im planning to start my own blog in the near future but Im having a hard time deciding between BlogEngineWordpressB2evolution and Drupal The reason I ask is because your design seems different then most blogs and Im looking for something completely unique PS Apologies for getting offtopic but I had to ask

  • Nickname_3484 :I read this post fully about the resemblance of hottest and preceding technologies its awesome article

  • Nickname_3585 :Wow At last I got a webpage from where I can actually take useful facts regarding my study and knowledge

  • Nickname_3601 :Hi friends nice post and pleasant urging commented at this place I am in fact enjoying by these

  • Nickname_3617 :I do not even understand how I finished up right here but I thought this publish used to be great I dont recognise who you might be however certainly you are going to a famous blogger when you arent already Cheers

  • Nickname_3635 :My spouse and I stumbled over here coming from a different website and thought I may as well check things out I like what I see so now i am following you Look forward to going over your web page again

  • Nickname_3663 :Бонусный слот казино Заходи и выигрывай htps:tmeMajestic_Bison

  • Nickname_3698 :I like the valuable info you supply on your articles Ill bookmark your blog and check again right here frequently I am slightly sure I will be told plenty of new stuff proper right here Best of luck for the following

  • Nickname_3797 :Magnificent site Lots of useful information here Im sending it to a few pals ans additionally sharing in delicious And of course thanks to your sweat

  • Nickname_3831 :I blog often and I seriously appreciate your content This article has truly peaked my interest Im going to bookmark your site and keep checking for new information about once per week I subscribed to your RSS feed as well

  • Nickname_3838 :Wonderful blog I found it while searching on Yahoo News Do you have any tips on how to get listed in Yahoo News Ive been trying for a while but I never seem to get there Many thanks

  • Nickname_3871 :That is really attentiongrabbing You are an excessively professional blogger Ive joined your feed and stay up for in quest of extra of your excellent post Also Ive shared your website in my social networks

  • Nickname_3897 :Wonderful beat I would like to apprentice while you amend your site how could i subscribe for a blog web site The account helped me a acceptable deal I had been tiny bit acquainted of this your broadcast offered bright clear idea

  • Nickname_3912 :Excellent blog you have here but I was curious if you knew of any forums that cover the same topics discussed here Id really like to be a part of community where I can get opinions from other experienced people that share the same interest If you have any suggestions please let me know Kudos

  • Nickname_3942 :I really like what you guys are up too This kind of clever work and coverage Keep up the amazing works guys Ive incorporated you guys to my personal blogroll

  • Nickname_3964 :Im extremely inspired with your writing talents as neatly as with the structure on your blog Is that this a paid subject or did you customize it yourself Anyway stay up the excellent quality writing it is rare to look a great weblog like this one nowadays

  • Nickname_4008 :naturally like your website but you have to test the spelling on several of your posts Many of them are rife with spelling problems and I to find it very troublesome to inform the reality nevertheless Ill surely come again again

  • Nickname_4046 :I was curious if you ever thought of changing the structure of your site Its very well written; I love what youve got to say But maybe you could a little more in the way of content so people could connect with it better Youve got an awful lot of text for only having one or two images Maybe you could space it out better

  • Nickname_4055 :Wonderful what a web site it is This website provides useful facts to us keep it up

  • Nickname_4194 :Hey there Quick question thats entirely off topic Do you know how to make your site mobile friendly My site looks weird when viewing from my iphone4 Im trying to find a template or plugin that might be able to resolve this problem If you have any recommendations please share Cheers

  • Nickname_4208 :Your style is very unique compared to other folks Ive read stuff from Thanks for posting when youve got the opportunity Guess Ill just bookmark this web site

  • Nickname_4287 :I enjoy lead to I found just what I used to be looking for Youve ended my four day lengthy hunt God Bless you man Have a great day Bye

  • Nickname_4293 :Hi there I discovered your web site by the use of Google at the same time as looking for a related topic your website got here up it appears to be like great I have bookmarked it in my google bookmarks Hello there simply become aware of your blog thru Google and located that it is truly informative Im gonna be careful for brussels I will appreciate when you continue this in future A lot of other folks will probably be benefited from your writing Cheers

  • Nickname_4353 :Thanks Ive recently been searching for information approximately this topic for a while and yours is the best I have came upon till now However what in regards to the conclusion Are you sure about the source

  • Nickname_4410 :Wonderful work That is the type of information that are meant to be shared across the net Disgrace on the seek engines for no longer positioning this publish upper Come on over and visit my web site Thanks =

  • Nickname_4498 :Great article That is the type of info that are meant to be shared around the web Shame on the seek engines for no longer positioning this submit upper Come on over and talk over with my web site Thank you =

  • Nickname_4563 :I blog frequently and I truly appreciate your content The article has truly peaked my interest I will bookmark your website and keep checking for new information about once per week I subscribed to your RSS feed too

  • Nickname_4590 :I think this is one of the most important info for me And im glad reading your article But want to remark on few general things The web site style is great the articles is really nice : D Good job cheers

  • Nickname_4761 :I read this piece of writing fully regarding the comparison of most recent and preceding technologies its awesome article

  • Nickname_4784 :Whats up its me I am also visiting this site on a regular basis this website is genuinely good and the viewers are genuinely sharing fastidious thoughts

  • Nickname_4854 :Can I just say what a relief to find an individual who genuinely knows what they are discussing on the internet You definitely realize how to bring an issue to light and make it important More and more people should read this and understand this side of your story Its surprising you arent more popular because you certainly have the gift

  • Nickname_4950 :Thanks in support of sharing such a good opinion post is pleasant thats why i have read it completely

  • Nickname_4977 :Thanks for the good writeup It in truth was once a amusement account it Glance complex to far introduced agreeable from you However how could we be in contact

  • Nickname_4987 :Hi i believe that i saw you visited my blog so i came to return the favorI am trying to find things to improve my web siteI assume its good enough to use a few of your ideas

  • Nickname_4993 :Thank you for any other excellent post Where else may just anyone get that kind of info in such an ideal means of writing I have a presentation subsequent week and Im at the search for such information

  • Nickname_4994 :Hi Do you know if they make any plugins to help with Search Engine Optimization Im trying to get my blog to rank for some targeted keywords but Im not seeing very good gains If you know of any please share Many thanks

  • Nickname_5012 :This article is in fact a fastidious one it helps new net visitors who are wishing for blogging

  • Nickname_5037 :We stumbled over here by a different page and thought I may as well check things out I like what I see so now i am following you Look forward to looking over your web page for a second time

  • Nickname_5100 :Good way of telling and pleasant paragraph to obtain information on the topic of my presentation focus which i am going to deliver in academy

  • Nickname_5106 :Its remarkable for me to have a web site which is useful in support of my experience thanks admin

Powered by PHP 学习者(mail:517730729@qq.com)

原百度博客:http://hi.baidu.com/ssfnadn

备案号:闽ICP备17000564号-1

开源中国 PHPCHINA