Tips for accessing a specific data value in a data table upon clicking in VueJS

I am working with a v-data-table displaying generic employee data. I'm trying to find out how I can retrieve the exact value of the cell that I click on.

This is a snippet of my current code:

Note: The CRUD-Actions Example from the official vuetify documentation is being used in this scenario: https://vuetifyjs.com/en/components/data-tables/

<v-data-table 
    :headers="headers" 
    :items="rows" 
    item-key="name" 
    :search="search" >

    <template v-slot:top>
        <!-- A dialog box for editing rows is located here -->                   
    </template>

    <template v-slot:item.actions="{ item }">
        <v-icon small class="mr-2" @click="editItem(item)">
            mdi-pencil
        </v-icon>
    </template>

</v-data-table>

Furthermore, I want to know if it's feasible to obtain the column name as well (based on the headers in the provided code). Thanks!

Answer №1

As per the documentation for v-data-table https://vuetifyjs.com/en/components/data-tables/, you can utilize the item slot:

<v-data-table
    :headers="headers"
    :items="desserts"
    :items-per-page="5"
    class="elevation-1"
  >    
    <template v-slot:item="props">
       <tr>      
          <td v-for="(prop, key) in props.item" :key="key" @click="onClickItem(key, props.item[key])">{{props.item[key]}}</td>
       </tr>
    </template>
</v-data-table>

You can retrieve the item using this method:

methods:{
  onClickItem(columName, value) {
     console.log(columName, value)
  },
}

UPDATE

If you want to add another column like actions, avoid using v-slot:item.actions slot because it will be overridden by v-slot:item. Adjust this property to achieve the desired outcome.

<v-data-table
    :headers="headers"
    :items="desserts"
    :items-per-page="5"
    class="elevation-1"
  >    
    <template v-slot:item="props">
     <tr>
      <td v-for="(prop, key) in props.item" :key="key" @click="onClickItem(key, props.item[key])">{{props.item[key]}}</td>

      <!-- This is for actions -->
      <td>
        <v-icon small class="mr-2" @click="editItem(item)">
          mdi-pencil
        </v-icon>
        <v-icon small @click="deleteItem(item)">
          mdi-delete
        </v-icon>
      </td>
     </tr>
    </template>
</v-data-table>

For further reference, check out this codepen: https://codepen.io/hans-felix/pen/bGVzoOj

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

Combine all elements into a single array using the map method in JavaScript

