Adding style using CSS to a dynamically generated table row in Vue.js

Currently, I am working with a table that dynamically generates rows using v-for:

<table>
  <tr><th class='name'>Name</th><th>Surname</th></tr>
  <tr v-for='data in datas'><td class='name'>@{{data.name}}</td><td>@{{data.surname}}</td></tr>
</table>

I am facing an issue where I need to hide the column with the class 'name' using jQuery. However, when I use

$('.name').hide();

Only the header disappears. This could be happening because the row is generated dynamically. How can I resolve this?

I have tried:

  1. Iterating through each element with the class using the each() function
  2. Applying CSS property .css('display','none') instead of hide()

But these solutions did not work. Interestingly, the alert() within each() triggers as expected, but hide() does not affect the added elements.

Here is more specific data about the table layout:

<table class="striped bordered responsive">
     <thead>
         <tr><th class="stat-table-creatives" colspan="2">Creative info</th><th>Impressions</th><th>Clicks</th><th>CTR</th><th>CPC</th><th>Price per unit</th><th>Sum</th><th class="stat-table-date">Date</th></tr>
      </thead>
      <tbody>
         <tr v-for="stat in stats"><td class="stat-table-creatives">@{{ stat.creativeTitle }}</td><td class="stat-table-creatives">@{{ stat.creativeType }}</td><td>@{{ stat.impressions }}</td><td>@{{ stat.clicks }}</td><td>@{{ stat.ctr }}</td><td>@{{ stat.cpc }}</td><td>@{{ stat.client_price }}</td><td>@{{ stat.sum }}</td><td class="stat-table-date">@{{ stat.date }}</td></tr>
      </tbody>
</table>

The following function is triggered on button click:

getJsonForStats: function(filters){
    if((filters[0]=='creative')||(filters[1]=='creative')){
        $('.stat-table-creatives').show();
    } else {
        $('.stat-table-creatives').hide();
    }
    if((filters[0]=='date')||(filters[1]=='date')){
        $('.stat-table-date').show();
    } else {
        $('.stat-table-date').hide();
    }
});

This function is called from another function, which is invoked on v-on:click event

Answer №1

Vue dynamically created tables are compatible with jQuery. Take a look at this demo on jsfiddle: https://jsfiddle.net/crabbly/9o69f2yr/

If you encounter issues, it could be related to the loading of your application or how filters are implemented.

When accessing table rows, ensure that the DOM is fully updated before querying it. You can utilize Vue's nextTick method (http://vuejs.org/api/#Vue-nextTick). Within your Vue method, prior to hiding rows as shown in your example by calling the getJsonForStats method, you can do:

