Manipulating tables in Vue.js is made easy due to its flexibility. One useful feature is the ability to

I have a dynamic table displaying customers generated using the v-for directive. Each row contains a button that adds the customer ID to an object sent to the API. I want to apply the Bootstrap .success class to only the clicked row, indicating it has been selected. Currently, all rows in the table receive the .success class. Additionally, when a user clicks on another customer, I would like the previously selected customer to lose the .success class. Here is the code snippet:

<table class="table table-responsive table-striped table-hover">
<tbody>
  <tr v-for="customer in customers">
    <td><button class="btn btn-default" v-on:click.prevent="selectCustomer(customer.id)"><i class="glyphicon glyphicon-ok"></i></button></td>
    <td>{{ customer.first_name }}</td>
    <td>{{ customer.last_name }}</td>
    <td>{{ customer.oib }}</td>
    <td>{{ customer.phone }}</td>
    <td>{{ customer.email }}</td>
    <td>{{ customer.street }} {{ customer.city }}, {{ customer.country }}</td>
  </tr>
</tbody>

export default {
data(){
  return {
    customers: [],
    selectedCustomer: ''
  }
},
methods: {
  selectCustomer(id, clicked){
    this.selectedCustomer = id;
    console.log(this.selectedCustomer);
  }
}

Appreciate any help on this matter!

Answer №1

To achieve the desired outcome, you can apply the success class to the row based on the value of selectedCustomer. Here is an example code snippet:

<table class="table table-responsive table-striped table-hover">
<tbody>
  <tr v-for="customer in customers" v-bind:class="{'success': (customer.id == selectedCustomer)}">
    <td><button class="btn btn-default" v-on:click.prevent="selectCustomer(customer.id)"><i class="glyphicon glyphicon-ok"></i></button></td>
    <td>{{ customer.first_name }}</td>
    <td>{{ customer.last_name }}</td>
    <td>{{ customer.oib }}</td>
    <td>{{ customer.phone }}</td>
    <td>{{ customer.email }}</td>
    <td>{{ customer.street }} {{ customer.city }}, {{ customer.country }}</td>
  </tr>
</tbody>

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

Whenever text is present, the sides of my box model, constructed using HTML5 and CSS3, are always pushed downward

When I add extra text to the body of my homepage, it causes a distortion and pushes down the sidebar and advertising boxes on the side. I'm working on this project for class and even though I've asked my teacher for help, she says the code is fin ...

Embedding hyperlinks in vue.js

As I scrape a website, I am pulling data from a JSON file and trying to display it in an HTML table within a component file in Vue.js. One of the values I want to display as an href link. However, the value {{ row[8] }} is in a format that Vue.js cannot re ...

Is there a way to bypass the initial result when using document.querySelectorAll?

this is my own unique html content <div class="content-body"> <table style="text-align:center;" class="table table-bordered"> <tbody> <tr> <th>Text Line</th> </tr> <tr> <td> ...

Using AngularJS to dynamically assign classes with values derived from an ng-repeat loop

I'm attempting to assign a class to a table row using ng-class based on the value of ng-repeat. I have searched online and found examples that involve calling a function. Is it possible to simply set ng-class with a value instead of using a function? ...

``There seems to be an issue with the visibility of Three.js OBJ MTL Loader

Utilizing the obj+mtl loader to import my OBJ files into the scene and loading textures from the mtl file. For example: newmtl initialShadingGroup illum 4 Kd 1.00 1.00 1.00 Ka 0.00 0.00 0.00 Tf 1.00 1.00 1.00 map_Kd 6922529901031.jpg map_Bump 69225299010 ...

Creating a comparison display using ng-repeat

I have a JSON file that contains revision and history information about a modified entity, including its old and new values. The diff is generated using the jsondiffpatch library and I have parsed it further to create the final JSON format for rendering. ...

Is it recommended to update my peer dependencies when upgrading my RN version?

Recently, I took on the task of updating my old project from RN 0.61.x to 0.70.x and react 16 to react 18. During this process, I encountered a challenge with dependencies that were tied to older versions of React Native in their peer dependencies. This is ...

What is the correct way to exclude and remove a portion of the value within an object using TypeScript?

The function useHider was created to conceal specific values from an object with the correct type. For example, using const res = useHider({ id: 1, title: "hi"}, "id"), will result in { title: "hi" } being returned. Attempting ...

Fixing the "Deprecated XMLHttp" Problem: Step-By-Step Guide

Currently, I am setting up a system for a small workplace where I am implementing live search functionality on keyup to display a table of users. Each user row contains a button that triggers an Ajax search for that particular user's clock-in hours. H ...

Iterating over an array and displaying elements on the webpage

Struggling to access an array and loop through it, but only able to print out one element instead of all. Need a hint on how to solve this issue. See my code below: let toppingsCount; const burger = document.createElement('div'); const toppingsD ...

JavaScript seems to have difficulty correctly parsing objects from JSON

Having trouble populating a Repeat Box with objects from a JSON document. Despite my efforts, it keeps duplicating the first item instead of adding each one individually. Any ideas on what might be causing this issue? Below is the code I am currently usin ...

How can one efficiently prevent an O(n) callback in Node.js from congesting the event loop?

Currently, I am in the process of developing a node server for a basic social media platform. This server facilitates fundamental CRUD operations for posts, comments, and likes. My upcoming task involves implementing a notification service to disseminate n ...

The current date is cycling back to the month before

There is a datetime received from my api as 2018-09-01T00:00:00.000Z, referred to as frame.scandate. Another date is generated within the program as 2018-09, simply known as scandate. These examples can represent any year/month combination. In my code: ...

A step-by-step guide to creating a custom JavaScript/AJAX function

Hello everyone, I am new to the world of Ajax and JavaScript, so please bear with me as I ask my question. I have a link on my website that opens a new window using JavaScript. In this new window, I have a form that submits data to my database using PHP. ...

The cursor is located on the right side instead of the usual left side

Why is the cursor positioned on the right side rather than the left side? $('.legoact').focus(); When the cursor is positioned on the right side, it can be achieved using the following CSS properties: .lego{ background:#ddd; width:70%; m ...

Is it necessary for Vue to download all pages on the client side?

Greetings! I'm currently delving into the world of Vue and SPA. From my understanding, Vue compiles everything you need into a folder which is then placed on your server. My question is whether all the content from this compilation is downloaded on t ...

How can you effectively set up a Next.js web application by integrating data from an API and storing it in the state for optimal performance?

Currently, my state management system is Zustand, but I believe the approach I am discussing can be applied to other state management libraries like Redux or Recoil. I have considered and experimented with the following: My initial attempt involved fetchi ...

Some places may not have detailed information available when using the Google Places API

Greetings I am currently in the process of developing a page on my website that utilizes the Google Places API along with the user's geographical coordinates (latitude, longitude) to locate nearby restaurants. In my code, I have implemented a functio ...

The reactjs-toolbox radio button group remains unchanged

In my React application, I have implemented radio buttons using the following code: <RadioGroup name='steptype' className={css.ProcessStepRadioButtons} value={this.state.stepsData[stepNumber].stepType} onChang ...

Why am I encountering http://localhost:3000/api/auth/error?error=AccessDenied when implementing Google signin in my Next.js application?

Can someone please assist me in resolving an issue I am facing with my NextJs application using Google signin? Whenever I try to sign in, I get the error message "http://localhost:3000/api/auth/error?error=AccessDenied". Why is this happening? Here is a ...