Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
Is there any way to select specific "tr" in a "div" created with data from json?

I created a table in a div only with Json.

The div looks like this:

                    <div id="datatable" class="datatable" data-mdb-hover="true"
                         data-mdb-border-color="dark"
                         data-mdb-full-pagination="true"
                         data-mdb-clickable-rows="true"

                    >
                    </div>

then I create the table:

 const columns = [
            {label: 'ID', field: 'id'},
            {label: 'Role', field: 'role'},
        ];

    const asyncTable = new mdb.Datatable(
        document.getElementById('datatable'),
        {columns,},
        {loading: true}
    )

    fetch('/api/roles')
        .then((response) => response.json())
        .then((data) => {
            asyncTable.update(
                {
                    rows: data.map((roles) => ({
                        ...roles,
                        name: `${roles.id}`,
                        role: `${roles.name}`,
                    })),
                },
                {loading: false}
            );
        });

Now I try to change the class to "active" if I click on a tr like so:

    // function to select the row
    const table = document.getElementById('datatable');
    table.addEventListener('rowClick.mdb.datatable', (e) => {

        const selected_item_id = e.row.id
        //console.log(e);
        //event.target._setClassNames("active");
        event.target.querySelector("tbody tr").className += "active"
        activeT.push(selected_item_id)
    });

It is working but only in the First tr. if I click on the 2nd tr it adds the "active" to the first tr again..

In the browserconsole is this after 2 clicks. Like you can see the double active here:

<tr scope="row" data-mdb-index="0" class="activeactive"><td style="" class="" data-mdb-field="id" false="">18</td><td style="" class="" data-mdb-field="role" false="">Admin</td></tr>

Maybe anyone is able to help me!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this

<script>
                $('tr').onclick(function (e) {
                    $(this).toggleClass('active');
                })
            </script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda