Selecting row information in an HTML table

Asked

Viewed 1,377 times

1

I have the following code:

var clicado = null;

$('.clicado').click(function(){
  clicado = $(this).val();
  $('#mostrarId').html(clicado);  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<table class="table table-hover table-striped">
  <tr>
    <td>1</td>
    <td>Primeiro</td>
    <td>
      <button type="button" value="1" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
  <tr>
    <td>2</td>
    <td>Segundo</td>
    <td>
      <button type="button" value="2" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
  <tr>
    <td>3</td>
    <td>Terceiro</td>
    <td>
      <button type="button" value="3" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
</table>
<div class="modal fade" id="mostrar">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Mostrar</h4>
      </div>
      <div class="modal-body">
        <p>Mostrar esse registro?</p>
        <table>
          <tr>
            <td id="mostrarId"></td>
          </tr>
        </table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="confirmar">Confirmar</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
      </div>
    </div>
  </div>
</div>

When I click on the button show opens the modal and shows the index number that was clicked, how do I show the name, IE, when I click on the Index Show button 1 I would like to show only the line 1 in the modal with the index and the name that would be "First"?

1 answer

2


In a simple way you can look for the <td> with Indice 1 (eq(1)) within the line to which the button that was clicked belongs.

var clicado = null, nome = null;
$('.clicado').click(function(){
  nome = $(this).parents('tr').find('td').eq(1).text();
  clicado = $(this).val();
  $('#mostrarId').html(clicado+ ', ' +nome);  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<table class="table table-hover table-striped">
  <tr>
    <td>1</td>
    <td>Primeiro</td>
    <td>
      <button type="button" value="1" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
  <tr>
    <td>2</td>
    <td>Segundo</td>
    <td>
      <button type="button" value="2" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
  <tr>
    <td>3</td>
    <td>Terceiro</td>
    <td>
      <button type="button" value="3" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
</table>
<div class="modal fade" id="mostrar">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Mostrar</h4>
      </div>
      <div class="modal-body">
        <p>Mostrar esse registro?</p>
        <table>
          <tr>
            <td id="mostrarId"></td>
          </tr>
        </table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="confirmar">Confirmar</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
      </div>
    </div>
  </div>
</div>

  • How do I get another column td on the same line tr?

  • 1

    @Laérciolopes goes by the Dice da <td> within the function eq(), nome = $(this).parents('tr').find('td').eq(INDICE AQUI).text();, Remember that these start at 0

  • 1

    I get it, thank you.

Browser other questions tagged

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