Using VueJS to dynamically filter table data based on the URL ID

I am looking to retrieve data from a JSON API based on the id in the URL of the page. First, I have the code for my table which contains links based on a URL id that leads to the template of the second table:

<table class="table table-condensed">
    <tr style="text-align:left;" v-for="data in metadata">
        <td>{{data.id}}</td>
        <td><router-link :to="{ name: 'mixtapesDetails', params{id : data.id}}">
            {{data.title}}
        </router-link></td>
        <td><router-link :to="{ name: 'mixtapesDetails', params{id : data.id}}">
            {{data.artist}}
        </router-link></td>
    </tr>
</table>

Here is the second table where I only want to show data based on the ID:

    <div style="margin-left:400px;">
        <div>{{$route.params.id}}</div>
        <table class="table table-condensed">
            <tr>
                <th>ID</th>
            </tr>
            <tr style="text-align:left;" v-for="data in metadata">
                <td>{{data.id}}</td>
                <td>{{data.title}}</td>
                <td>{{data.artist}}</td> 
                <td>{{data.cover}}</td>

            </tr>
        </table>
    </div>

Next, I have a JavaScript function to fetch all the data:

export default {
  data(){
    return {
        metadata: null
    }
  },
  methods:{
    httpGet(theUrl){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", theUrl, false); // true for asynchronous request
    xmlHttp.send(null);
    console.log(JSON.parse(xmlHttp.responseText));
    return JSON.parse(xmlHttp.responseText);
    }
  },
  mounted(){
    this.metadata = this.httpGet("urltooJsondata");
  }
}

Lastly, here are my routes in JavaScript:

export default new VueRouter({
    routes: [
      {
        path: '/mixtapeList',
        name: 'mixtapeList',
        component: mixtapeList
      },
    {
        path: '/mixtapesDetails/:id',
        name: 'mixtapesDetails',
        component: mixtapesDetails
    }
    ]
  })

The goal is to click on a router-link which will lead to a single page with a URL like /mixtapesdetails/id, and the data displayed on that page should correspond to the URL id. I am struggling to find a solution online and would appreciate any help.

Any guidance or assistance would be greatly appreciated.

Answer №1

In my opinion, the $route.params object is the perfect tool for this scenario. If your route is structured like the following,

{
  path: '/albums/:id',
  name: 'albumDetails',
  component: albumDetails
}

then in the albumDetails component, you can easily retrieve the value of the :id path segment using $route.params.id.

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

Just initiate an API request when the data in the Vuex store is outdated or missing

Currently, I am utilizing Vuex for state management within my VueJS 2 application. Within the mounted property of a specific component, I trigger an action... mounted: function () { this.$store.dispatch({ type: 'LOAD_LOCATION', id: thi ...

The PHP script is not receiving the post parameters sent in the ajax request

Help Needed jQuery.ajax({ url: 'PHPdocs/appsearch.php', data: {term:'blub'}, type: "POST", async: true, data: "text", succes ...

Having trouble updating the Vue-Multiselect model with a pre-existing object

Encountering a minor issue while using vue-multiselect to set a predefined selected value on form edit. I have attempted various methods to make this work and currently, the predefined value is being set. However, when I manually try to update the form.new ...

Tips for showing a limited number of results the first time in an AngularJS application

I am a beginner in AngularJS and Ajax requests. I created a demo where I make an Ajax request to get remote data and display it in a list. My goal is to initially show only 10 results when the page loads for the first time, and then load the next 10 result ...

Updating a React component upon pressing a button to trigger a re-render

Currently, I am in the process of developing a React component that allows users to input an ID and retrieve relevant information upon submission. The code for this component is structured as follows: export default class IDContainer extends React.Componen ...

Script for tracking user activity on Facebook and Google platforms

I currently have Google tracking conversion set up, but now I also need to implement Facebook pixel tracking. My existing Google Head Script code is as follows: <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||fu ...

retrieve and assign values to select input fields created dynamically

I have created several dropdown select fields using the following code: <tr v-for="(data,index) in dataList" :key="data.headers"> <td>{{ data.headers }}</td> <td>{{ data.sample }}</td> <td> ...

Float: A threshold reached when adding 1 no longer has any effect

In the realm of programming, float serves as an estimation of a numeric value. For example, 12345678901234567890 === 12345678901234567891 evaluates to true However, 1234567890 === 1234567891 yields false Where does the equation num === num+1 reach a br ...

Verify authentication on a SignalR console application using a JavaScript client

Here is the scenario and solution I am currently working on: In project one, I have a SignalR console application that handles the logic, including authentication using Entity Framework to query the database. In project two, I have an ASP.Net web applicat ...

retrieve information using Python in JavaScript

I am in the process of developing a website using Python, Javascript (JQuery), and AJAX. While I know how to initiate a Python script with Ajax, I am unsure of how to send data back to Javascript from Python. For instance, if there is an error in a form s ...

Here's a unique version: "Strategies for effectively closing a modal when moving to a

I'm currently working with react-router-dom and material UI modal, and I am looking for a way to automatically hide the modal whenever the user navigates to another page. Here is my App component: const App = () => ( <BrowserRouter> &l ...

What impact does async await have on the asynchronous nature of JavaScript?

Although JavaScript is synchronous, we utilize callback promises and async-await to achieve asynchronous behavior. However, with async-await, the code waits for the await statement to be completed before proceeding to the next statement, which may seem s ...

`Developing reusable TypeScript code for both Node.js and Vue.js`

I'm struggling to figure out the solution for my current setup. Here are the details: Node.js 16.1.x Vue.js 3.x TypeScript 4.2.4 This is how my directory structure looks: Root (Node.js server) shared MySharedFile.ts ui (Vue.js code) MySharedFi ...

Design an interactive div element that allows users to modify its content, by placing a span

Here is an example of the desired effect: ICON LINE - 1 This is some sample text inside a div element and the next line should begin here ICON LINE - 2 This is some sample text inside a div element a ...

Ways to have a list component smoothly descend when a dropdown menu is activated

I have a situation where I have a list with two buttons, and each button triggers the same dropdown content in a sidebar component. The issue is that when I click on one button to open the dropdown, the second button does not move down to make space for it ...

Tips and tricks for seamlessly aligning a specific section of your webpage to ensure it scrolls smoothly into view

My codes are not being centered when I use smooth scrolling on a specific section of a Bootstrap carousel template. Is there an easier way to achieve this? The explanation in a post from six years ago didn't help much. Here are my codes and screenshot ...

Exploring React search bar component

I'm struggling to figure out how to search through my list of Pokemon using React. I've tried a few methods, but none seem to be working. Here's a snippet of my code: import React, { Component} from "react"; import axios from "axios"; impor ...

Guide on incorporating mouse movement while the mouse button is held down in JavaScript

For my project, I want to activate the mouse move event only when the mouse button is held down. I specifically need the action "OK Moved" to be triggered only when both the mouse button is held down and there is mouse movement. This is the code that I h ...

Filtering a string array in Angular based on an object's value

I am facing a challenge with two arrays in my code. The first array, $scope.termini, contains strings, and the other one, $scope.reserved, contains objects. Each object in the second array has a variable called vrijemeTermina. My goal is to filter $scope.t ...

Creating a Cancel Button in JSP/HTML/JS/CSS Form: A Step-by-Step Guide

It is necessary to incorporate the functionality of "save" and "cancel" in the JSP code for this particular form. By selecting "save", the data entered into the form will be submitted to its intended destination. Alternatively, choosing "cancel" will dismi ...