2017-09-19 1 views
0

J'essaie de vérifier si <td> s avec la classe 'checkthisone' sont vides, et si elles sont masquer toute la colonne. J'essaye de faire ceci par l'intermédiaire de jquery. La table est créée en boucle à partir d'une page de wordpress donc je ne pouvais pas trouver un moyen de le faire avec php.Vérifiez le vide des cellules d'une certaine colonne, si elles sont toutes, cachez toute la colonne

Le tableau HTML est comme ceci:

 <table> 
      <tr> 
      <th>header</th> 
      <th id="checking-tds-header">header</th> 
      <th>header</th> 
      </tr> 
      <tr> 
      <td>info</td> 
      <td class="checkthisone"></td> 
      <td>info</td> 
      </tr> 
      <tr> 
      <td>info</td> 
      <td class="checkthisone"></td> 
      <td>info</td> 
      </tr> 
    </table> 

Le code PHP qui affiche la table est ici: https://paste.ee/p/yrQpg

Je veux vérifier les cellules avec la classe 'temizleme' dans ce code. La section pertinente est:

while ($query->have_posts()) : $query->the_post(); 
    ?>  
    <tr> 
     <td> 
      <a class="mangaep-episode" href="<?php the_permalink(); ?>"><?php echo get_post_meta(get_the_ID(), 'kacinci_bolum', true); ?> 
      <?php $bolum_aciklamasi = get_post_meta(get_the_ID(), 'bolum_aciklamasi', true); 
       if (! empty ($bolum_aciklamasi)) { ?>   
       <span><?php echo $bolum_aciklamasi; ?></span> 
       <?php } ?> 
      </a>  
     </td> 
     <td><?php if(function_exists('the_views')) { the_views(); } ?></td> 
     <td class="down"><?php echo get_post_meta(get_the_ID(), 'cevirmen', true); ?></td> 
     <td class="down"><?php echo get_post_meta(get_the_ID(), 'editor', true); ?></td> 
     <td class="down temizleme"><?php echo get_post_meta(get_the_ID(), 'temizleme', true); ?></td> 
     <td class="down"> 
       <?php $indirme_linki = get_post_meta(get_the_ID(), 'indirme_linki', true); 
       if (! empty ($indirme_linki)) { ?>   
       <a target="_blank" href="<?php echo $indirme_linki; ?>" class="mangaep-down">İNDİR</a> 
       <?php } ?>     
     </td> 
    </tr> 
+0

Cela devrait être parfaitement réalisable en PHP, et serait préférable d'ajouter la fonctionnalité côté client. Si vous incluez le code qui crée la table en PHP, nous pouvons jeter un oeil. – FluffyKitten

+0

@FluffyKitten j'ai partagé un lien pour cela, merci –

Répondre

2

Exclure la <tr> dans votre PHP

Pour exclure toute une ligne:

  1. obtenir la valeur post_meta (s) au début de la boucle while
  2. Vérifiez s'il est vide ou non
  3. Si ce n'est pas vide y, affiche la ligne.

Par exemple, pour vérifier la valeur du temizleme dans le post_meta:

<?php 
while ($query->have_posts()) : $query->the_post(); 

    /* 1. Get the post_meta value of temizleme */ 
    $temizleme = get_post_meta(get_the_ID(), 'temizleme', true); 

    /* 2. check if temizleme has a value */ 
    if (!empty($temizleme)) { 

     /* 3. if temizleme isn't empty, display the row */ 
     ?>  
     <tr> 
      <td> 
       <a class="mangaep-episode" href="<?php the_permalink(); ?>"><?php echo get_post_meta(get_the_ID(), 'kacinci_bolum', true); ?> 
       <?php $bolum_aciklamasi = get_post_meta(get_the_ID(), 'bolum_aciklamasi', true); 
        if (! empty ($bolum_aciklamasi)) { ?>   
        <span><?php echo $bolum_aciklamasi; ?></span> 
        <?php } ?> 
       </a>  
      </td> 
      <td><?php if(function_exists('the_views')) { the_views(); } ?></td> 
      <td class="down"><?php echo get_post_meta(get_the_ID(), 'cevirmen', true); ?></td> 
      <td class="down"><?php echo get_post_meta(get_the_ID(), 'editor', true); ?></td> 
      <td class="down temizleme"><?php echo get_post_meta(get_the_ID(), 'temizleme', true); ?></td> 
      <td class="down"> 
       <?php $indirme_linki = get_post_meta(get_the_ID(), 'indirme_linki', true); 
       if (! empty ($indirme_linki)) { ?>   
        <a target="_blank" href="<?php echo $indirme_linki; ?>" class="mangaep-down">İNDİR</a> 
       <?php } ?>     
      </td> 
     </tr> 

    <?php } /* 3. end if (!empty(temizleme)) */ ?>  

<?php endwhile; ?> 

Vérifiez plus d'une valeur

Le code lié à est différent du code que vous avez inclus dans votre question, je ne sais pas exactement ce que vous devez vérifier. Donc, si vous voulez vérifier plus d'une valeur, il suffit d'obtenir toutes les valeurs au début de la boucle while et de vérifier si elles ont des valeurs.

Si TOUS doivent avoir une valeur, utilisez && dans votre instruction if, si TOUT peut avoir une valeur, utilisez ||. par exemple.

