Tips for keeping a button selected in each row of a table

I'm confused about a simple issue. I have a table that fetches data from a JSON file, where each row represents a JSON field. In each row, I added two buttons (Like/Dislike) to assess the search quality. However, whenever I click on the like button on one row and then on another row, it doesn't keep the like selected on the first row.

The desired functionality is for the user to evaluate each line of data by choosing between like or dislike, and then saving this assessment.

Unfortunately, the evaluation options are not being retained for each line.

The code already incorporates integration with VUE.

Here's my HTML file:

<template>
<div v-for="regList in myJson" :key="regList" class="container" >
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <table>
    <thead>
      <tr>
        <th>Document</th>
        <th colspan="2">Rate Search</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="countryList in regList[2]" :key="countryList">
        <td style="visibility: visible">
          {{countryList}}
        </td>
        <td>
          <button class="btn btn-1" type="button"><i class="fa fa-thumbs-up"></i></button>
        </td>
        <td>
          <button class="btn btn-2" type="button"><i class="fa fa-thumbs-down"></i></button>
        </td>
      </tr>
    </tbody>
  </table>
  </div>  
</template>

https://i.sstatic.net/sHULR.png

EDIT 1

Nikola Pavicevic's solution seems suitable, but I need to provide more details about my code. My Vue file runs the project, where users enter a term, send it to the backend, receive a JSON response with autocomplete suggestions and information from another API. The frontend separates the autocomplete suggestion and displays the JSON information in a table format. The data shown in the table is just descriptive. Below is a model representing how the JSON populates the table.

VUE section:

<script>
export default {
  name: 'Autocomplete',
  data: function() {
    return {
      autoComplete: "",
      maxChars: 75,
      connection: null, 
      myJson: []
    }
  },
  mounted() {
    const url = "ws://localhost:8000/"
    this.connection = new WebSocket(url);
    this.connection.onopen = () => console.log("connection established");
    this.connection.onmessage = this.receiveText;
  },
  methods: {
       sendText() {
      const inputText = this.$refs.editbar.textContent;
      this.connection.send(inputText);
    },
    receiveText(event) {
      let result = JSON.parse(event.data)
      this.autoComplete = result.shift();
      this.myJson = result
    }
  }
}
</script>

JSON returned in table:

[
    {
        "1": 0.471,
        "2": {
            "82": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        }
    },
    {
        "1": 0.47,
        "2": {
            "686": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        }
    }
]

EDIT 2 - Problem Solved

Following Nikola's guidance, I managed to resolve the issues by making adjustments based on my specific requirements. Here's the modified code:

The ForEach loop had to be placed inside the Receive Text method since this is where my JSON data receives additional information

receiveText(event) {
      let result = JSON.parse(event.data)
      this.autoComplete = result.shift();
      this.myJson = result
      this.selected = []
      this.myJson.forEach((m, i) => {return this.selected.push({i: i, state: null})})
    }

I followed Nikola's advice for the remaining adjustments, tailoring them to my existing code while implementing the recommendations.

Answer №1

If I have grasped your message correctly, perhaps it can be showcased as seen in the snippet below:

