Vue.js - 更改v-for中单个表行的类

boj*_*259 2 javascript twitter-bootstrap vue.js

我正在显示使用v-for动态生成的客户表.每行都有一个按钮,用于将客户ID添加到将发送到API的对象中.问题是,我想将Bootstrap .success类添加到单击的行,因此用户知道客户已被选中,但我只能实现表中的所有行都获得.success类.此外,我希望当用户点击另一个客户时,所选客户将失去.success类.这是我的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

Eri*_*AND 7

success根据selectedCustomer值将类绑定到行应该可以帮助您实现所需的内容.这样的事情,未经测试:

<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>
Run Code Online (Sandbox Code Playgroud)