Opening Modal using different links to the same table row

0

I have the following table:

<table id="tabela">
    <thead>
      <tr>
         <th>Produto</th>
         <th>Cliente</th>
      </tr>
    </thead>
    <tbody>    
        <tr>
           <td id="produto1"><a href="" data-toggle="modal" data-target="#myModal">Banana</a></td>
           <td id="cliente1"><a href="" data-toggle="modal" data-target="#myModal">José da Silva</a></td>                   
        </tr>                
        </tbody>
</table>

As you can see, in the table I have 2 links on the same line, a link must show the product data in a modal and the other shows the customer data. To show the modal of the product I used the following Jquery script:

$(document).on("click","#tabela td a",function(e){
    e.preventDefault()
    var idproduto= $(this).parent().attr("id");    
    $('.modal-body').load('dados_produto.php?id='+idproduto);            
});

    
asked by anonymous 08.02.2018 / 13:16

1 answer

0

I was able to solve it like this: In the product link, the id goes with a name that indicates the column referring to the information and concatento with a separator '-' plus the value of the id where the user clicked, thus: 'customer-1' or 'product-5'

In Jquery I give a split and separate the field that identifies the position in the table and the value of the clicked id.

$(document).on("click","#tabela td a",function(e){
    e.preventDefault()
    var linkClicadoComId = $(this).parent().attr("id").split('-');
    var tipo = linkClicadoComId[0];
    var id = linkClicadoComId[1];

    if(tipo == 'produto'){
        $('.modal-body').load('dados_produto.php?id='+id);
    }
    if(tipo == 'cliente'){
        $('.modal-body').load('dados_cliente.php?id='+id);
    }
});

He was fierce, whoever wants to go to the resolution.

    
09.02.2018 / 17:54