Forum Moderators: open

Message Too Old, No Replies

Hiding Columns of a Table

hiding table columns / cells

         

posaune02

4:01 pm on May 9, 2004 (gmt 0)

10+ Year Member



How exactly would I go about hiding specific cells (columns) in a table? I'm using php, and the script will only be called if a user is not authorized to view certain columns. Thus I just need the script to loop through all the cells in a page, and reset the style of the cell to "hide" if the cells class is specified as such.

Bernard Marx

4:44 pm on May 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOOPING METHOD:


window.onload = function()
{
var hideClasses = {class1:1,class2:1}

var allCells = document.getElementById("tableID").getElementsByTagName("td")
for(var k=0;k<allCells.length;k++)
{
var cell = allCells[k]
if(hideClasses[cell.className])
cell.style.display = "none"
}
}

STYLE METHOD (better)

Script block in the BODY:

<script>
var hideClasses = ["class1","class2"]
if(notAuthorisedCondition)
{
document.write("<style>")
for(var k=0;k<hideClasses.length;k++)
document.write("."+hideClasses[k]+"{display:none;}")
document.write("<\/style>")
}
</script>

..that's if you're doing this on the client side.
If you're doing it on the server, why not just not serve them the columns in the first place? Whether displayed or not, the cells will still be visible in the page code if served.

Have I missed something?

Rambo Tribble

4:50 pm on May 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It might be easier to give the cells of each column a unique class, then have the script change either the display or visibility attribute values for a column's class, thereby affecting the whole column in one sweep.

Bernard Marx

5:06 pm on May 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rambo, how well are columns (and CSS effects on) supported in browsers?
[That just a straight question]

posaune02

5:06 pm on May 9, 2004 (gmt 0)

10+ Year Member



Thanks for the replies. Bernard Marx, I technically could do it all server side, and you're right, that would keep the cell content *completely* invisible (even in the source) from unauthorized users. However, I have most of the content in the cells, including some of the actual table markup, stored in php variables, and to interleave that with if conditions would be tedious. I think I will end up doing it server side though. The hassle may be worth it, but if it becomes too much of a problem, I will refer to your scripts, of which I am greatly appreciative. Thank you.

Rambo Tribble

8:40 pm on May 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The display:none; seems to be compatible through NS 4, though other style attributes are hit and miss (borders don't work; backgrounds do, etc.). To clarify, I'm not talking about using column groups, just applying a class consistently to each <td> that is part of a column on the page.

Perhaps a bigger issue is accommodating archaic DOM's if one chooses to use DHTML to change CSS attributes.

As for straight questions, remember the wisdom of Snidely Wiplash, "It's hard to think straight with a crooked mind."