1
votes

I have a sort and pagination computed function in VUE which and allows displaying in a table

sortedTransactions: function() {
      return this.transactions
          .sort((a, b) => {
            let modifier = 1;
            if (this.currentSortDir === "desc") modifier = -1;
            if (a[this.currentSort] < b[this.currentSort]) return -1 * modifier;
            if (a[this.currentSort] > b[this.currentSort]) return 1 * modifier;
            return 0;
          })
          .filter((row, index) => {
            let start = (this.currentPage - 1) * this.pageSize;
            let end = this.currentPage * this.pageSize;
            if (index >= start && index < end) return true;
          });
      }

So I want to be able to search the data and then display the results based on the user input

searchResults() {
      const search = this.sortedTransactions.filter(transaction => { 
          return transaction.user_id.toLowerCase() === this.searchTable
        });
        this.sortedTransactions = search;
    }

Of course, I need the search results to update in the data table but can't see a way to do it.

2

2 Answers

0
votes

You should modify the data property transactions instead of updating computed one because it's not correct what you're doing this.sortedTransactions = search; since computed properties are not mutable, so your function should be like :

  searchResults() {
  const search = this.transactions.filter(transaction => { 
      return transaction.user_id.toLowerCase() === this.searchTable
    });
    this.transactions = search;
}

and the computed property sortedTransactions will be updated automatically

0
votes

Define serchResults() in your method, listen to @key event, and pass it to the method to filter result.