In my possession, I have an array of objects with IDs and names: [ { "id": 10, "name": "comedy" }, { "id": 12, "name": "documentary" }, { ...

How can I design a personalized dashboard exclusively for my client's use?

After building my client's site using Nuxt, he now wants more control over his content to be able to make changes without constantly relying on me. Do you have any suggestions on how to give him a user-friendly admin interface without using WordPress? ...

Using the Greasemonkey browser extension, one can employ the waitForKeyElements utility to trigger a function once a designated element becomes visible on the webpage

(In connection to the question I posted on Stack Overflow). I have been developing a userscript for metal-archives.com, which can be found here. When you navigate to a band page (like this example), you will see the DISCOGRAPHY section and its sub-tabs ...

Create a personalized edit button for ContentTools with a unique design

I'm struggling to figure out how to customize the appearance and location of the edit button in ContentTools, a wysiwyg editor. After some research, I learned that I can use editor.start(); and editor.stop(); to trigger page editing. However, I want ...

ways to display nested arrays in angularjs

I'm having an issue with binding and displaying the value of an array. When I assign the value using the scope variable like this: $scope.StockList = obj.data Everything works fine. However, when I try to push the value into the array like this $ ...

Having difficulty positioning the dropdown above the other elements in the body

Below, you'll notice that the dropdown menu isn't positioned correctly over other body elements like the timer. I'm utilizing bootstrap for the dropdown and redcountdown js for the timer. https://i.sstatic.net/OO8Jm.png Here is the HTML: & ...

Dynamic inheritance in Node.js based on the version being used

Why does the code provided only function correctly in Node.js versions 5.x and 6.x, but not in versions 4.x and older? Is there a way to modify the code so that it can work across Node.js versions 0.10.x - 6.x? 'use strict'; var util = require ...

The Admob platform is displaying test ads, but my actual ads are not appearing

I'm currently using Admob to display Android ads in my app. Strangely, my actual ads are not appearing, but when I input the test code, it works perfectly fine. Interestingly, when I tested my ads in a different game, they displayed as intended. It&ap ...

Viewing Image in Modal on Click

Despite numerous attempts, I have been unable to make this work, even after trying various examples. The issue at hand is with a list of imageURL's that are displayed on the webpage. The goal is to allow users to click on an image, which would then o ...

React component props are failing to update

import React,{useEffect} from 'react' import { Pin, Group } from './Pin'; import Cluster from './Cluster'; function DisplayClusterGroup({textAsIcon,iconStyle,mapRef,onViewportChange,dataSet,icon,onClick,clickedMarkers }) { ...

Retrieve a parameter from jQuery and pass it to a JavaScript function

Currently, I am working with Puppeteer and encountering an issue where the "selector" is undefined when calling this function. async function checkIfTextIsPresent(page, selector){ let myElement = await page.evaluate(() => document.querySelector(sel ...

Loading vue.config.js Asynchronously for Pre-Rendering Meta Data

I am facing an issue with asynchronously loading data in my Vue.js application's config for use with Chris Fritz's PrerenderSPA webpack plugin. It seems that the routes are not being pre-rendered as expected. When I manually input the values, th ...

Unable to retrieve custom CSS and JS files hosted on the server

Encountering Server Issue My server is returning a 404 NOT FOUND error when trying to access my static files: css and js. In IntelliJ IDEA, the path appears correct as shown in the image https://i.stack.imgur.com/nTFv9.png. However, upon accessing the pa ...

"Efficiently handling asynchronous calls with Angular and Node.js $scope

As I am new to Angular, I have encountered a problem that requires some assistance. My task involves creating a selection of clickable items. When an item is clicked, the page should display its properties such as description. While the initial part is rel ...

Is it possible to transfer files using web-bluetooth technology?

As I work on developing an embedded system that counts the number of cars, saves their speed and time data in a logs file using rsyslog. Simultaneously, I am creating a web-API (in Typescript/Angular with Electron for Desktop usage and later Web as well) t ...

What is the best way to use JavaScript to fill in missing images with alternative HTML content?

As a provider of a service that helps users find the location of pictures, I face a challenge due to the fact that the pictures are stored on another internal site. Some users may not have access to this site as my service offers additional information. Th ...

The property 'get' cannot be accessed because the axios module of vue__WEBPACK_IMPORTED_MODULE_0__ is undefined

Having some trouble with Vue and Axios - encountering the following error message: [Vue warn]: Error in created hook: "TypeError: can't access property "get", vue__WEBPACK_IMPORTED_MODULE_0__.default.axios is undefined" Here's the code snippet: ...

Activating the loader dismiss command will transition the swiper to the current page

Having a swiper and loader makes the scenario very straightforward. The loader is initialized whenever calculations are performed, and after successfully obtaining the result, the loader turns off and swipes to the second slide. <swiper-container #sl ...

Google Maps problem: cross-origin framing is not allowed

I've encountered an issue with the custom Google Maps embedded using iframe. The error message I'm getting is: does not permit cross-origin framing. Instead of displaying a blank iframe, I would like to change it to show a "map cannot be disp ...

Preventing the horizontal scrolling of my application's sticky header

My application layout can be viewed here: http://jsfiddle.net/rhgyLjvx/ The layout includes a main header (red), sticky section header (dark blue), fixed footer (grey), and fixed left side nav (green). The application should have full scroll bars on both ...