Matheus Barem
Matheus Barem

Reputation: 1627

vuetify v-radio doesn't select one radio for click in data-table

I'm using Vuetify and I have a v-data-table. I want to select a client using v-radio but when I click on the radio all the radios in my v-data-table are selected. I need to click on a radio button and only one has be the selected.

html:

 <v-data-table :headers="headersAllClientesCadastrados" :items="AllClientesCadastrados" :search="searchAllClientesCadastrados">
                    <template v-slot:item="row">
                      <tr>
                        <td>
                          <v-radio-group v-model="ex8">
                            <v-radio
                              color="primary"
                              @click="selecionaCliente(row.item)"
                            ></v-radio>
                          </v-radio-group>
                        </td>
                        <td>{{ row.item.codigo }}</td>
                        <td>{{ row.item.empresa }}</td>
                        <td>{{ row.item.responsavel }}</td>
                        <td>{{ row.item.email }}</td>
                      </tr>
                    </template>
                  </v-data-table>

script:

export default {
    name: "NewDI",
     data: function () {
        return {
         ex8: 'primary',
         headersAllClientesCadastrados: [ 
        { text: '', value: '' },
      { text: "Código", value: "numCotation", },
      { text: "Empresa", value: "provider" },
      { text: "Responsável", value: "refItem" },
      { text: "Email", value: "precoFOB" },
      ],
       searchAllClientesCadastrados: '',
       AllClientesCadastrados: [],
        }
      }
     methods: {
        selecionaCliente(row){
          console.log('selected', row);
        },
     }
  }

Upvotes: 1

Views: 1273

Answers (1)

Dan
Dan

Reputation: 63099

You should use the props show-select, single-select="true", and item-key to do this more easily using built-in behavior. It uses checkbox but it is essentially the same thing:

<v-data-table
   show-select
   single-select="true"
   item-key="email"
>
</v-data-table>

Here is a demo:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      headers: [
        {
          text: 'Dessert (100g serving)',
          align: 'start',
          sortable: false,
          value: 'name',
        },
        { text: 'Calories', value: 'calories' },
        { text: 'Fat (g)', value: 'fat' },
        { text: 'Carbs (g)', value: 'carbs' },
        { text: 'Protein (g)', value: 'protein' },
        { text: 'Iron (%)', value: 'iron' },
      ],
      desserts: [
        {
          name: 'Frozen Yogurt',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: '1%',
        },
        {
          name: 'Ice cream sandwich',
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: '1%',
        },
        {
          name: 'Eclair',
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: '7%',
        },
        {
          name: 'Cupcake',
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: '8%',
        }
      ],
    }
  },
})
<div id="app">
  <v-app id="inspire">
    <v-data-table
      :headers="headers"
      :items="desserts"
      single-select="true"
      item-key="name"
      show-select
      class="elevation-1"
    >
    </v-data-table>
  </v-app>
</div>


<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>

<!-- begin snippet: js hide: true console: false babel: false -->

Upvotes: 3

Related Questions