TahaSh / vue-paginate

A simple vue.js plugin to paginate data
MIT License
593 stars 103 forks source link

Prevent negative value for currentPage #95

Open patrickkivits opened 6 years ago

patrickkivits commented 6 years ago

When given an empty list, the value of currentPage becomes -1.

Even when you then update the empty list with an item, the items still won't show up.

This pull request ensures there can never be a negative value for the currentPage and it will always have an minimum value of 0.

Fixes: #94

JeremyLopez commented 6 years ago

I found a hacky workaround that fixed it for my app. First, I added a ref to my <paginate></paginate> component ref="paginator". Then I created a computed property:

emptyArray () {
    return store.state.recipes.length == 0
}

then I created a watcher that looks for a change from length == 0 to length != 0:

watch: {
      emptyArray: function(newVal, oldVal) {
        if ( newVal === false && oldVal === true ) {
          setTimeout(() => {
            if (this.$refs.paginator) {
              this.$refs.paginator.goToPage(page)
            }
          }, 100)
        }
      }
    }

The timeout was necessary otherwise it thought there was no page 1.