Vue table displaying a list of books with a button that allows users to easily send the title of the

Hey everyone, I am new to Vue and struggling with a certain task. I have two tables: Books and Booking. Books: ID, NAME, AUTHOR etc. Booking: ID, ID_USER, ID_BOOK

I'm creating a page in Vue that displays all bookings, but the table only shows the BOOK ID. I want to make it so that when I click on the BOOK ID, the page will display the name of the book associated with that ID.

<template>
<div>
  <table class="table table-striped">
     <thead>
        <tr>
           <th>User</th>
           <th>Book</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="booking in bookings" :key="title">
          <td>{{booking.user}}</td>
            <buitton typeof="button" class="btn btn-light mr-1">{{booking.ID_BOOK}}</button>        
        </tr>
      </tbody>
    </table>
 </div>
</template>
<script>
import axios from "axios" 
export default {
  name: "Prenotazioni",
  data() {
    return {
      bookings: [],
      books:[]
    }
  },
  mounted() {
    axios
      .post("https://localhost:7285/Prenotazioni/GetPrenotazione")
      .then(response => {
         this.bookings = response.data.pre
      })
      .catch(error => {
         console.log(error)
         this.errored = true
      })
      .finally(() => this.loading = false),
         axios
           .post("https://localhost:7285/Libri/GetLibri")
           .then(response => {
              this.books=response.data.libro
           })
           .catch(error => {
              console.log(error)
              this.errored = true
           })
           .finally(() => this.loading = false)
      }
}
</script>

Answer №1

Give this a try :

