How to efficiently filter data in Vuetify Datatable without making unnecessary API calls

I am curious about how I can filter the displayed data by clicking on the headers or searching for a specific word. The issue I am facing is that when I click on the headers to filter the data in descending order, it triggers a new API call to the backend, which I want to avoid as I only want to filter the visible data. The same problem occurs with the text search functionality.

Here is a snippet of my code:

  <v-card>
    <v-card-title>
      Data Display
      <v-spacer></v-spacer>
       <v-flex xs2>
      <v-text-field
        append-icon="search"
        label="Search Data"
        single-line
        hide-details
        v-model="search"
      ></v-text-field>
      </v-flex>
    </v-card-title>
    <v-data-table
        :headers="headers"
        :items="items"
        :total-items="pagination.totalItems"
        :pagination.sync="pagination"
        :search="search"
        item-key="combo_id"
        :rows-per-page-items="[50, 50]"
        hide-actions
      >
      <template slot="items" slot-scope="props">
        <tr @click="getCombo(props.item.combo_id); props.expanded = !props.expanded">
        <td class="text-xs-left">{{ props.item.data }}</td>
        <td class="text-xs-left">{{ props.item.data2 }}</td>
        <td class="text-xs-left">{{ props.item.data3 }}</td>
        <td class="text-xs-left">{{ props.item.data4 }}</td>
        <td class="text-xs-left">{{ props.item.data5 }}</td>
        <td class="text-xs-left">{{ props.item.data6 }}</td>
        <td class="text-xs-left">{{ props.item.data7 }}</td>
       </tr>
      </template>
      <template slot="expand" slot-scope="props">
        <v-card flat>
          <v-card-text>
            <v-btn color="primary" dark @click.stop="commentDialog = true">Show Comments</v-btn> Tags: <strong>{{ comboItemTags }}</strong>
            <v-btn color="warning" class="right" @click.stop="editDialog = true">Edit System</v-btn>
          </v-card-text>
        </v-card>
      </template>
    </v-data-table>
    <div class="text-xs-center pt-2">
      <v-pagination v-model="pagination.page" :length="pages" :total-visible="10"></v-pagination>
    </div>
  </v-card>

    data () {
      return {
      search: '',
      items: [],
      pagination: {
        page: 1,
        rowsPerPage: 50,
        totalItems: 0
      },
...
...
},
watch: {
        pagination: {
            handler() {
                this.getAllSystemsNewPage(this.pagination.page); //Fetch new data and push into items
            },
            deep: true
        }
    },

Answer №1

One issue arises when pagination is affected by sorting or filtering of items.
The watch handler is designed to take val and oldval as parameters. By checking if the page has changed, you can optimize the call to this.getAllSystemsNewPage only when necessary.

watch: {
  pagination: {
      handler(newVal, oldVal) {
          if (newVal.page !== oldVal.page) {
            this.getAllSystemsNewPage(newVal.page); //Retrieve new information and add it to items
          }
      },
      deep: true
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamic text in Bootstrap 5 Popovers

Having some trouble setting the text on a bootstrap popover: Here's the HTML: <span class="d-inline-block" id="searchPredict" tabindex="0" data-bs-toggle="popover" data-bs-trigger="hover focus" tit ...

A helpful guide on how to dynamically input database values into the href attribute of an 'a' tag

How do I successfully pass an ID to href in my code? When I try to return it via req.params, it keeps coming back as undefined. I need the ID from the URL in order to use the findOne method to access the data stored in the database. I've searched thr ...

Steps to refresh a variable when the SMS read plugin successfully completes

I'm attempting to make a post call within the success callback of my SMS read plugin code. I can successfully print _this.otpnumber in the console. Please refer to my stack trace image link getSMS(){ var _this= this; var fil ...

Navigating with Link in React Router DOM v5 to successfully pass and catch data

In the process of developing a basic chat application, I encountered an issue with passing the username via the Link component. Below is the relevant code snippet: <Link to={{ pathname: `/${room}`, state: { the: user } }}> Enter room </Link> ...

Retrieving the value of onCheck from the Checkbox/ListItem

Imagine I have a React.Component that displays components from material-ui: {data.map(value => ( <ListItem key={data.indexOf(value)} primaryText={value} leftCheckbox={ <Checkbox onCheck={this.pr ...

JavaScript and CSOM updates cause SharePoint list item properties to disappear

Within my SharePoint environment, there exists a website where users can load a single list item based on their selection using JavaScript and CSOM. This particular list item contains approximately 60 properties defined in its list definition. Users have ...

Troubleshooting node modules for browser compatibility

Looking for assistance with running a specific node module in a browser. The module in question is called fury.js. I attempted to use browserify, however, encountered an error stating "ReferenceError: fury is not defined" when trying to utilize it. In th ...

What is causing my component callback to not function properly?

I'm currently following a tutorial on AngularJS callbacks from . In my code at https://plnkr.co/edit/dxyI0p0pZFZdMalLfvXO?p=preview, I've attempted to showcase the issue I'm encountering. I have initialized the component in three different w ...

The property fetchPriority is not a valid attribute for HTMLLinkElement

The HTMLLinkElement model has a property mentioned above (as shown in the image), yet the compiler is indicating that the property does not exist. Interestingly, there is one reference visible (the reference I have included in my component). https://i.sst ...

How can a single value (the clicked one) be retrieved from a for loop using addEventListener in a Django project with JavaScript

Struggling to integrate JS into my Django project, I received the following message in the console: HTMLCollection(2) [p.currency-one, p.currency-one] HTMLCollection(2) [input.amount-one, input.amount-one] app.js:21 Uncaught TypeError: c1.addEventListener ...

Issue with Angular 8: click event is not triggering when using ngFor directive to iterate through arrays of objects

Update: The original post has been modified to omit implementation details and complexity. I am facing an issue with an ngFor loop that invokes a method on a service. The method returns an array which is then iterated over by the for loop. The click even ...

Implementing folder-specific routing in Express js

I'm struggling to figure out how to implement a solution that functions like this code snippet. I need to serve different folders based on various parameters, but I'm hitting a roadblock. An example of this in action would be incredibly helpful. ...

When using a file uploader to set an image on v-model in Vue JS, it sometimes results in

I am currently using Vue JS 2 to develop an image uploader functionality. The input in question has a change function that triggers a function and sets the selected file to the v-model property. After logging the data, I noticed that only an empty object ...

Can I employ a PHP script as a "server" for a React application?

As I am using shared hosting without Node installed, I can't utilize Express as a server (or any other node-related features xD). The issue arises when fetching data from the Behance API in my app and encountering a CORS error. To address this probl ...

Using Bitly API in JavaScript to launch a popup window

I have implemented a script that dynamically generates short links for my Tweet buttons. It is working perfectly fine, but I am encountering an issue with opening the link either in a new tab or a popup window. I've tried adjusting the window.locatio ...

Utilizing shared state across multiple files in ReactJS

Learning React has been a challenge for me and I'm struggling to grasp it. How can I access this.state.planet in the newMove() function? I keep getting an error message - Uncaught TypeError: Cannot read property 'state' of undefined. Be ...

Harnessing the Power: Ajax Combined with JQuery

I am facing an issue with my function where I make an ajax request, wait for a response, and return a value but the returned value is coming out as undefined. What could be causing this problem? function RetrieveDataFromURL(url){ return $.ajax({ ...

Webpack encounters difficulty in mounting Vue components

Upon compiling a project using Webpack with Vue, an error arises when accessing a page containing a Vue component: [Vue warn]: Failed to mount component: template or render function not defined. Instead of the expected component, this is rendered by Vu ...

What is the process for utilizing jQuery's advanced ticker feature to extract content from a text file?

I am currently implementing this code on my website: <script> var file = "http://newsxpressmedia.com/files/theme/test.txt"; function getData(){ $.get(file,function(txt){ var lines = txt.responseText.split("\n"); for (var i = ...

The property 'join' is undefined for null values

I've recently started learning AngularJS, but I'm having trouble figuring out what's causing issues in my code. Despite trying various approaches, the outcome is always incorrect. <!DOCTYPE html> <html lang="en" ng-app="myApp"> ...