hide column in table

T

Tito

I have a html table, is possible hide and unhide some column with javascript
?

How can I do ?
 
S

s0lnic

Tito said:
I have a html table, is possible hide and unhide some column with
javascript ?

How can I do ?

The are some options, if you're not using any JS library, and have a table
like this one:

<table id="some_table">
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
</table>

you can use the following JS code to accomplish your task:

var trs = document.getElementById('some_table').getElementsByTagName('tr');
for(var i = 0; i < trs.length; i++) {
var cell = trs.getElementsByTagName('td')[0];
if(cell.style.display == 'none'){
cell.style.display = 'block';
} else {
cell.style.display = 'none';
}
}

If you are using Prototype library, then you can just write:

$$('td.first').invoke('toggle');

Which hide/show the first column items.
 
T

Thomas 'PointedEars' Lahn

s0lnic said:
The are some options, if you're not using any JS library, and have a table
like this one:

<table id="some_table">
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
</table>

you can use the following JS code to accomplish your task:

var trs = document.getElementById('some_table').getElementsByTagName('tr');

var trs = document.getElementById('some_table').rows;

More run-time feature-testing is clearly indicated before property access.
for(var i = 0; i < trs.length; i++) {

for (var i = trs && trs.length; i--;) {

is sufficient here and more efficient.
var cell = trs.getElementsByTagName('td')[0];


var cell = trs.cells[0];

Then it will be more efficient and it will also work with `th' elements.
With a table, it is reasonable to assume that the table header should
disappear along with the corresponding cells. And any accessible properly
used table should have `th' elements.
if(cell.style.display == 'none'){
cell.style.display = 'block';
} else {
cell.style.display = 'none';
}

According to CSS2, `table-cell' and `table-header' are the initial values of
the `display' property of `td' and `th' elements, respectively. It is only
that IE ignores that. However fortunately, current DOM implementations
allow the property value to be set to the empty string to restore that
value. Therefore:

if (cell.style.display == 'none')
{
cell.style.display = '';
}
else
{
cell.style.display = 'none';
}
}

If you are using Prototype library

-- which you should not because of its author(s) not knowing what they are
doing --
, then you can just write:

$$('td.first').invoke('toggle');

Which hide/show the first column items.


Ceterum censeo Prototype.js esse deletam.

PointedEars
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 09 nov 2007 in comp.lang.javascript:
Ceterum censeo Prototype.js esse deletam.

So you think it is already done?

Next time use the gerundivium of deleo:

delendum.
[(proto)typus is masculine]

or delendam
[(java)scriptio is feminine]
 
S

s0lnic

Thomas said:
According to CSS2, `table-cell' and `table-header' are the initial values
of the `display' property of `td' and `th' elements, respectively.  It is
only that IE ignores that.  However fortunately, current DOM
implementations allow the property value to be set to the empty string to
restore that value.  Therefore:

if (cell.style.display == 'none')
{
cell.style.display = '';
}
else
{
cell.style.display = 'none';
}

Right, we're talking about cells, not divs. Good point.
-- which you should not because of its author(s) not knowing what they are
doing --

That's interesting, I'm not a JS specialist and I've found this library
very, very useful. Please point out some details, what's so wrong about
Prototype?
 
S

s0lnic

s0lnic said:
That's interesting, I'm not a JS specialist and I've found this library
very, very useful. Please point out some details, what's so wrong about
Prototype?

ok, don't answer, I'll search the archives...
 
R

RobG

Thomas 'PointedEars' Lahn wrote on 09 nov 2007 in comp.lang.javascript:


So you think it is already done?

It may be an appropriate saying, even if the grammar is wrong (I'll
take your word on that). Cato didn't live to see the total
destruction of Carthage, I bet that Thomas will not see call for total
destruction come true either. :)
 
T

Thomas 'PointedEars' Lahn

RobG said:
It may be an appropriate saying, even if the grammar is wrong (I'll take
your word on that).

The original statement (from Marcus Porcius Cato Censorius called "Cato the
Elder") was "Ceterum censeo Carthaginem esse delendam." which means "I also
think Carthago should be destroyed." There is nothing of a past tense in
it; AFAIK, "Carthaginem delendam esse" is accusative infinitive. Maybe I
did not decline "Prototype.js" properly (comments welcome), but I think it
is understood anyway.
Cato didn't live to see the total destruction of Carthage, I bet that
Thomas will not see call for total destruction come true either. :)

Nevertheless, his repeated ending his speeches in the senate this way and
thereby his repeated motion to vote on this subject eventually led to a
majority among the senators. And consequently, Carthage was destroyed by
the Romans in the Third Punic War :)


Regards,

PointedEars
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 10 nov 2007 in comp.lang.javascript:
The original statement (from Marcus Porcius Cato Censorius called
"Cato the Elder") was "Ceterum censeo Carthaginem esse delendam."
which means "I also think Carthago should be destroyed." There is
nothing of a past tense in it; AFAIK, "Carthaginem delendam esse" is
accusative infinitive. Maybe I did not decline "Prototype.js"
properly (comments welcome), but I think it is understood anyway.

Do read your own words, Doubtfull Thomas, you used his construct:

"censeo aliquid deletum esse"
[= "I'm of the opinion that something IS destroyed"]
(Acc. cum Inf. <http://en.wikipedia.org/wiki/Accusative_and_infinitive>)

A somewhat irregular expression.

I suppose, though it is risky to suppose on your actions,
you wanted to use the gerundive:

"censeo aliquid delendum esse"
[= "I vote that something should be destroyed"]
(Gerundivum, <http://en.wikipedia.org/wiki/Gerundive>
with Accusativus cum Infinitovo)

Censeo + gerundive, though often translated as "I am of the opinion",
in a Senate speech should be "I vote".
Nevertheless, his repeated ending his speeches in the senate this way
and thereby his repeated motion to vote on this subject eventually led
to a majority among the senators. And consequently, Carthage was
destroyed by the Romans in the Third Punic War :)

His Latin was better than yours, that's why.

I hope you take more care "in Javascriptione".
 
T

Tito

s0lnic said:
Tito wrote:
The are some options, if you're not using any JS library, and have a table
like this one:

<table id="some_table">
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
<tr>
<td class="first">item in the first column</td>
<td>item in the second column</td>
</tr>
</table>
[cut]

Many Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,798
Messages
2,569,650
Members
45,382
Latest member
tallzebra

Latest Threads

Top