javascript - 如何根据 php 条件隐藏和显示 html 表中的列

标签 javascript php html css

如何根据 php 条件隐藏和显示 html 表中的列。

$gid = $_SESSION['gid'];

if(gid == NO) 
{
      //display whole table 

}
else{
        // Dont dispaly the certain columns like gst,sgst.
}

表格看起来像

    <table class="table">
    <thead>
    <tr>
    <th class="border-0 text-uppercase small font-weight-bold">Sl no</th>
    <th class="border-0 text-uppercase small font-weight-bold">Spares Particulars</th>
    <th class="border-0 text-uppercase small font-weight-bold">Amount</th>
    <th class="border-0 text-uppercase small font-weight-bold">Quantity</th>
    <th class="border-0 text-uppercase small font-weight-bold">GST %</th>
    <th class="border-0 text-uppercase small font-weight-bold">GST AMT</th>
    <th class="border-0 text-uppercase small font-weight-bold">CGST %</th>
    <th class="border-0 text-uppercase small font-weight-bold">CGST AMT</th>
    <th class="border-0 text-uppercase small font-weight-bold">SGST %</th>
    <th class="border-0 text-uppercase small font-weight-bold">SGST AMT</th>
    <th class="border-0 text-uppercase small font-weight-bold">Total AMT</th>
     </tr>
        </thead>
        <tbody>
    <tr>
<td><?php echo $i ?></td>
<td><?php echo $sparen ?></td>
<td><?php echo $row9['amt'] ?></td>
<td><?php echo $row9['quant'] ?></td>
<td><?php echo $row9['gstp'] ?></td>
<td><?php echo round($row9['amt']*($row9['gstp']/100)) ?></td>
<td><?php echo round($row9['gstp']/2) ?></td>
<td><?php echo round(($row9['amt']*($row9['gstp']/100))/2) ?></td>
<td><?php echo round($row9['gstp']/2) ?></td>
<td><?php echo round(($row9['amt']*($row9['gstp']/100))/2) ?></td>
<td><?php echo round((($row9['amt']*($row9['gstp']/100))+$row9['amt'])*$row9['quant']) ?></td>

</tr>                            
</tbody>
</table>

如果 gid=YES,我不想显示像 gst,gstamt,cgst,cgstamt,sgst,sgstamt 这样的列。问题是我尝试了 js 但它不起作用。谁能帮我解决一个更简单的问题。

最佳答案

您可以使用 PHP 三元运算符和 css 显示属性来完成该任务

$ShowHide = ($gid == 'NO') ? 'block' : 'none';
<td style="display:<?php echo $ShowHide; ?>;"><?php echo round($row9['gstp']/2) ?></td>

我认为这是完成任务的一种简单方法。 $ShowHide 会根据$gid 的值设置显示属性为td

关于javascript - 如何根据 php 条件隐藏和显示 html 表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53700511/

相关文章:

php - 在 htaccess 中重定向

php - 通知: Use of undefined constant CURL_IPRESOLVE - assumed 'CURL_IPRESOLVE'

html - 无法读取未定义的 angularjs 的属性 'toLowerCase'

javascript - TinyMCE 隐藏 Div 的占位符

javascript - 尝试让 div 在将鼠标悬停在图像上时出现......无法让它工作

javascript - graphqljs - 如何搜索枚举类型

javascript - JS文件中的System.register是什么意思?

javascript - 将 ArrayList 传递给 JQuery Flot

javascript - 使用 PHP 和 Javascript 创建翻转

javascript - 页面加载期间脚本出现卡顿