Manipulating td’s of tables

Asked

Viewed 225 times

0

I have a code that breaks down the information coming from the database into three tables. But when I filter these results by name and code (fields available in the DOM to filter the tables) the rows () of the table get messy because what I do is hide the lines that do not meet the filter. Is there a way to reinsert into the table the lines I want and delete the ones that existed before? If it helps, they’re an example:

<td nome=x codigo=y>Dado</td>

The search fields are an example:

<input type='text' id='pesquisa_nome'>
<input type='number' id='pesquisa_codigo'>
  • You can’t use a table plugin to do this ? since you’re using jQuery

  • I don’t know, you know some I can research on?

  • I’ll make an answer that works well.

1 answer

2


As Voce is using jQuery, I believe that the simplest way to have this result would be using a plugin in table manipulation, in the example below, I am using the Datatables:

$(document).ready(function() {
    $('#example').DataTable();
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr><tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2012/03/29</td>
                <td>$433,060</td>
            </tr>
            <tr>
                <td>Airi Satou</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>33</td>
                <td>2008/11/28</td>
                <td>$162,700</td>
            </tr>
            <tr>
                <td>Brielle Williamson</td>
                <td>Integration Specialist</td>
                <td>New York</td>
                <td>61</td>
                <td>2012/12/02</td>
                <td>$372,000</td>
            </tr>
            <tr>
                <td>Herrod Chandler</td>
                <td>Sales Assistant</td>
                <td>San Francisco</td>
                <td>59</td>
                <td>2012/08/06</td>
                <td>$137,500</td>
            </tr>
            <tr>
                <td>Rhona Davidson</td>
                <td>Integration Specialist</td>
                <td>Tokyo</td>
                <td>55</td>
                <td>2010/10/14</td>
                <td>$327,900</td>
            </tr>
            <tr>
                <td>Colleen Hurst</td>
                <td>Javascript Developer</td>
                <td>San Francisco</td>
                <td>39</td>
                <td>2009/09/15</td>
                <td>$205,500</td>
            </tr>
            <tr>
                <td>Sonya Frost</td>
                <td>Software Engineer</td>
                <td>Edinburgh</td>
                <td>23</td>
                <td>2008/12/13</td>
                <td>$103,600</td>
            </tr>
            <tr>
                <td>Jena Gaines</td>
                <td>Office Manager</td>
                <td>London</td>
                <td>30</td>
                <td>2008/12/19</td>
                <td>$90,560</td>
            </tr>
            <tr>
                <td>Quinn Flynn</td>
                <td>Support Lead</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2013/03/03</td>
                <td>$342,000</td>
            </tr>
            <tr>
                <td>Charde Marshall</td>
                <td>Regional Director</td>
                <td>San Francisco</td>
                <td>36</td>
                <td>2008/10/16</td>
                <td>$470,600</td>
            </tr>
            <tr>
                <td>Haley Kennedy</td>
                <td>Senior Marketing Designer</td>
                <td>London</td>
                <td>43</td>
                <td>2012/12/18</td>
                <td>$313,500</td>
            </tr>
         </tbody>
</table>

How it works:

Just create the table normally, but it is important to have the <thead> and <tbody> marked, and instantiate the element (#example) table the call of the plugin:

$(document).ready(function() {
        $('#example').DataTable();
} );

Reference of the example: Zero Configuration

  • Dude, it’s actually simple what I want to do, I just keep the TR elements of the table in an array and show them by dividing them into three tables. if I just give. Hide() in which I do not want to show the table gets crooked, so I have to remove all, filter the ones I want to add and then insert this array of tds that I want to show

  • The array I’m able to do, now I just need to take all the tr elements from the table and insert the ones that passed the filter

  • @Mateusalberghini puts an example even if without working on the question, I think I misunderstood his doubt

  • I ended up getting it, man, but thanks for trying to help ;)

Browser other questions tagged

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