Expandable Table

Asked

Viewed 716 times

3

In this script below https://jsfiddle.net/gatkzgtw/4/ I have a table that expands when we click on the line. when expanding it shows me a second table. the problem is that the script is repeating itself in this second table inpedindo to show all the lines, in this second table if we just click there and show the rest of the lines, how can I make the script only work the expantion in the first table?

1 answer

4


The best thing would be to do this with CSS and give the classes on the server when you generate this HTML. But to answer your question and do this via Javascript I suggest some changes.

Usa thead and tbody, especially in this case where you have tables inside tables the selectors are more accurate. It also uses direct descending selector > who avoids finding others tr inside the table that is inside a tr.

So you can use:

$(document).ready(function () {
    $("#report > tbody > tr").hide();
    $("#report > tbody > tr:even").addClass("odd").show();

    $("#report tr.odd").click(function () {
        $(this).next("tr").toggle();
        $(this).find(".arrow").toggleClass("up");
    });
});

jsFiddle: https://jsfiddle.net/gxr47dqr/

and to be part of it in CSS you can do it like this: https://jsfiddle.net/gxr47dqr/2/

  • Perfect that same, just a doubt... because you think it best to do in CSS?

  • @Fabiohenrique CSS is faster than Javascript and thus saves lines of code and processing. For example these tables will be with nothing hidden until the browser runs Javascript, when if you had these hides/shows in the CSS it would load right with the page.

  • Would you have some model example?

  • @Fabiohenrique I added an example in the answer

  • Very good I will do for this ..... Thanks brother

Browser other questions tagged

You are not signed in. Login or sign up in order to post.