const app = Vue.createApp({
  data() {
    return {
      myJson: [{"1": 0.471, "2": {"82": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."}}, {"1": 0.47, "2"... (the remaining content preserved for brevity)
(Additional HTML and CSS code included within the original response)

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

Creating a basic jQuery button to switch an element's background color is a breeze

I'm new to this, so I hope this is a straightforward question. I'd like to use the bgtoggle class as a button to switch the background color of the metro class. I know I'm doing something wrong because it's not working as expected. Any ...

Loading images in advance before webpage loads

Welcome friends! This is my first time asking a question here, so I hope I'm doing it right :) I'm looking to preload 3 images before the webpage fully loads so that they all appear simultaneously. Currently, the images load one after another and ...

Tips on managing multiple form data using ajax for php backend

<input type="text" value="2021-05-01" class="date" name="date"> <input type="text" value="10:00" class="starttime" name="starttime"> ...

Angular Bootstrap 4 modal with responsive content dimensions

I am facing an issue where I am trying to place a media list inside a Bootstrap card. However, when I zoom in on the browser, the media list <ul/> overflows from the modal-body and goes out of the Bootstrap card. How can I ensure that the media list ...

Foundation framework enables the creation of a full width footer that stands out

I'm having trouble getting my footer panel to stretch across the entire page, it seems stuck in the center. The header at the top of the page spans the full width just fine. Am I missing something? My goal is to make both panels at the bottom of the p ...

Div element is not being styled by the second CSS class

I'm facing an issue while trying to split my html code into separate php files. I can't seem to figure out the error in my index.php file. Here is a snippet from the file: .topheader { background-color: #404040; color: white; padding: 16 ...

Adjust the Column Alignment Without Affecting the Entire Row or Table

My inquiry revolves around altering the sort direction of a particular column within a Telerik RadGrid. Utilizing Firebug, it is possible to identify and modify the sorting direction of a table (either master or detail). However, there seems to be no desig ...

How to choose an option from a dropdown menu using React

In a React application, I am creating a straightforward autocomplete feature. The code is outlined below. index.js import React from 'react'; import { render } from 'react-dom'; import Autocomplete from './Autocomplete'; co ...

Vuetify - Issue with v-tooltip not showing when the keyboard tab button is pressed

I'm facing an issue with a shared component that utilizes Vuetify's v-tooltip. It functions perfectly when hovering with the mouse, but when I try to access a button with the tooltip using the tap button, it doesn't work. Can anyone assist m ...

HTML and CSS styled accordion with nested checkboxes: handling long items

I am currently working on creating a multi-level accordion for an aside navigation section on a webpage. However, I have encountered an issue where the background color extends too far when hovering over a specific element. This occurs on a folder-type ite ...

Troubleshooting Vue: Why is my component not visible when using v-form and v-text-field components in Vuetify?

Attempting to implement the v-form and v-text-field components from the Vuetify node package. <template> <v-form> <v-text-field label="Test" type="foo"></v-text-field> <v-text-field label="bar&q ...

What is the reason behind the malfunctioning of this navigation menu?

This is a responsive navigation bar created using bootstrap. The issue I am facing is that I want the navigation items to be displayed in a single row. Here is the fixed-top navbar: Below is the navigation menu code: https://i.sstatic.net/Zqrvm.png ...

How can a button be linked directly to a particular list item?

I currently have a HTML tag within my React application that looks something like this: <ul> <li> Item 1 <button> Delete </button> </li> <li> Item 2 <button> ...

Implementing Event Listeners in Vue 3.0: A Guide to Attaching to the Parent Element

How can I attach an addEventListener to the parent element in Vue 3.x? I discovered that you can access the parent by using this code: import { getCurrentInstance, onMounted } from 'vue' onMounted(() => { console.log(getCurrentInstance() ...

Firefox changes the sequence of form input attributes

Is anyone familiar with this issue and how to prevent it? I am using AJAX to populate a div with a form from a PHP file. The input element is generated in PHP as shown below: echo ('<input type="text" name="court_type_name" id="court_type_name_in ...

"Sorry, we couldn't find the page you're looking for" message appears on a website built using Gridsome and hosted on Net

I recently launched a new website using the Gridsome framework deployed with Netlify, but I'm experiencing some issues as the site is not showing up when accessed. Instead, I keep getting an error message from Netlify: Page Not Found Looks like you& ...

What could be causing the hover effect to not work on the cards in my Svelte component? (HTML+CSS)

Greetings everyone! This is my debut post on this platform, and I'm in urgent need of assistance as I am relatively new to the world of programming. I've been trying tirelessly to implement a hover effect on these cards, but for some reason, it ...

Automating the process of running npm start on page load: A guide

Recently, I've been delving into learning npm in order to incorporate it into a website. I'm curious about how exactly it is used within a website - do you typically need to execute the command "npm start"? How does this integration work for a li ...

The wp_mail function is exclusively designed to send plain text

I have encountered an issue while using the wp_mail() function to send HTML formatted emails. Despite setting the headers to indicate "Content-Type: text/html; charset=UTF-8", all the emails appear as plain text without any formatting. While testing on Wi ...

How to rotate text direction using JavaScript and CSS

Despite my extensive efforts, I have been unable to find a solution to a seemingly simple problem. In JavaScript, I have generated dynamic text using the following code: context.fillText('name', 10, 10, 20); Now, I need this text to be ori ...