Search code examples
vue.jsvuetify.jsv-data-table

@click:row not triggered in v-data-table (Vuetify)


I have a v-data-table from vuetify where I have two click-events. One is triggered (deleteBooking), the other not (booking_clicked)?! The button is to delete an item. The row-click event is to show the user additional information for the clicked row.

<v-data-table
    item-key="id"
    v-model="selected_bookings"
    :loading="isLoading"
    :items="tmp_bookings"
    :headers="headers"
    :single-select="single_select"
    :hide-default-footer="true"
    @click:row="booking_clicked"
    :footer-props="{
        'items-per-page-options': [-1]
    }"
    class="elevation-1">
        <template slot="item" slot-scope="row">
            <tr>
                <td>{{row.item.booking_text}}</td>
                <td>
                    <v-btn class="mx-2" dark small color="red" @click="deleteBooking(row.item)">
                       <v-icon dark>mdi-delete-forever</v-icon>
                    </v-btn>
                </td>
            </tr>
        </template>
</v-data-table>

booking_clicked(item){
 console.log(item);
}

deleteBooking(booking_item) {
  console.log(booking_item);
},

Solution

  • Try setting up the row click like this:

    @click:row="(item) => booking_clicked(item)"
    

    Update

    Check this codesanbox I made: https://codesandbox.io/s/stack-70203336-32vby?file=/src/components/DataTableActions.vue

    If you want to add an extra column to your v-data-table there's no need to use the main ìtem slot, unless you want to modify the whole row. If all you want to do is add like example a delete button. You just need to add it to the headers array first. On my example I added an extra column for actions.

    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' },
       { text: 'Actions', value: 'actions'}
    ],
    

    After that, all you need to do is customize the slot for the actions column, inside your v-data-table. Also, to avoid the @click:row effect when you click your delete button you need to use @click.stop on your delete button.

    <v-data-table
       :headers="headers"
       :items="desserts"
       :items-per-page="5"
       class="elevation-1"
       @click:row="(item) => dessertRowlicked(item)"
    >
       <template  #item.actions="{ item }">
          <v-icon color="primary" @click.stop="deleteDessert(item)">mdi-delete</v-icon>
       </template>
    </v-data-table>