Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset a vue-infinite-loading element?

I have a list of Exhibits. The user can add exhibits. When the user adds exhibits, vue-infinite-loading does not pick up the change in the number of exhibits, so it says "End of List" even though there are more to show.

Here's a Code Pen. Note: I can't get vue-infinite-loading working in a sandbox, but it is definitely working on my site.

Here's the code:

HTML:

<div id="app">
  <button @click="addTenRows()">Add 10 Rows</button>
  <table cellpadding="10">
    <tr><th>id</th><th>Name</th><th>Description</th></tr>
    <tr v-for="exhibit in exhibits.resultsMap">
      <td>{{exhibit.id}}</td>
      <td>{{exhibit.name}}</td>
      <td>{{exhibit.desc}}</td>
    </tr>
  </table>
  <infinite-loading 
      :distance="10" 
      @infinite="loadMore" 
      ref="infiniteLoading" 
      identifier="exhibitLoader">
      <span slot="no-more">
        <h5>
          <i class="fa fa-check-circle"></i> End of Results
          <span v-if="exhibits.results">
              : <strong>{{exhibits.meta.totalRecords}} shown</strong>
          </span>
        </h5>
      </span>
  </infinite-loading>
</div>

JS:

new Vue({
  el: '#app',
  methods: {
    addTenRows(){
      this.exhibits.meta.totalRecords += 5;
    },
    loadMore(){
      this.exhibits.results.push('16');
      this.exhibits.results.push('17');
      this.exhibits.results.push('18');
      this.exhibits.results.push('19');
      this.exhibits.results.push('20');

      this.exhibits.resultsMap[ '16' ] = { 'id':'16', 'name': 'STR-16', desc: 'Exhibit number 16' };
      this.exhibits.resultsMap[ '17' ] = { 'id':'17', 'name': 'STR-17', desc: 'Exhibit number 17' };
      this.exhibits.resultsMap[ '18' ] = { 'id':'18', 'name': 'STR-18', desc: 'Exhibit number 18' };
      this.exhibits.resultsMap[ '19' ] = { 'id':'19', 'name': 'STR-19', desc: 'Exhibit number 19' };
      this.exhibits.resultsMap[ '20' ] = { 'id':'20', 'name': 'STR-20', desc: 'Exhibit number 20' };
    }
  },
  data: {
    exhibits: {
      meta: {
        totalRecords: 25
      },
      results: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'],
      resultsMap:{
        '1':{ id: 1, name: 'STR-01', desc: 'some exhibit' },
        '2':{ id: 2, name: 'THR-20', desc: 'another exhibit' },
        '3':{ id: 3, name: 'STR-02', desc: 'a third' },
        '4':{ id: 4, name: 'THR-21', desc: 'This is the fourth Exhibit' },
        '5':{ id: 5, name: 'STR-03', desc: 'Number 5' },
        '6':{ id: 6, name: 'THR-22', desc: 'Up Up Down Down' },
        '7':{ id: 7, name: 'STR-04', desc: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis ultrices ' },
        '8':{ id: 8, name: 'THR-23', desc: 'Praesent augue tellus, aliquet sit amet tellus in' },
        '9':{ id: 9, name: 'STR-05', desc: 'Cras eget augue tellus.' },
        '10':{ id: 10, name: 'THR-24', desc: 'Nunc facilisis elit at efficitur ullamcorper.' },
        '11':{ id: 11, name: 'STR-06', desc: 'In vel turpis ex. Curabitur mollis ac sapien eget aliquet' },
        '12':{ id: 12, name: 'THR-25', desc: 'Morbi eget lacus eu mi tincidunt scelerisque ac sed metus' },
        '13':{ id: 13, name: 'STR-07', desc: 'Donec consequat est maximus, venenatis velit a, vehicula felis' },
        '14':{ id: 14, name: 'THR-26', desc: 'Mauris orci neque, ullamcorper non blandit non' },
        '15':{ id: 15, name: 'STR-08', desc: 'Vivamus efficitur erat vel consectetur sagittis' }
      }
    }
  }
});

In the Example above here's what happens:

  1. The page loads, 15 records are displayed
  2. The user clicks the "Add 10 records" button
  3. exhibits.meta.totalRecords is increased by 10
    • This SHOULD tell infinite-loader to refresh
  4. The user scrolls down to within 10px of the bottom, and infinite-loading shows a spinner while loadMore adds 10 rows to exhibits.results and exhibits.resultsMap

Things I've tried:

  • forcing a reset:
    • in addTenRows, after I add ten to meta.totalRecords
    • this.$refs.infiniteLoading.stateChanger.reset(); - nothing changes
    • this.$refs.exhibitLoader.stateChanger.reset(); - error, exhibitLoader is undefined.
    • this.exhibitLoader.stateChanger.reset(); - errors, exhibitLoader is undefined.
like image 886
Travis Heeter Avatar asked Dec 04 '22 19:12

Travis Heeter


1 Answers

Well, you already add the ref="infiniteLoading" so you need to validate the component has been created:

created(){
    if(this.$refs.InfiniteLoading){
        this.$refs.InfiniteLoading.stateChanger.reset(); 
    }
}
like image 159
Pedro Cáceres Avatar answered Jun 11 '23 21:06

Pedro Cáceres