通过代码隐藏表列


Hide table column via code

我知道有很多关于隐藏列的帖子,但我想补充一个问题。以下是php生成的html:的一个片段

<table id="dataGrid">
<col style="display:none">
<col style="display:table-column">
<col style="display:table-column">
<col style="display:table-column">
<col style="display:table-column">
<col style="display:table-column">
<col style="display:table-column">
<col style="display:table-column">
<thead><tr>
...

这根本不起作用。有没有一种有效的方法可以通过html/css隐藏列,而不需要使用无数的td?w3.org暗示有,但我尝试过可见性、隐藏、折叠表单元格等等,但没有结果。

我不想在巨大的表中为每个表设置一个类,所以jquery是不可能的。

在CSS:中尝试这样的东西

#myTable tr *:nth-child(2), {
    display: none;
}

在这种情况下,2是要隐藏的列的索引。

我从这个问题的第二个答案中得到了这个答案:如何在HTML表中隐藏列?

我会这样做。这将隐藏"第二个标题"列或中间列。您也可以用类似style="<?php echo $nodisplay;?>"的变量替换style="display:none"。然后,您可以根据数据关闭或打开列。例如$nodisplay可以等于display:none;这取决于您是否希望显示该列。

<table id="dataGrid">
<thead>
    <tr>
        <th>First Title</th>
        <th style="display:none">Second Title</th>
        <th>Third Title</th>
    </tr>
</thead>
<tbody>
    <tr>
        <th>First Body</th>
        <th style="display:none">Second Body</th>
        <th>Third Body</th>
    <tr>
</tbody>
<table>