new Vue({
  el: '#app',
  data: {
    bookings: [],
    books:[],
    selectedBook: null
  },
  mounted() {
    // Just for demonstration purposes, I am simulating the response here, but in reality you can fetch it from an API.
    this.books = [{
      id: 1,
      name: 'Book 1',
      author: 'Author 1'
    }, {
      id: 2,
      name: 'Book 2',
      author: 'Author 2'    
    }];
    this.bookings = [{
      id: 1,
      user: 'Alpha',
      ID_USER: 1,
      ID_BOOK: 1
    }, {
      id: 2,
      user: 'Beta',
      ID_USER: 2,
      ID_BOOK: 2   
    }];
  },
  methods: {
    getBookDetails(bookId) {
        this.selectedBook = this.books.find(obj => obj.id === bookId);
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <table class="table table-striped">
    <thead>
      <tr>
        <th>User</th>
        <th>Book</th>
        <th>Book Name</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(booking, index) in bookings" :key="booking.id">
        <td>{{ booking.user }}</td>
        <td><button @click="getBookDetails(booking.ID_BOOK)">{{ booking.ID_BOOK }}</button></td>
        <td v-if="selectedBook?.id === booking.ID_BOOK">{{ selectedBook?.name }}</td>
      </tr>
    </tbody>
  </table>
</div>

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

Autofill Text Input with React Material-UI

For my current project, I am utilizing Material UI and React. Each TextField in the project has a button next to it, and when the button is clicked, I want it to retrieve the value of its corresponding TextField and set that value as the input for another ...

How can a child value be transferred from a client component to a parent component on the server side?

I am facing a situation where a client-side component needs to send a value to its parent component which is server-side. I have tried using useState and other hooks, but so far it has been unsuccessful. Can anyone provide guidance on how to achieve this? ...

Using Node.js to efficiently parse JSON data into customizable PUG templates

I have a challenge where I am parsing JSON data into elements called homeCards. To achieve this, I use Axios to request the JSON data and then utilize a for loop to cycle through it. Inside my Axios function, I extract the fields I need and store them in v ...

Sometimes, Express may return the message "not found" on and off

After working with express for many years, I find myself a bit out of practice with TypeScript - and it seems like my eyesight is failing me! This is the first time I've encountered this issue, so I must be missing something... My current dilemma is ...

Discovering the flaw in my programming that pertains to JSON parsing

After countless hours of searching and reviewing documentation, I am still unable to identify the issue with my jQuery code. My goal is to practice reading a local JSON file and displaying its contents in the body of my HTML document. $(document).ready(fu ...

Show the value of a JavaScript object in VueJS

New to Vue and need some guidance... I'm having trouble accessing the values in a JS object displayed in the DevTools within one of my routes in a Vue app. Specifically, how can I display the values from filteredData:Array[1]? https://i.sstatic.net/ ...

Converting a function to an ES6 class-based style

Hello, I am new to ES6 and eventEmitter. I have created a module in Node.js with an event-based style and now I am trying to convert it to ES6 class style. This is my code: // eventStyle.js const events = require('events'); const util = require ...

Find all strings in the array that do not begin with a letter from the alphabet

When a specific button is clicked, I need to filter the example array provided in the snippet below. The expected output should only include elements that do not start with an alphabet. I attempted: const example = ["test", 'xyz', '1 test& ...

When using threejs, the color set for setClearColor is supposed to be white. However, when calling an external HTML file, it unexpectedly renders as

When I call an external HTML file in Three.js, the value for setClearColor is white but it renders as black. How can I solve this issue? Click here to view the image Here are the codes from the external file: <div id="3d-modal"></div> <sc ...

Trouble installing NPM packages from Artifactory on Windows 10

Problem Description: I am utilizing Artifactory for my NPM packages. When attempting to install them on "Windows - 7", everything is functioning correctly. However, on "Windows - 10" an error is being displayed and the packages are not installing. Error M ...

Prevent the execution of an event while another event is already running in jQuery

Two events are in play here: one with the onclick function which scrolls to a specific div upon menu item click, and the other utilizes the onscroll function to "highlight" the corresponding menu item when scrolling near the div. The burning question is: ...

Issue with the usage of section helpers in express-handlebars

I'm having trouble getting the section helper to function correctly. The content of login.hbs was parsed properly, but the js section was not parsed at all. I attempted using helpers within res.render() and directly including section() in engine(), bu ...

Ways to incorporate smooth transitions to content using CSS

I recently created a website for a competition and I am looking to make the text change when a user hovers over a link. While I have managed to achieve the text change, I now want to add a transition to it. I have only used CSS to change the code. Can some ...

Incorporating Dynamic Events into HTML Generated on the Fly within a Vue.js Component

Currently, I am facing an issue where I am trying to dynamically generate HTML in a Vue.js component. While I have successfully rendered the HTML, I am struggling to connect the events for these dynamically generated elements. To illustrate this problem, I ...

Utilizing Vue.js to initiate a server request with vue-resource

Seeking guidance on performing a server call using vue-resource. I'm unsure about how to set the header and send data via Vue.$http.post Vue.$http.post('http://url/', { email: 'foo', password: 'bar' }, { headers: { ...

What is the best way to separate the date and time into individual components?

I have created a dynamic object that shows both the date and time. My goal is to figure out how I can separate the time portion from the date so that I can place it in its own HTML element and style it differently? JavaScript isn't my strong suit, e ...

Selenium Tips: Ensuring RemoteDriver Remains Connected to the Active Browser Tab

Currently working on a unique Windows application that utilizes voice commands to control web browsers. I am trying to figure out the best approach when users add tabs and modify the selected tab as needed. Unfortunately, RemoteDriver only supports swi ...

The process of authenticating route parameters in Nuxt

I'm having trouble validating route parameters in my page component using the following code: async validate({ params, store }) { await store.dispatch(types.VALIDATE_PARAMS_ASYNC, params.id) } And here's the corresponding code in the store: ...

What is the best way to extract all the data from a JSON using a shared key?

I'm trying to extract an array containing all the name values from a JSON structure. This is how my JSON data is structured: { "profile": { "G5j7": { "name": "siddharth", "age": &quo ...

Learn how to implement drag-and-drop functionality in React by making a component

I am currently experimenting with dragging a component using the react-dnd library. I wanted to replicate functionality similar to this example, specifically only focusing on dragging at the moment. In my application, I have imported and utilized the rea ...