Steps to include a data-xx attribute in the displayed <table> within a Vuetify 2 v-simple-table

I am facing an issue where I want to include an HTML data-xxxx attribute to the HTML <table> within a v-simple-table. However, when I add the data attribute to the <v-simple-table>, it ends up being placed in a surrounding div two levels above the actual <table>.

I rely on data attributes for targeting components in my testing framework. In my Page Object Model (POM), I would ideally like to do:

get tableResultsAvailable () {
    return $('table[data-qaid="results_available-panel"]');
}

In one of my pages, I might have something like this:

        <v-skeleton-loader v-if="loading" type="table-tbody"
                           :types="{ 'table-tbody': 'table-row-divider@4', 'table-row': 'table-cell@3', }"/>
        <v-simple-table v-else data-qaid="results_available-panel">
            <thead>

When writing test specifications, I may need to wait for the table to load:

    await expect(MyAccountPage.tableResultsAvailable).toBeDisplayed()

However, the data attribute is actually attached to a div instead of the table:

<div data-v-b386ddc8="" class="v-data-table v-data-table--fixed-height theme--light" data-qaid="results_available-panel">
    <div class="v-data-table__wrapper" style="height: 395px;">
        <table>
            <thead data-v-b386ddc8="">

This forces me to use a broader selector in my POM such as

'[data-qaid="results_available-panel"]'
, or resort to
'div[data-qaid="results_available-panel"]'
, which is specific to the Vuetify structure and may change in the future, neither of which are ideal.

I am contemplating using the default slot to override the entire table definition, but that feels like an excessive solution. Additionally, I have observed that CSS classes are also applied to the wrapper in a similar manner.

Answer №1

After examining the code, it appears that there is no direct way to access the <table> element programmatically. Although you cannot override the table via the default slot, there exists an undocumented wrapper slot:

<v-simple-table>
  <template #wrapper>
    <div class="v-data-table__wrapper">
      <table data-qaid="results_available-panel">
        ... (previous default slot content goes here)
      <table>
    </div>
  </template>
</v-simple-table>

However, if overriding the entire wrapper is not suitable for you, another approach is to set the attribute using pure JS, such as through a ref:

mounted(){
  this.$refs.wrapper.$el.querySelector('table').dataset['quaid'] = 'results_available-panel'
}

This method can be considered fragile since it involves direct DOM manipulation on a node controlled by Vue.

In either of these methods, your reliance is on the internal structure of VSimpleTable, so using the adjusted selector may be the most straightforward solution.

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  mounted(){
    this.$refs.wrapper.$el.querySelector('table').dataset['prop'] = 'asdf'
  }
})
*[data-prop="asdf"]{
  background: red;
}
<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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="57313839231761792f">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3959686978a859aa3d1cd9b">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet">

  <div id="app">
    <v-app>
      <v-main>
        <v-container>

        Table with wrapper slot:
        <v-simple-table>
          <template #wrapper>
            <div class="v-data-table__wrapper">
              <table data-prop="asdf">
                <tr><td>Table content</td><tr>
              </table>
            </div>
          </template>
        </v-simple-table>

        Table with ref:
        <v-simple-table ref="wrapper">
          <template #default>
            <tbody>
              <tr><td>Table content</td><tr>
            </tbody>
          </template>
        </v-simple-table>

        </v-container>
      </v-main>
    </v-app>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a2c2f3f1a687422">[email protected]</a>/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23555646557a57585584">[email protected]</a>/dist/vuetify.js"></script>

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

I need the title to be filled with the input data and the content to be filled with the textarea data

