Filter table data if row table consists of zero

I was wondering how can I delete a line if that line contains 0?

This is what my table looks like without filtering method: enter image description here

I want to achieve this:

enter image description here

Is this possible somehow with JQuery? I am just using a simple HTML table: JSFIDDLE

<table>
     <tbody>
         <tr>
             <th class="resultModule">
                 Module
             </th>
             <th class="resultCredits">
                 Credits
             </th>
         </tr>        
         <tr>
             <td>
                 Test A
             </td>
             <td>
                 0
             </td>

         </tr>                    
         <tr>
             <td>
                 Test B
             </td>
             <td>
                 8
             </td>
         </tr>
         <tr>
             <td>
                 Test C
             </td>
             <td>
                 0
             </td>
         </tr>
         <tr>
             <td>
                 Test D
             </td>
             <td>
                 6.5
             </td>
         </tr>         
    </tbody>
</table>

      

PS I dont want to use database related answers.

+3


source to share


2 answers


You can use filter()

to search for strings where the second element td

contains 0

and then remove()

those. Try the following:

$('table tr').filter(function () {
    return $(this).find('td:eq(1)').text() == '0';
}).remove();

      



Sample script

+3


source


You can use : has () and : contains () like

$('tr:has(td:contains(0))').remove();

      

$('tr:has(td:contains(^0$'))').remove();
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
     <tbody>
         <tr>
             <th class="resultModule">
                 Module
             </th>
             <th class="resultCredits">
                 Credits
             </th>
         </tr>        
         <tr>
             <td>
                 Test A
             </td>
             <td>
                 80
             </td>

         </tr>                    
         <tr>
             <td>
                 Test B
             </td>
             <td>
                 8
             </td>
         </tr>
         <tr>
             <td>
                 Test C
             </td>
             <td>
                 0
             </td>
         </tr>
         <tr>
             <td>
                 Test D
             </td>
             <td>
                 6.5
             </td>
         </tr>         
    </tbody>
</table>
      

Run codeHide result




Update

Note. the above will be broken if the text contains 0, like 80. In this case, you can do

$('tr:has(td:contains(0))').filter(function(){
    return $(this).find('td:contains(0)').text() == '0';
}).remove();

      

+1


source







All Articles