Applying a filter to a Vue.js ajax-generated list - Step by step tutorial

Currently, I am facing an issue while attempting to implement a filter on a list fetched via AJAX in my Vue.js application. The problem arises as Vue.js attempts to apply the filter before the list is fully loaded and displayed.

Below is my current implementation:

<template>
  <div class="grid__container games">
    <h2>Games</h2>
    <div v-if="games">
      <table>
        <tr>
          <th>Player One</th>
          <th>Results</th>
          <th>Player Two</th>
          <th>Played At</th>
        </tr>
        <tr v-for="game in games.data">
          <td>{{ game.player_one }}</td>
          <td>{{ game.player_one_goals }} - {{ game.player_two_goals }}</td>
          <td>{{ game.player_two }}</td>
          <td>{{ [ game.created_at, "YYYY-MM-DD h:mm:ss" ] | moment "dddd, MMMMM, Do YYYYY" }}</td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
  import auth from '../auth'

  export default {
    data() {
      return {
        data: '',
        games: ''
      }
    },
    methods: {
      getGames() {
        this.$http
          .get('/api/games', { headers: auth.getAuthHeader() }).then((data) => {
            this.data = data
            this.games = JSON.parse(this.data.body)
        }, (error) => {
          console.log(error)
        })
      }
    },
    created: function () {
      this.getGames()
    } 
  }
</script>

While trying to apply the filter based on the game's creation date, I encounter an error when loading the page:

Uncaught ReferenceError: string is not defined

I'm looking for a solution or workaround that would delay the filtering process until after the list has been successfully loaded. Is there any way to achieve this?

Answer №1

If you're looking for a quick solution, consider using a method instead of a filter:

formatDate(game) {
  if (game !== undefined && game.date !== undefined) {
    return someFunctionToFormatDate(game.date)
  } else {
    return ''
  }
}

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

Current component is not able to bind to the scope of the Vue Axios promise

One challenge I am facing is how to properly bind functions in the methods object. While using arrow functions may seem like a solution, it actually creates its own scope and prevents me from updating data variables after an HTTP GET request. The componen ...

"Cross-Origin Resource Sharing (CORS) Configuration Issue Between FastAPI and Vue Deployed on Google Cloud

I am facing an issue with my FastAPI backend and Vue frontend setup where they are deployed separately. While they work fine on localhost when containerized and deployed on the same machine, I am having trouble with browser negotiation between the two in o ...

The controller fails to acknowledge the Ajax request

How can I implement Ajax functionality to add a new micropost to my home page without redirection immediately after it is created? I have included remote: true in the form: <%= form_for(@micropost, html: { multipart: true }, remote: true) do |f| %> ...

Discover the method to retrieve the month name from an HTML Date input value

Here we have a date input field <input id="customer-date" name="customer-date" type="date" required> Accompanied by this script const customerDate = document.getElementById('customer-date').value; const dateHandler = document.getElementB ...

What is the best way to create a reset button for a timing device?

Is there a way to reset the timer when a button is clicked? Having a reset button would allow users to revisit the timer multiple times without it displaying the combined time from previous uses. Check out this code snippet for one of the timers along wit ...

The Power of Javascript in Enhancing Lightbox Experience

I am trying to enhance an image outputted by JavaScript with Lightbox functionality. Since the image link is dynamically created, I am approaching it this way! Despite searching on Stack Overflow, I have not found a solution that fits my needs... The cur ...

Obtain the parameters of a JavaScript function that is stored as a string

While parsing a webpage, I came across a JavaScript function stored as a string: "translate(737.4170532226562,136.14541625976562)" My goal is to extract the two parameters from this function. I currently parse the string up to the '(' and &apos ...

JavaScript exit page trigger

I have successfully implemented a way to exit the page with a confirmation using window.onbeforeunload. However, I am curious about how I can customize the appearance of the exit confirmation box by adding CSS and HTML elements, instead of just displayin ...

Exporting Google Visualization DataView to an Excel file

Looking for guidance on the best way to provide users with a button to export data from a DataView behind a chart or table into Excel. I explored using the Toolbar functionality but discovered it was not suitable for my needs since the DataView is manuall ...

Can const variables be reassigned in JavaScript programming?

Can you reassign a const variable in JavaScript? In C++, we can cast variables to and from const. Is there something similar in JavaScript? My question is const a = 1; unconst(a); a = "xyz"; a === "xyz" // true I'm not referring to object prope ...

Is it possible to utilize a Directive for nesting content within an ng-template in Angular?

I'm working on a project where I need to dynamically nest my code into the ng-template using a Directive. Is there a method to accomplish this in Angular? Here's an example of the code I have: <button> Click Me </button> If I use a ...

Utilizing Jquery and Ajax for dynamically filling a select dropdown menu

My goal is to dynamically populate a drop down menu based on the selection made in another drop down. It's interesting because this functionality works perfectly in my development environment, but when I try to implement it on the IIS server, it encou ...

Is it possible to use ajax to upload files, as demonstrated below?

Is it possible to upload a file using AJAX? Currently, I am only able to retrieve the file name. $(document).ready(function() { $("#uploadbutton").click(function() { var filename = $("#file").val(); $.ajax({ type: "POS ...

Transfer a script from a view to application.js in Rails: Guide and Instructions

After conducting some research, I found a code that worked well for me in the view. However, I am looking to transfer it to application.js so that I can utilize it in various forms. Another issue I encountered is having two scripts performing the same fun ...

Testing a function within a React functional component using jest.spyOn with React Testing Library can be accomplished with the following steps:

I'm currently working on a React component and I'm facing an issue with unit testing using React Testing Library. Specifically, I'm having trouble testing the handleClick function of the TestComponent using jest.spyOn(). Can anyone provide s ...

What is the process for connecting this to React ES6 getter and setter methods?

When utilizing this within an ES6 function, it is necessary to explicitly declare it in the constructor. export class MyComponent extends React.Component { constructor(){ super(); this.myFunc = this.myFunc.bind(this); } ...

Errors have been observed when using JavaScript variables that begin with the symbol $

For the longest time, I've used JavaScript variable names that begin with $ to signify that they hold jQuery values. For example: $buttons = $( 'button' ); However, a couple of nights ago, I encountered an issue when loading the page in the ...

Having trouble making class changes with ng-class

I am attempting to change the direction of the arrow in my checkbox by utilizing two classes with the assistance of ng-class. Unfortunately, it is not producing the desired outcome. Below is my code: Note: The angularJS CDN has already been incorporated. ...

What is the technique used to initialize the $route object?

Upon attempting to access this.$route in the created() hook, I noticed that it consistently returns an empty object when logged to the console. {path: '/', name: undefined, params: {…}, query: {…}, hash: '', …} fullPath: "/&q ...

Watching the scope.$watch item is not effective for fields within JavaScript objects

Struggling with observing the properties of JavaScript objects has been a challenge for me. I attempted to explain this issue in more detail here, but unfortunately, I haven't found a solution yet... To elaborate on the problem further, you can find ...