<?php 
while ($query->have_posts()) : $query->the_post(); 

    /* 1. Get the post_meta values to check */ 
    $temizleme = get_post_meta(get_the_ID(), 'temizleme', true); 
    $kacinci_bolum= get_post_meta(get_the_ID(), 'kacinci_bolum', true); 

    /* 2. check that BOTH have a value */ 
    if (!empty($temizleme) && !empty($kacinci_bolum)) { 
    ?>  
     <tr> 
      [... display your tds the same way as above...] 
     </tr> 

    <?php } /* end if (!empty...) */ ?> 
<?php endwhile; ?> 


Pour masquer une colonne dans votre PHP

Pour vérifier les valeurs d'une colonne, vous devez:

  1. boucle à travers les messages, sauvegarder les valeurs dans un
  2. Vérifiez toutes les valeurs de "temizleme" dans le tableau, et si l'une d'entre elles a une valeur, définissez un indicateur à utiliser lors de l'affichage de la table (par exemple, $bTemizlemeColumnIsEmpty à false dans l'exemple ci-dessous)
  3. Parcourez la matrice de données pour afficher les informations dans la table. Si notre drapeau indique la colonne est vide (c.-à-$bTemizlemeColumnIsEmpty est vrai), alors ne pas afficher la <td> pour « temizleme »

Par exemple:

<?php 

$postdata = array(); /* array to save data for ALL posts */ 

while ($query->have_posts()) : $query->the_post(); 

    /* 1. Save all the post data in an array */ 
    $row = array(); /* array to save data for THIS post */ 

    $row["temizleme"] = get_post_meta(get_the_ID(), 'temizleme', true); 
    $row["kacinci_bolum"] = get_post_meta(get_the_ID(), 'kacinci_bolum', true); 
    $row["bolum_aciklamasi"] = get_post_meta(get_the_ID(), 'bolum_aciklamasi', true); 

    [etc...] 

    $postdata[] = $row; /* add this data to the $postdata array */ 
<?php endwhile; ?> 


/* 2. check the values of temizleme */ 

$bTemizlemeColumnIsEmpty = true; 
foreach ($postdata as $row) 
    if (!empty($row["temizleme"])) $bTemizlemeColumnIsEmpty = false; 

/* if any of the cells have a value, $bTemizlemeColumnIsEmpty will be false after we finish this loop */ 


/* 3. now loop through all the row to display the table */ 
foreach ($postdata as $row){ 
?> 
    <tr> 
     <td> 
       <a class="mangaep-episode" href="<?php the_permalink(); ?>"><?php echo $row["kacinci_bolum"]; ?> 
       <?php if (! empty ($row["bolum_aciklamasi"])) { ?>   
        <span><?php echo $row["bolum_aciklamasi"]; ?></span> 
        <?php } ?> 
       </a>  
      </td> 
      <td><?php if(function_exists('the_views')) { the_views(); } ?></td> 
      <td class="down"><?php echo $row["cevirmen"]; ?></td> 
      <td class="down"><?php echo $row["editor"]; ?></td> 

      <?php 
      /* 4. only display this td if $bTemizlemeColumnIsEmpty is false */ 
      if (!$bTemizlemeColumnIsEmpty){ 
      ?> 
       <td class="down temizleme"><?php echo $row["temizleme"]; ?></td> 
      <?php } ?> 


      <td class="down"> 
       <?php if (! empty ($row["indirme_linki"])) { ?>   
        <a target="_blank" href="<?php echo $row["indirme_linki"]; ?>" class="mangaep-down">İNDİR</a> 
       <?php } ?>     
      </td> 
    </tr> 

<?php } /* end foreach */ ?> 
0

Essayez cette approche:

$(document).ready(function(){ 

    $(".checkthisone").each(function(){ // check each class 
     if($(this).text() == ""){   // if the <td> has nothing in it, enter this condition 
     $(this).parent("tr").hide(); // hide the entire <tr> 
     } 
    }); 
}); 

$(document).ready(function(){ 
 

 
    $(".checkthisone").each(function(){ 
 
     if($(this).text() == ""){ 
 
     $(this).parent("tr").hide(); 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
      <tr> 
 
      <th>header</th> 
 
      <th id="checking-tds-header">header</th> 
 
      <th>header</th> 
 
      </tr> 
 
      <tr> 
 
      <td>info</td> 
 
      <td class="checkthisone"></td> 
 
      <td>info</td> 
 
      </tr> 
 
      <tr> 
 
      <td>info</td> 
 
      <td class="checkthisone">stuff</td> 
 
      <td>info</td> 
 
      </tr> 
 
    </table>

+0

@ clearshot66 "cacher toute la colonne" pas "cacher la ligne" –

0

La solution la plus simple est d'utiliser :empty sélecteur

$("#checking-tds-header").hide(); 
 
$("td.checkthisone:empty").hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
      <tr> 
 
      <th>header</th> 
 
      <th id="checking-tds-header">header</th> 
 
      <th>header</th> 
 
      </tr> 
 
      <tr> 
 
      <td>info</td> 
 
      <td class="checkthisone"></td> 
 
      <td>info</td> 
 
      </tr> 
 
      <tr> 
 
      <td>info</td> 
 
      <td class="checkthisone"></td> 
 
      <td>info</td> 
 
      </tr> 
 
    </table>

+0

Je pense que le code "$ (" # checking-tds-header ") .hide()" masque directement l'en-tête. Je veux vérifier si toutes les cellules de cette colonne sont vides. Je dois cacher l'en-tête avec les cellules après avoir vérifié –