如何重置Vue无限加载元素?

时间:2019-01-04 19:09:26

标签: vuejs2 vue-infinite-loading

我有一个展览清单。用户可以添加展览。当用户添加展览品时,vue-infinite-loading不会选择展览品数量的变化,因此即使有更多的展览品,它也会显示“列表结尾”。

这里是Code Pen注意:我无法在沙盒中使用vue-infinite-loading,但绝对可以在我的网站上使用。

代码如下:

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' }
      }
    }
  }
});

在上面的示例中,将发生以下情况:

  1. 页面加载中,显示15条记录
  2. 用户单击“添加10条记录”按钮
  3. exhibits.meta.totalRecords增加10
    • SHOULD 告诉无限加载程序刷新
  4. 用户向下滚动到底部的10像素以内,无限加载显示一个微调框,而loadMore向exhibits.resultsexhibits.resultsMap添加10行

我尝试过的事情:

  • 强制重置:
    • addTenRows中,我将十加到meta.totalRecords
    • this.$refs.infiniteLoading.stateChanger.reset();-没什么变化
    • this.$refs.exhibitLoader.stateChanger.reset();-错误,exhibitLoader未定义。
    • this.exhibitLoader.stateChanger.reset();-错误,exhibitLoader未定义。

1 个答案:

答案 0 :(得分:2)

好吧,您已经添加了ref =“ infiniteLoading”,因此需要验证是否已创建组件:

created(){
    $if(this.$refs.InfiniteLoading){
        this.$refs.InfiniteLoading.stateChanger.reset(); 
    }
}