【问题标题】:Check cells emptiness of a certain column, if all of them are, hide whole column检查某一列的单元格是否为空,如果都是,则隐藏整列
【发布时间】:2018-02-28 16:52:13
【问题描述】:

我正在尝试检查具有“checkthisone”类的<td>s 是否为空,以及它们是否隐藏了整列。我正在尝试通过 jquery 来做到这一点。该表是从 wordpress 页面循环创建的,所以我找不到使用 php 的方法。

表格HTML是这样的:

     <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>

显示表格的 PHP 代码在这里:https://paste.ee/p/yrQpg

我想在此代码中检查具有“temizleme”类的单元格。相关部分是:

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>

【问题讨论】:

  • 这在 PHP 中应该是完全可以实现的,并且比在客户端添加功能更可取。如果你在 PHP 中包含创建表的代码,我们可以看看。
  • @FluffyKitten 我分享了一个链接,谢谢

标签: php jquery wordpress html-table


【解决方案1】:

在您的 PHP 中排除 &lt;tr&gt;

排除整行:

  1. 在 while 循环开始时获取 post_meta 值
  2. 检查是否为空
  3. 如果不为空,则显示该行。

例如,检查 post_meta 中temizleme 中的值:

<?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; ?> 

检查多个值

您链接到的代码与您在问题中包含的代码不同,因此我不确定您需要检查的确切内容。

因此,如果您想检查多个值,只需在 while 循环开始时获取所有值并检查它们是否有值。

如果它们都必须具有值,则在您的 if 语句中使用 &amp;&amp;,如果任何可以具有值,则使用 ||。例如

<?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;   ?>   


在 PHP 中隐藏列

要检查列的值,您需要:

  1. 遍历帖子,将值保存到数组中
  2. 检查数组中“temizleme”的所有值,如果其中任何一个有值,则设置显示表格时使用的标志(例如,我们在下面的示例中将$bTemizlemeColumnIsEmpty设置为false)
  3. 循环遍历数据数组以显示表格中的信息。如果我们的标志表明该列是空的(即$bTemizlemeColumnIsEmpty 为真),则不要为“temizleme”显示&lt;td&gt;

例如:

<?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 */  ?>  

【讨论】:

    【解决方案2】:

    试试这个方法:

    $(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>

    【讨论】:

      【解决方案3】:

      最简单的解决方案是使用:empty选择器

      $( "#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>

      【讨论】:

      • 我认为代码 "$( "#checking-tds-header" ).hide()" 直接隐藏了标题。我想检查该列中的所有单元格是否都是空的。检查后我需要用单元格隐藏标题
      猜你喜欢
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多