Troubleshooting issue in Vue.js: difficulty in fetching and showing a specific value from an object when clicking

I've been experimenting with various aspects of vue.js such as methods, directives, loops, and higher order functions, including working with $this.refs... The main objective is to show a book description when the title (which is a button element in the vue.js template) is clicked - the rendering works perfectly:

<section  v-for="(bookTitle, index) in books"
      v-bind:key="index"
      
      >
    <button
      ref="el"
      @click="hidden = !hidden"
      class="list-group-item"
    v-if="bookTitle.title"
    >{{bookTitle.title}}
    </button></section>

I have retrieved data from an API and structured it into a data array:

mounted() {
fetch("https://www.googleapis.com/books/v1/volumes?q=%22coding%22", {
  method: "get",
})
  .then((res) => {
    return res.json();
  })
  .then((result) => {
    let title;
    let description;
    let authors;
    let id;
    for (var i = 0; i < result.items.length; i++) {
      title = result.items[i].volumeInfo.title;
      description = result.items[i].volumeInfo.description;
      authors = result.items[i].volumeInfo.authors;
      id = result.items[i].id;
      this.bookData.push(
      
        {
        available: true, 
        title,
        id
        },
        {
        authors,
        description,
        id
        }

It's possible that my object structure might be causing issues. If there's a simple way to achieve the desired outcome within a vue.js project, I would appreciate any assistance. Thanks.

Answer №1

When creating the bookData object, include a property called showButton that can be toggled false when clicked on. See the code snippet below for reference.

For a working example, check here

this.bookData.push(      
{
   avalable: true, 
   title,
   id,
   description,
   showButton = true
}

In the template, depending on the showButton property, either display the button or the title of the book. Using ref element is not necessary in this scenario.

<section  v-for="(bookTitle, index) in books"
   v-bind:key="index"
   >
   <button
      @click="bookTitle.showButton = !bookTitle.showButton"
      class="list-group-item"
      v-if="bookTitle.showButton"
      >{{bookTitle.title}}
   </button>
   
  <!-- Logic for displaying title goes here -->
  <h2 v-if="!bookTitle.showButton">{{bookTitle.description}}</h2>
</section>

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

How can I create a JSON output from my MySQL database that includes the total count of records per day for a Task entry?

I am looking to implement the JavaScript library called Cal-Heatmap (https://kamisama.github.io/cal-heatmap/) to create an Event style heatmap similar to GitHub's. The objective is to visualize the number of actions taken on each Task record in my Pr ...

How can I receive notification of changes within a template component in AngularJS 1.5?

For instance, I have a form with various input fields: <form> <input type="text" ng-model="$ctrl.inputText"> <input type="number" ng-model="$ctrl.inputNumber"> </form> Since my app is component-based, I need to trigger an event in ...

Transforming an unordered list to function similarly to a select input

I am looking to style a ul list to function as a select form element. I have successfully populated a hidden input using my code. Now, I am trying to make the ul behave like a select input when either the keyboard or mouse is used. Previously, I had some ...

Various inquiries utilizing varying parameters

Initially, I am receiving instructions from a mysql query. Receiving Instructions function getAllInstructions(req,res){ let mysqlQuery = `SELECT * FROM instructions where id = '` + req.params.id + "'"; connection.query(mysqlQuery, funct ...

What is the reason for having to add my IP to the white-list daily?

As a beginner, I am delving into the world of back-end development with Node.js, Express.js, and Mongoose for educational purposes. My database is hosted on Atlas-Mongo DB. Initially, everything seemed to be running smoothly as I configured it with the fre ...

The value being passed to the Child Component is only updating once

Within my Vue app, the parent component contains a list of months structured as shown below: https://i.stack.imgur.com/B9XQK.png Upon clicking on any month from the list, I aim to transfer its corresponding id to the child component named timeComponent. ...

In the Vue 3 options API, default props values are not automatically assigned

After creating a select2 wrapper in vue3 using the options API, everything was functioning correctly. However, I encountered an issue where the default value was not being selected in the select2 option when retrieving values from an API call. Strangely, i ...

Obtain the chosen item from a Bootstrap4 dropdown menu by utilizing a button

I am struggling to get a button with dropdown working in Bootstrap4. Below is the HTML code: <div class="row" id="dropdown-box"> <div class="col-lg-6"> <div class="input-group"> <div class="input-group-btn" id="button-grou ...

JavaScript alert box

I'm fairly new to the world of web development, with knowledge in CSS & HTML and currently learning TypeScript. I'm attempting to create a message icon that opens and closes a notifications bar. Here's where I'm at so far: document.getE ...

Using JavaScript, adding an element to an array results in the array returning a value of 1

When I try to push a string into an array and then return the array, I am getting unexpected result of "1". Upon closer inspection, it seems that the array is being successfully created but before returning it, only "1" is being returned. Here is the snip ...

Is there a way to send a post request containing a base64 encoded image?

I am currently developing an image upload component in Vue.js that includes a custom cropping option. The cropped version of the image is saved in my state as a base64 string, like this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAYAAAB91L6 ...

The functionality of AngularJS ng-click is disrupted by the presence of {{$index}}

Having an issue with AngularJS where using $index breaks the ng-click function. This issue arises within a div utilizing ng-repeat, the repeat code is functioning correctly... <a class="accordion-toggle" data-toggle="collapse" data-parent="#acc{{$inde ...

Angular backend endpoints for serving non-HTML assets

My goal is to establish routing using ngRoute in Angular. I'm aware that when making GET requests to the backend, the expected response is to return index.html, which then loads Angular, allowing for the correct page to be constructed. The issue I&ap ...

How to update an Array<Object> State in ReactJS without causing mutation

In my program, I store an array of objects containing meta information. This is the format for each object. this.state.slotData [{ availability: boolean, id: number, car: { RegistrationNumber : string, ...

Using Express.js 4 to manipulate data by reading from and writing to an array directly, without the need

I am currently developing an express.js sample application for my own reference. I need to store data without setting up a database at the moment. I am curious about how I can save data to a file in express. It doesn't necessarily have to persist, bu ...

The latest update to andt version 5.20.0 has encountered an issue with the FastColor.js file located in the node_modules. An error has been identified at line 56

After updating to the latest version of [email protected], I utilized npm i antd --save --legacy-peer-deps without any issues. However, upon running npm start, I encountered an error in the console. Is anyone else experiencing this problem? ERROR in . ...

Is there a way to configure my datepicker so that it displays only dates that are later than a specified date

At the heart of my inquiry lies a dilemma: I have two date pickers, one for leave_start and the other for leave_end. If an individual selects "YES" for a future text_field, I aim to ensure that the date pickers only display dates after the person's an ...

Processing requests through Axios and Express using the methods GET, POST, PUT, and DELETE

When working with express router and Axios (as well as many other frameworks/APIs), the use of GET/POST/PUT/DELETE methods is common. Why are these methods specified, and what are their differences? I understand that a GET request is used to retrieve dat ...

Retrieve a JSON file with Hebrew data from a server and then interpret it using Node.js

I have a JSON file with dynamic content stored on a remote server acting as an API. The file also includes Hebrew text in its values. How can I retrieve the response and convert it into a JSON object? Here is the code snippet I have put together using Re ...

The issue with AngularJS and the JQuery $.each() function is that it seems to only output the final

Incorporating both AngularJS and the JQuery library into my project has been a rewarding experience. Utilizing JQuery's $.each() function to iterate through my class, I aim to structure an array of objects in the following format: [ {"invoice_no ...