Clicking on a button and get the row values of a table

0

I'm doing a project in nodejs / javascript and I'm trying to get the values that are in the row of a table. When clicking on the button on this line will get me these values and anger sends them to another page.

I basically want to get the cell values from the table through a button

This is the table code (not in html this is done in the controller that is of the MVC structure)

function catalogo() {
//debugging para ver se foi pedido com sucesso
console.log(' pedido get  entrou success');
//criação de uma tabela para demonstração dos resultados recebidos
var txt = "";
txt += "<div class='table-responsive'>";
txt += "<table id='tblLivrosCatalogo' class='table table-sm'>";
txt += "<thead color:white '>";
txt += "<tr> <th>#ID</th> <th>Titulo</th> <th>Autor</th> <th>Género</th><th>Ano De Lançamento</th><th>Proprietário</th><th>Disponibilidade</th></tr></thead><tbody>";
//percorrer a variável data e por cada row cria a linha da tabela com os dados presentes
for (var i = 0; i < lista.length; i++) {
    if (lista[i].disp_req == "Disponivel") {
        // console.log(i)
        //aqui os id's são os do mysql
        txt += "<tr><td id ='id_tr'>" + lista[i].id_livro + "</td><td>" + lista[i].titulo +
            "</td><td>" + lista[i].autor + "</td><td>" + lista[i].genero + "</td><td>" + lista[i].ano_lanc + "</td><td>" + lista[i].user_prop + 
            "</td><td>" + lista[i].disp_req + "</td><td>" + "<button id='btnn'>Requisitar</button>" + "</tr>"
    }
    else {
        //aqui os id's são os do mysql
        txt += "<tr><td  id ='id_tr'>" + lista[i].id_livro + "</td><td>" + lista[i].titulo +
            "</td><td>" + lista[i].autor + "</td><td>" + lista[i].genero + "</td><td>" + lista[i].ano_lanc + "</td><td>" + lista[i].user_prop + "</td><td>" + lista[i].disp_req + "</td></tr>"
    }
}
txt += "</tbody></table></div>";
//envia a tabela construida para a view e mostra o resultado (txt) no object com ID result
$("#tablecatalogo").html(txt);
}

This table is generated after I make a book record that will stop the sql database and then it is read in this table

    
asked by anonymous 05.07.2018 / 23:40

1 answer

1
  

Basically I want to get the cell values from the table through a button

You can create a onclick in the button that has a JSON with the values of the line. An example would be:

+ "<button onclick='requesitar(" + JSON.stringify(lista[i]) + ")'>Requisitar</button>" +

and then use this data in the function:

function requesitar(json){
    const obj = JSON.parse(json);
    alert(obj.autor);
}

NOTE: You have too many repeating IDs, this is HTML invalid. You can only have 1 unique ID per page.

    
06.07.2018 / 03:04