www.webdeveloper.com
Results 1 to 11 of 11

Thread: show/hide table row

  1. #1
    Join Date
    Sep 2007
    Posts
    27

    show/hide table row

    Is it possible to show/hide rows in a table?

    I'm able to do hide tables by wrapping them in divs, but i need to hide JUST the columns. Let me know if this is possible, or if there is a way to emulate it.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Try a search of this forum for items like this: show hide table columns for some other ideas http://www.webdeveloper.com/forum/sh...+table+columns

  3. #3
    Join Date
    Sep 2007
    Posts
    27
    My question was how to hide a row, akak hide/show this whole section:

    <tr>
    <td>
    Content
    </td>
    <td>
    Content
    </td>
    </tr>

    That link only explains how to hide a span of content, something I can already do.

  4. #4
    Join Date
    Nov 2006
    Posts
    526
    Dang. I thought you had asked to hide columns (which is more difficult) so I wrote code to do that. Oh well, I added additional code to hide a row and left the column code in as well:

    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    
      function hideCol(column, hideVal) {
    
        var row = 0;
        var displayStyle;
    
        while (document.getElementById('r'+row+'c'+column)) {
          var displayStyle = (hideVal!=true)? 'inline' : 'none' ;
          document.getElementById('r'+row+'c'+column).style.display = displayStyle;
          row++;
        }
    
      }
    
      function hideRow(row, hideVal) {
        if (document.getElementById('row'+row)) {
          var displayStyle = (hideVal!=true)? 'inline' : 'none' ;
          document.getElementById('row'+row).style.display = displayStyle;
        }
      }
    
    </script>
    
    </head>
    <body>
    
      <table border="1" cellpadding="10">
        <tr>
          <th>&nbsp;</th>
          <th id="r0c1">Column 1</th>
          <th id="r0c2">Column 2</th>
          <th id="r0c3">Column 3</th>
          <th id="r0c4">Column 4</th>
        </tr>
        <tr id="row1">
          <td><b>Row 1</b></td>
          <td id="r1c1">Row-1 Col-1</td>
          <td id="r1c2">Row-1 Col-2</td>
          <td id="r1c3">Row-1 Col-3</td>
          <td id="r1c4">Row-1 Col-4</td>
        </tr>
        <tr id="row2">
          <td><b>Row 2</b></td>
          <td id="r2c1">Row-2 Col-1</td>
          <td id="r2c2">Row-2 Col-2</td>
          <td id="r2c3">Row-2 Col-3</td>
          <td id="r2c4">Row-2 Col-4</td>
        </tr>
        <tr id="row3">
          <td><b>Row 3</b></td>
          <td id="r3c1">Row-3 Col-1</td>
          <td id="r3c2">Row-3 Col-2</td>
          <td id="r3c3">Row-3 Col-3</td>
          <td id="r3c4">Row-3 Col-4</td>
        </tr>
        <tr id="row4">
          <td><b>Row 4</b></td>
          <td id="r4c1">Row-4 Col-1</td>
          <td id="r4c2">Row-4 Col-2</td>
          <td id="r4c3">Row-4 Col-3</td>
          <td id="r4c4">Row-4 Col-4</td>
        </tr>
    
      </table>
    <br>
    <a href="#" onclick="hideRow(1, true);">Hide Row 1</a>
    &nbsp;&nbsp;&nbsp;
    <a href="#" onclick="hideRow(1, false);">Show Row 1</a>
    <br>
    <a href="#" onclick="hideRow(2, true);">Hide Row 2</a>
    &nbsp;&nbsp;&nbsp;
    <a href="#" onclick="hideRow(2, false);">Show Row 2</a>
    <br>
    <a href="#" onclick="hideRow(3, true);">Hide Row 3</a>
    &nbsp;&nbsp;&nbsp;
    <a href="#" onclick="hideRow(3, false);">Show Row 3</a>
    <br>
    <a href="#" onclick="hideRow(4, true);">Hide Row 4</a>
    &nbsp;&nbsp;&nbsp;
    <a href="#" onclick="hideRow(4, false);">Show Row 4</a>
    <br><br>
    
    <a href="#" onclick="hideCol(1, true);">Hide Column 1</a>
    &nbsp;&nbsp;&nbsp;
    <a href="#" onclick="hideCol(1, false);">Show Column 1</a>
    <br>
    <a href="#" onclick="hideCol(2, true);">Hide Column 2</a>
    &nbsp;&nbsp;&nbsp;
    <a href="#" onclick="hideCol(2, false);">Show Column 2</a>
    <br>
    <a href="#" onclick="hideCol(3, true);">Hide Column 3</a>
    &nbsp;&nbsp;&nbsp;
    <a href="#" onclick="hideCol(3, false);">Show Column 3</a>
    <br>
    <a href="#" onclick="hideCol(4, true);">Hide Column 4</a>
    &nbsp;&nbsp;&nbsp;
    <a href="#" onclick="hideCol(4, false);">Show Column 4</a>
    <br>
    
    </body>
    </html>

  5. #5
    Join Date
    Sep 2007
    Posts
    27
    Thank you!

  6. #6
    Join Date
    Aug 2005
    Location
    Singapore
    Posts
    155
    [URL=http://www.samliew.com]Personal Website[/COLOR] | Programming Page

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Question Confused:

    Concerning post #2 and the response of #3, this was the ORIGINAL requirement.
    but i need to hide JUST the columns
    Hope you are getting what you want.

  8. #8
    Join Date
    Aug 2005
    Location
    Singapore
    Posts
    155
    Yup, but I expanded on that in case next time somebody wants to hide rows instead, or something else.
    [URL=http://www.samliew.com]Personal Website[/COLOR] | Programming Page

  9. #9
    Join Date
    Mar 2008
    Posts
    1
    Thank you Mjdamato - you Rock! - I really needed your help and your code is easy to understand as well as edit - thank you again.

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    var displayStyle = (hideVal!=true)? 'inline' : 'none' ;
    That will work in IE only. Mozilla (and all the DOM full compliant browsers) will not accept "inline" as positive display for table's elements. You should use a empty value for a positive display, as a crossbrowser solution:
    Code:
    var displayStyle = (hideVal!=true)? '' : 'none' ;

  11. #11
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    just a thought, nobody feel is useless the function hideCol?
    function hideCol(column, hideVal) {

    var row = 0;
    var displayStyle;

    while (document.getElementById('r'+row+'c'+column)) {
    var displayStyle = (hideVal!=true)? 'inline' : 'none' ;
    document.getElementById('r'+row+'c'+column).style.display = displayStyle;
    row++;
    }

    }
    instead of traveling each row to hide the proper cell, why not use the collapse property for column? so, the proper html is:
    HTML Code:
    <table id='mytable'>
    <col /><col /><col />...
    ...
    </table>
    where <col /> represent an single column and there should be as many <col /> as necessary (that is, a <col /> for each table's column). then, hideCol is redefined to apply col.style.visibility = 'collapse'; to the desired column.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles