最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Load more button in vuejs - Stack Overflow

programmeradmin3浏览0评论

I receive from php an array with customer reviews:

var comment_list = new Vue({

el: '#comment-list',

 data: {
    testimonials: JSON.parse('{!! addslashes(json_encode(array_reverse($product_info['testimonials'])))!!}'),
 },

 methods: {
    colorAvatar: function(letter) {
        return 'avatar-' + letter.toLowerCase();
    },
    starClass: function(star) {
        return 'star-' + star;
    }
  }
});

I want to create a button to load more and show comments ten by ten.

How can I do it?

I receive from php an array with customer reviews:

var comment_list = new Vue({

el: '#comment-list',

 data: {
    testimonials: JSON.parse('{!! addslashes(json_encode(array_reverse($product_info['testimonials'])))!!}'),
 },

 methods: {
    colorAvatar: function(letter) {
        return 'avatar-' + letter.toLowerCase();
    },
    starClass: function(star) {
        return 'star-' + star;
    }
  }
});

I want to create a button to load more and show comments ten by ten.

How can I do it?

Share Improve this question edited Nov 11, 2018 at 6:57 Aravinda Meewalaarachchi 2,6291 gold badge30 silver badges27 bronze badges asked Nov 11, 2018 at 6:48 RichardMiraclesRichardMiracles 2,15212 gold badges31 silver badges47 bronze badges 3
  • Looks like you're going to have to implement an API on the back end, then use a library like axios or fetch to grab the updates from said API based on the last comment you fetched – Derek Pollard Commented Nov 11, 2018 at 7:00
  • I know, but I don't have a comment api, it's just an array in php. Can you think of anything? – RichardMiracles Commented Nov 11, 2018 at 7:08
  • Ajax really is the most efficient way to do this. Otherwise, you can load all of the comments on the initial page load, then only display the first 3, then when they click a button, display 3 more and so on – Derek Pollard Commented Nov 11, 2018 at 7:10
Add a comment  | 

2 Answers 2

Reset to default 19

Without an API, and loading all the comments on the initial load:

new Vue({
  el: ".vue",
  data() {
    return {
      reviews: [{name: 'Derek', description: 'Some comment'}, {name: 'Joe', description: 'Some comment'},{name: 'Mike', description: 'Some comment'}, {name: 'Ron', description: 'Some comment'},{name: 'Dii', description: 'Some comment'}, {name: 'Lonnie', description: 'Some comment'},{name: 'Paul', description: 'Some comment'}, {name: 'Mike', description: 'Some comment'},{name: 'Jody', description: 'Some comment'}, {name: 'Ryn', description: 'Some comment'},{name: 'Jord', description: 'Some comment'}, {name: 'Milly', description: 'Some comment'},{name: 'Judy', description: 'Some comment'}, {name: 'Vanilly', description: 'Some comment'},{name: 'Nolan', description: 'Some comment'}, {name: 'Pino', description: 'Some comment'},{name: 'Ryne', description: 'Some comment'}, {name: 'Scott', description: 'Some comment'},{name: 'Son', description: 'Some comment'}, {name: 'Bann', description: 'Some comment'},],
      commentsToShow: 2
    };
  }  
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div class="container vue">
  <div v-if="commentIndex <= commentsToShow" v-for="commentIndex in commentsToShow"> 
    <div>{{reviews[commentIndex - 1].name}} says:</div>
    <i><div>{{reviews[commentIndex - 1].description}}</div></i>
    <hr />
  </div>
  <button @click="commentsToShow += 2">show more reviews</button>
</div>

I hope this helps!

The correct way is using AJAX, and send a request each button click.

You need to create an web service endpoint (for example /api/comments) and send the code of this web service send you the comments as JSON. You may also add parameter ?page=1 to be able to divide it to tens, page 1 is the first ten, page 2 is the second ten and so on.

Then, you need to assign on click event handler to the "load more" button, that should sends the request to that web service. You can use axios here like this:

axios.get('/api/comments').then(res => {
    /* returned data will be in res.data */
    /* it should contain the comments array you sent in the web service */
    this.testimonials = res.data;
});
发布评论

评论列表(0)

  1. 暂无评论