Filter an html table with a button

Asked

Viewed 1,282 times

1

I have an HTML table for call management

<table class="table">
    <thead>
        <tr>
            <th>Protocolo</th>
            <th>Problema</th>
            <th>Situação</th>
            <th>Usuario</th>
            <th>Atendente</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        <td>#1</td>
        <td>Internet</td>
        <td>Encerrado</td>
        <td>Alexandre</td>
        <td>Carlos</td>
        </tr>
        <tr>
        <td>#2</td>
        <td>Monitor</td>
        <td>Aberto</td>
        <td>Renato</td>
        <td>#</td>
        </tr>
        <tr>
        <td>#3</td>
        <td>Formatação</td>
        <td>Em andamento</td>
        <td>Alexandre</td>
        <td>José</td>
        </tr>
    </tbody>
</table>

Here is the example, I have 3 buttons

<div class="btn-group">
  <button type="button" id="Abertos" class="btn btn-success btn-xs">Abertos</button>
  <button type="button" id="Andamento" class="btn btn-warning btn-xs">Andamento</button>
  <button type="button" id="Encerrados" class="btn btn-danger btn-xs">Encerrados</button>
</div>

I wanted to know how to do so that when I click for example on the "Open" button it brings me in the table, only the records where the "Situation" column is filled with the word "Open", and so on, how I would do it?

1 answer

1


It would be better to give a data-estado each column. Then use the same value as the data-estado in the button id.

Example:

var tds = document.querySelectorAll('table td[data-estado]');
document.querySelector('.btn-group').addEventListener('click', function(e) {
  var estado = e.target.id;
  for (var i = 0; i < tds.length; i++) {
    var tr = tds[i].closest('tr');
    tr.style.display = estado == tds[i].dataset.estado || !estado ? '' : 'none';
  }
});
.btn-group {
padding: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
rel="stylesheet"/>

<div class="btn-group">
  <button type="button" id="aberto" class="btn btn-success btn-xs">Abertos</button>
  <button type="button" id="em_andamento" class="btn btn-warning btn-xs">Andamento</button>
  <button type="button" id="encerrado" class="btn btn-danger btn-xs">Encerrados</button>
  <button type="button" class="btn btn-xs">Todos</button>
</div>

<table class="table">
  <thead>
    <tr>
      <th>Protocolo</th>
      <th>Problema</th>
      <th>Situação</th>
      <th>Usuario</th>
      <th>Atendente</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>#1</td>
      <td>Internet</td>
      <td data-estado="encerrado">Encerrado</td>
      <td>Alexandre</td>
      <td>Carlos</td>
    </tr>
    <tr>
      <td>#2</td>
      <td>Monitor</td>
      <td data-estado="aberto">Aberto</td>
      <td>Renato</td>
      <td>#</td>
    </tr>
    <tr>
      <td>#3</td>
      <td>Formatação</td>
      <td data-estado="em_andamento"></td>
      <td>Alexandre</td>
      <td>José</td>
    </tr>
  </tbody>
</table>

  • Thanks friend, it worked but I have another doubt, after, in a real situation, these data would come from the database, by default, the date-state would come as "Open", then when the technician adopted the call, some other script could change that date-state to "In Progress" and to "Finished" when finished?

  • @Kahzinhuh that state will be changed before the page loads again? ie it will be changed while the page is open?

  • It changes while the page is open, for example when I click "Answer Called", it would automatically change to "In Progress"

  • The same when I finish the call

  • @Kahzinhuh in that case you should use ajax and change the td whole. If you don’t know how to do that, ask a new question, or take a look here: https://answall.com/q/6626/129

  • 1

    Thanks, I think with your help I can already find the way to study what I need.

Show 1 more comment

Browser other questions tagged

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