import React from 'react'; export default class CreateNote extend React.component { constructor(props) { super(props); this.state = {note:{title:" ",content:" "} }; console.log(this.state); ...

Struggling to clear items from input field within AngularJS

I'm currently facing an issue where I am unable to delete data from an input field. var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', function MyCtrl($scope) { $scope.items = [ { name: & ...

Merge JavaScript files from various folders using grunt's configuration settings

I am currently working with Grunt and Sass, and I am in search of a SASS-like feature that will allow me to import any JavaScript file I desire and merge them into a single file based on some configuration depending on the directory I am in. For instance, ...

How can you selectively export a single function from a JavaScript file?

Within my project, I have two separate modules - one written in ts and the other in js. There is a utility within the js module that needs to be accessed by the ts module. The utility service.js looks like this: module.exports = { helloFriends: functi ...

Trouble with passing a nodejs variable into a nested function

I've been working on the code below but I'm having trouble getting the inner function .each to successfully call res.write with the results from the MongoDB query. At first, I thought the issue might be related to the variable res not being glob ...

Experience running two functions simultaneously in jQuery simulation

Presented here are two functions that I am working with: loadTop(); loadBottom(); One of these functions is responsible for loading the top portion of a page while the other takes care of loading the bottom. The loadTop function involves asynchronous o ...

Navigate to a new page on button click using Row with Tanstack / React-Table and Typescript (2339)

Encountering a linting error when attempting to navigate to a new route by clicking on a table row. The functionality is working but how can I resolve this issue? It's showing an error message stating "The property "id" for type TData does not exist." ...

Mastering Inter-Composable Communication in Vue 3: A Guide

Composables in Vue documentation demonstrate how small composition functions can be used for organizing code by composing the app. Discover More About Extracting Composables for Code Organization "Extracted composables act as component-scoped servi ...

What are some ways to enhance the speed of my canvas animations?

My code generates imagedata and places it in a canvas. However, when I expand the window size, the rendering slows down significantly. Is there a way to optimize this for smooth operation in fullscreen mode, even though it might seem naive? What would be ...

Troubleshooting $digest problems with AngularJS and the selectize directive

I am encountering difficulties when utilizing the watch function within a directive in conjunction with a third-party plugin named selectize. Despite researching extensively about $digest and $watch, I am still facing issues. Although my example below is ...

Using Vue 3 or Nuxt, the v-if directive and watch property can cause issues with initializing

Visit this link: Currently using Nuxt 3.4.2 along with Supabase, I have implemented skeleton loading and pagination features. However, after adding these, the Flowbite off-canvas drawer stopped working properly. The issue arises when the data-drawer-targe ...

What is the solution for the red underline issue with the @json Blade directive in VSC?

Everything seems to be functioning correctly with these variables, but I'm curious as to why they are underlined in red. Does anyone have a solution for this issue? Error message ...

The specified key is not found in the 'StoreOptions<State>' type of Vuex 4, in combination with Vue 3 and Typescript

I'm navigating the process of setting up a Vuex 4 Store with Typescript and Vue3, despite having limited experience with typescript. Following the Vuex Tutorial for the initial setup, I almost entirely copy-pasted the code. The only difference is tha ...

Using `await` is only permitted in an asynchronous function within Node.js

I've been working with node and express to develop a server for my application. Here is a snippet of my code: async function _prepareDetails(activityId, dealId) { var offerInfo; var details = []; client.connect(function(err) { assert.equ ...

Forget the function once it has been clicked

I'm looking for a solution to my resizing function issue. When I press a button, the function opens two columns outside of the window, but I want to reset this function when I click on another div. Is there a way to remove or clear the function from m ...

Steps for customizing the dropdown arrow background color in react-native-material-dropdown-v2-fixed

Currently, I am utilizing react-native-material-dropdown-v2-fixed and I am looking to modify the background color of the dropdown arrow. Is there a way for me to change its color? It is currently displaying as dark gray. https://i.stack.imgur.com/JKy97.pn ...

Is there a way to dynamically add <td> based on the quantity of values in a JSON object?

I have developed a program that retrieves values from JSON and I aim to display these values in a table. Currently, only the last array value is being displayed in the table even though all values are available in the console. The objective now is to dynam ...

Can JavaScript be executed from the console while navigating between pages?

I am facing a challenge that I can't find any solutions for online or elsewhere. The task at hand seems straightforward. I am using the Chrome console and attempting to run Javascript code to navigate between pages with pauses in between. It feels lik ...

Removing filters dynamically from ng-repeat in Angular

I am currently using ng-repeat to display a list of staff members: <div ng-repeat="user in staff | profAndDr"> There is also a custom filter called 'profAndDr' that only shows people with the titles "Dr." and "Prof.": app.filter('pr ...

What is the best way to deliver data from the main Vue instance to components that are being utilized by vue-router?

Currently, I am utilizing vue-router within a single HTML file instead of using separate file components. I am encountering an issue where I am unsure how to pass the data stored in the Vue data object to the component when loading it for a route. Oddly e ...