this.$nextTick(function() {
   //DOM is updated and ready for action
   this.getJsonForStats(..your filter params...);
}

Answer №2

It seems like the issue may be due to using the same class for both <th> and <td> elements.

You can try using a different class for the header, such as <th class="name-th">, and then hiding it using $(".name-th").hide(); instead of $(".name").hide();. This change might resolve the problem.

UPDATE:

I'm not entirely sure why you're experiencing issues because the following code works perfectly:

<table id="tab" border='1'>
    <tr><th class='name'>Name</th><th>Surname</th></tr>
    <tr><td class='name'>n</td><td>s</td></tr>
</table>
<input id="click" type="button" value="Hide" />

Javascript:

$(document).ready(function(){
    $("#click").click(function(){
        $("#tab").find(".name").hide();
    });
});

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 am having difficulty with my JavaScript code not being able to interpret the JSON output from my PHP code. Can anyone help me troubleshoot

Having trouble working with an AJAX call and handling the JSON object it generates in JavaScript? Here's a sample snippet of PHP code returning the JSON object: echo json_encode(array("results" => array(array("user" => $member['user'] ...

"Implementing automated default values for Select/dropdown lists in ReactJs, with the added capability to manually revert back to the default selection

After browsing multiple websites, I couldn't find a clear solution on how to both set and select a default value in a select element. Most resources only explain how to set the value, without addressing how to reselect the default value. My Requireme ...

What could be causing my dropdown links to malfunction on the desktop version?

I've been developing a responsive website and encountering an issue. In desktop view, the icon on the far right (known as "dropdown-btn") is supposed to activate a dropdown menu with contact links. However, for some unknown reason, the links are not f ...

Gradually reveal each individual path of the SVG, one by one

I am faced with an SVG containing numerous paths like the following: <svg version="1.1" id="svg2" inkscape:version="0.91 r13725" sodipodi:docname="drawing.svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:svg="http://w ...

Having trouble retrieving the value of the hidden field using ng-model

Hello, I'm currently learning AngularJS and facing some challenges with accessing hidden field values using ng-model. Specifically, I am working on an editing modal where I need to retrieve the ID for each record. Below is my controller code snippet: ...

The position of the HTML class shifts when the window is resized

I am in the process of creating a basic website layout that resembles this design. However, I am facing an issue where the webpage does not adapt well to different window sizes. Specifically, the elements with the classes .gameInfo and .playerList overlap ...

Is there a way to verify the content of a span and automatically guide the user elsewhere if it meets certain criteria?

Currently, I am facing an issue with adding Google authentication to my website as the redirect function is not working properly. As a solution, I plan to implement manual redirection using JavaScript in case the value of the span element is <span id= ...

Guide on executing YUI tests in headless mode and recording outcomes in a log document

I currently have some YUI tests that I need to run in headless mode. Right now, these tests are executed by launching their corresponding TestFileName.html. As a result, the browser displays the test results with green and red icons along with messages ind ...

Tips for incorporating a value within the AngularJS select function

Having an issue with passing a list value in the html select method using AngularJS. Here is my code: app.js $scope.subcategory = function() { var query = "SELECT unit FROM Length;"; $cordovaSQLite.execute(db, query).then(function(res) { ...

How to Perform a Redirect to a Named Route in Vue.js?

Currently, I am in the process of working on a Vue.js project and have set up a route with the following configuration: { path: '/Cart', component: Cart, props: (route) => ({ payment_id: route.query.payment_id, payment_request_id: ...

Tips for closing 2 models in a React application

Currently, I am working on a project using ReactJs. One of the functionalities I am implementing is generating a user's gallery when the user clicks on their avatar. To achieve this, I am utilizing the react-grid-gallery package along with semantic ui ...

An error occurred while trying to retrieve the message from the JSON data in PHP/AJ

i have created a form in a PHP file and I am sending a request to another PHP file (test2.php) using AJAX. However, when I receive the data back in AJAX and try to print it in id="txtHint2" (paragraph tag), it shows as undefined. What could be causing th ...

Dragging and dropping elements onto the screen is causing them to overlap when trying

I am having trouble merging the drag and drop functionality from Angular Material with resizing in a table. Currently, both features are conflicting and overlapping each other. What I am hoping for is to automatically cancel resizing once drag and drop s ...

Consolidate radio group in Vuetify platform

I'm having trouble centering the v-radio-group. Here's my current setup: <v-container grid-list-md text-xs-center> <v-form ref="form"> <div v-if="question.question_type == 'YESNO' "> <v-radio-group ...

Ways to ensure a ul li element perfectly fits inside a div element without any spaces

As a newcomer to CSS and HTML, I'm facing an issue with my ul li dropdown menu and login boxes. The background colors are red and blue, but there are gaps within the div that I want to fill with the height of the div. Below is the code snippet: @key ...

Execute animation prior to displaying the image

I have developed a custom lightbox that features the smooth transition of images when users click on next/previous buttons. However, my current challenge lies in animating the width and height of the lightbox before the image actually appears. $('im ...

Is it possible to incorporate Nth child into JavaScript?

Is it feasible to implement an Nth Child in the following code snippet? $(function() { var count = $(".parent a").length; $(".parent div").width(function(){ return ($(".parent").width()/count)-5; }).css("margin-right","5px"); }); ...

What is the best method for extracting a particular value from my dataset?

I'm interested in creating a variable that stores the IsUserSiteOwner value from the data. Can someone help me with this? Any suggestions on how I can achieve this task? ...

Generating Multilayered PDF files with JavaScript in NodeJS

After reviewing the documentation for PDFMake, PDFKit, and WPS: PostScript for the Web, I couldn't find any information beyond background layers. It seems like Optional Content Groups might be what I need, but I'm unsure how to handle them using ...

Can Ember store in Route handle Post requests?

Is there a way to use ember store functionality similar to this.store.findAll('report') for making POST requests with postObj in my route? Also, how should I handle the response received from these requests? Right now, I am sending ajax POST requ ...