0

I know there a lot of postings about hiding columns but I would add another question. Below is a snippet from php-generated 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>
...

This doesn't work at all. Is there an effective way to hide a column via html/css, without using a myriad of td's? w3.org implies that there is, but I have tried visibility, hidden, collapse table cells, and so on - with no result.

I don't want to set a class for each in huge table, so jquery is out of the question.

3
  • Show us your CSS and what you have tried Commented Jul 22, 2013 at 16:07
  • 1
    Please see w3schools.com/TAGS/tag_col.asp display is not a supported attribute. Commented Jul 22, 2013 at 16:13
  • @DevZer0 - uh-huh. w3fools mention that the <col> tag supports align, char, charoff, span, valign and width attributes. However it has nothing to say regarding which style attributes it will respond to. Perhaps you misread <col style="display as <col display? Commented Jul 22, 2013 at 16:43

2 Answers 2

4

Try something like this in CSS:

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

In this case, 2 is the index of the column you want to hide.

I got this from the second answer of this question: How to hide columns in HTML table?

Sign up to request clarification or add additional context in comments.

3 Comments

This is no solution. A table may have unknoun number of fields and furthermore unknown fields to hide. I just want to hide fields with some non-editable attribute like auto_increment one. Since I spent a lot of time looking for method to hide whole col and got no result so I simple put disable prop to corresponding input tag.
The index, 2, refers to the column number to hide. So if you have 5 columns, the above CSS rule would hide the entire 3rd column (0, 1, 2).
Thanks. But what could i do with 20 columns and 'hidden' numbers 0,4,5,18? Next table opens and there 5 cols and col #0 #2 wants to be hidden. So I ended up with canonical solution: check field flags and set class hide{display:none} for each corresponding th-td in while loop. Works great. Here I just want to hear something like this: hide whole col now impossible at all.
0

I would do it this way. This will hide the "Second Title" column, or middle column. You could also replace style="display:none" with a variable like this style="<?php echo $nodisplay;?>" Then you could turn columns off or on depending on your data. For instance $nodisplay could be equal to display:none; depending on whether you wish to show the column or not.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.