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

120
Visualizações
Multiple Tables on Datatables with different option

I have multiple table (tbUser, tbRole, tbJob) and i want to make my code simple.

this is what i done before:

var userTable = $('#tbUser').DataTable();
var roleTable = $('#tbRole').DataTable();
var jobTable = $('#tbJob').DataTable();

Each tables has different options, columns and have one thing in common has column [action] to put View/Edit/Remove button. Is there a simple way to do jquery event click action button in each table.

This is my code:

$('#tbUser tbody').on('click', '#btn_edit', function (e) {
  let index = $(this).parents('tr');
  let data = userTable.row(index).data();

  /** Something */
});

/** REPEAT EACH TABLE */

and I've tried :

$('table tbody').on('click', '#btn_edit', function (e) {
  let index = $(this).parents('tr');
  let data = userTable.row(index).data(); //===> But how to change table dynamicly on this line

  /** Something */
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Firstly your edit button needs to be targetted using a class not an ID, otherwise it will only ever find the first button.

Create an object that holds a reference to each of your tables. I'm using the table id as the key, and the instantiated datatable as the value.

const tables = {
    tbUser: userTable,
    tbRole: roleTable,
    tbJob: jobTable
}

Then with your button click, identify which table it is part of and use that to grab the table instantiation from the object you created earlier

$('table tbody').on('click', '.btn_edit', function (e) {
    const tableId = this.closest('table').id;
    const datatable = tables[tableId];

    const index = $(this).parents('tr');
    const data = datatable.row(index).data();

    /** Something */
});
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