简体   繁体   中英

Cannot hide <tr> if <td> are empty

I want to hide <tr> only if no data is available for <td> . I tried with many scripts as well as if condition. But no luck. Please help.

<table class="table table-striped why-choose-tbl" id="insurance">
   <thead>
      <tr>
         <th scope="col"></th>
         <th scope="col">Basic</th>
         <th scope="col">Comprehensive</th>
      </tr>
   </thead>
   <tbody>
      <tr>
        <td>Medical</td>
        <td><span><?php the_field('basic_medical') ?></span><i class="fas fa-check"></i></td>
        <td><span><?php the_field('comp_medical') ?></span><i class="fas fa-check"></i></td>
     </tr>
     <tr>
        <td>Cancellation</td>
        <td><span><?php the_field('basic_cancellation') ?></span><i class="fas fa-check"></i></td>
        <td><span><?php the_field('comp_cancellation') ?></span><i class="fas fa-check"></i></td>
     </tr>
   </tbody>
</table>

<script>
$('#insurance tr').filter(function() {
    return $.trim($(this).text()) === '';
}).hide(); 

// $('#insurance > tbody  > tr').has('td:empty').hide()

// $('#insurance > tbody  > tr').each(function () {
//    if ($(this).find('td').is(':empty')) {
//        $(this).hide();
//    }
// });
</script>

I tried as follows too. but neither of them worked.

<?php 
     $basic = the_field('basic_medical');
     $comp =the_field('comp_medical');

   if (empty($basic) && empty($comp))
        {
            echo "<td style=/"display:none;/"><i class="fas fa-check"></i><span><?php the_field('basic_medical') ?> </span></td>";
            echo "<td style=/"display:none;/"><i class="fas fa-check"></i><span><?php the_field('comp_medical') ?></span></td>";
        }
?>

May I know where I have gone wrong? Thank you so much.

You don't need to print the entire <tr> element if the fields are empty. For example,

<?php

$basic_medical = the_field('basic_medical');
$comp_medical = the_field('comp_medical');

?>

<tbody>
    <?php if(!empty($basic_medical) && !empty($comp_medical)): ?>
       <tr>
           <td>Medical</td>
           <td><span><?php the_field('basic_medical')?></span><i class="fas fa-check"></i></td>
           <td><span><?php the_field('comp_medical')?></span><i class="fas fa-check"></i></td>
       </tr>
    <?php endif; ?>

   ...
   ...

    <tr>
       <td>Cancellation</td>
       <td><span><?php the_field('basic_cancellation')?></span><i class="fas fa-check"></i></td>
       <td><span><?php the_field('comp_cancellation')?></span><i class="fas fa-check"></i></td>
    </tr>
</tbody>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM