简体   繁体   English

隐藏行取决于html表单元格的值

[英]Hide row depending on a html table cell value

I have a table that displays data from database and i have a cell with a simple arithmetic function. 我有一个表,显示数据库中的数据,我有一个带有简单算术函数的单元格。 I want to hide the entire row where the result of the sum is zero(if $sold value is zero). 我想隐藏整行的总和为零(如果$ sold值为零)。

 <input type="button" id="btnHide" Value=" Hide Empty Rows " /> ... <tbody> <?php } while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $sold=$row['value1']+$row['value2']); { ?> <tr> <td><?php echo $row['contract'] ?></td> <td><?php echo (round($row['value1'], 2)) ?></td> <td><?php echo (round($row['value2'],2 )) ?></td> <td><?php echo ((round($sold, 2))+0) ?></td> </tr><?php } } ?> </tbody> 

I found some code to hide all rows where it have empty cells, but it's not what i want. 我找到了一些代码来隐藏所有具有空单元格的行,但这不是我想要的。 Thx for help. 谢谢。

 $(document).ready(function() { $("#gdRows td").each(function() { var cellText = $(this).text(); if ($.trim(cellText) == '') { $(this).css('background-color', 'cyan'); } }); $('#btnHide').click(function() { $("#gdRows tr td").each(function() { var cell = $.trim($(this).text()); if (cell.length == 0) { $(this).parent().hide(); } }); }); $('#btnReset').click(function() { $("#gdRows tr").each(function() { $(this).show(); }); }); }); 

Add a class to those cells for simplification 为这些单元格添加一个类以进行简化

<td class="sold"><?php echo ((round($sold, 2))+0) ?></td>

Then use filter() 然后使用filter()

$("td.sold").filter(function() {         
     return +$(this).text().trim() === 0;         
}).parent().hide();

You could also do the same thing in php by adding a hidden class to the row if $sold is zero and add a css rule for hidden class 如果$sold为零,您还可以在php中通过向行添加一个hidden类来做同样的事情,并为该hidden类添加一个css规则

PHP 的PHP

<tr class="<?= $sold == 0 ? 'hidden' :'';?>"> 

The following function will loop through all <tr> in a table and find the 4th cell within the row. 以下函数将循环遍历表中的所有<tr>并在行中找到第4个单元格。 If that cell contains a value that evaluates to zero, then the row becomes hidden. 如果该单元格包含一个求值为零的值,则该行将隐藏。

 $("table tr").each(function() { var sold = $(this).find(":nth-child(4)"); if (parseFloat(sold.text()) === 0) $(this).hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table> <tr> <td>Contract</td> <td>123</td> <td>456</td> <td>789</td> </tr> <tr> <td>Contract</td> <td>123</td> <td>456</td> <td>0</td> </tr> <tr> <td>Contract</td> <td>0.123</td> <td>0.456</td> <td>0.0</td> </tr> <tr> <td>Contract</td> <td>0.123</td> <td>0.456</td> <td>0.789</td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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