Advertisement

problems on "table row rollover effect"

Moderators: slothy, Calcifer, Chris

problems on "table row rollover effect"

Postby gailw20 » Thu Feb 01, 2007 10:21 am

hi,
could someone help me w the problems i got? thank u!
im using the script i found on http://www.dhtmlgoodies.com/index.html?showDownload=true&whichScript=highlight_table_row

the script doesn't work when:
1. the rollover and onclick effect doesn't work when adding form action
Code: Select all
<form name="frm2" id="frm2">


2. when a user click on a row; it should trigger "toggle_list" function to show item (id "item_1" ~ "item_4"), but it didn't.
Code: Select all
<tr onClick="toggle_list('item_1');">


gail
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Postby Chris » Thu Feb 01, 2007 10:50 am

Hi there,
Where have you added this form?

I have tried the following 3 esamples and it worked with no problems:
form around table
form within table
form outside table

Can you post an url or the complete source code for the page.

Chris
Just here to help ;)
cbolson.com
Chris
Moderator
 
Posts: 613
Joined: Sun Nov 26, 2006 6:20 pm
Location: Malaga, Spain

Postby gailw20 » Thu Feb 01, 2007 10:57 am

hi,
thx for the quick reply!

here's the code inside <body>
Code: Select all
<BODY>
<table border="0" cellpadding="0" cellspacing="0" id="datatable">
  <thead>
    <tr>
      <form name="frm1" id="frm1">
        <th width="3%" scope="col"><input type="checkbox" name="checkbox222" onClick="CheckAll()" value="Check All">        </th>
        <th align="left" scope="col">Item</th>
        <th align="left" scope="col">Type</th>
        <th align="left" scope="col">Status</th>
      </form>
    </tr>
  </thead>
  <tbody>
  <form name="frm2" id="frm2">
    <tr onClick="toggle_list('item_1');">
      <td align="center"><input type="checkbox" name="checkbox23" value="checkbox" onClick="disable:toggle_list('item_1');"></td>
      <td style="padding-left:5px">aaa</td>
      <td style="padding-left:5px">bbb</td>
      <td style="padding-left:5px">ccc</td>
    </tr>
    <tr id="item_1" style="display:none;">
      <td colspan="4" valign="top" style="padding:0"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="data_detail">
        <tr>
          <td colspan="2"> detail detaildetaildetaildetaildetaildetaildetaildetaildetail detaildetaildetaildetailetail detaildetaildetaildetaildetaildetaildetaildetaildetail detaildetaildetaildetail</td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td class="btm" colspan="4" height="1"></td>
    </tr>
    <tr onClick="toggle_list('item_2');">
      <td align="center"><input type="checkbox" name="checkbox23" value="checkbox" onClick="disable:toggle_list('item_2');"></td>
      <td class="content" style="padding-left:5px">ddd</td>
      <td class="content" style="padding-left:5px">eee</td>
      <td class="content" style="padding-left:5px">fff</td>
    </tr>
    <tr id="item_2" style="display:none;">
      <td colspan="4" valign="top" style="padding:0"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="data_detail">
       
        <tr>
          <td colspan="2"> detail detaildetaildetaildetaildetaildetaildetaildetaildetail detaildetaildetaildetailetail detaildetaildetaildetaildetaildetaildetaildetaildetail detaildetaildetaildetail</td>
        </tr>
       
      </table></td>
    </tr>
    <tr>
      <td class="btm" colspan="4" height="1"></td>
    </tr>
  </form>
  </table>

  <SCRIPT type=text/javascript>
addTableRolloverEffect('datatable','tableRollOverEffect1','tableRowClickEffect1');
  </SCRIPT>

</BODY>
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Postby gailw20 » Thu Feb 01, 2007 11:11 am

here's the css:

Code: Select all
#datatable {
   font: 11px Arial, Helvetica, sans-serif;
   border: 1px solid #7386AC;
   border-collapse: collapse;
   width: 100%;
}
#datatable td {
   padding: 2px;
   height:0px;
}
#datatable th {
text-align:left;
   border: 1px solid #7386AC;
   background-color: #Dfe2e7;
   color:#333333;
   font-weight: bold;
   padding-top: 2px;
   padding-bottom: 2px;
   padding-left: 4px;
   padding-right: 4px;
   height:24px;
}
#datatable td.btm {
   border-bottom: 1px dotted #D6DDE6;   
   height:2px;
   
}

.tableRollOverEffect1 {
   background-color: #DFE7F2;
   color: #333333;
   cursor:hand;
}
.tableRollOverEffect2 {
   COLOR: #fff; BACKGROUND-COLOR: #000
}
.tableRowClickEffect1 {
   background-color: #DFE7F2;
   color: #333333;
   cursor:hand;
}
.tableRowClickEffect2 {
   COLOR: #fff; BACKGROUND-COLOR: #00f
}
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Postby gailw20 » Thu Feb 01, 2007 11:29 am

another q,
how to add an image when hover and click on a row? i added an background image to ".tableRollOverEffect1" & ".tableRowClickEffect1", but the images show on every col. i would like it to appear only on the last col of the selected row.

thank u very much for everyone's help

gail
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Postby Chris » Thu Feb 01, 2007 12:08 pm

OK, you are right (obviuosly) the second form seems to stop the rollover effect on the rows.
From your code, it would appear that the first checkbox in the thead row is just for checking/unchecking all the other checkboxes. THis doesn't really need to be within it's own form.
Why not remove this first form and enclose the whole table in the main form and just ignore this first checkbox when using the submitted data.

I realise that the code you have posted me is not complete and there may be other reasons as to why you can't do this.


Chris
Just here to help ;)
cbolson.com
Chris
Moderator
 
Posts: 613
Joined: Sun Nov 26, 2006 6:20 pm
Location: Malaga, Spain

Postby gailw20 » Thu Feb 01, 2007 12:46 pm

hi,
thx. i'll try that later to c if it works. any idea on my other questions?


gail
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Postby Chris » Thu Feb 01, 2007 2:03 pm

OK, I think I have managed it - an interesting challenge for today :)

You wanted an image in the last table cell that would appear on rollover and click and disappear when we roll out - is that right?

Follow these steps:
Add new css style to define "over" state for this last cell (modify the image path for your image:
Code: Select all
.last_td_image{
   background-image: url("../images/download.gif");
   background-repeat: no-repeat;
   background-position: right;
}


Add a new class name to the LAST td cell in your table (ie the cell where you want the image to appear) and give it a UNIQUE id eg:
Code: Select all
<td style="padding-left:5px" id="last_td1" class="last_td_off">ccc</td>

(we DON't need to create a class within the styles for this "off" stage unless you want it to be different from the rest) - we use this class name to identify it in the js code.

Now modify the js code to detect these cells and modify with the new class.
Replace the function "highlightTableRow()" with this - you will notice the new lines at the end where we loop through the row cells to detect the ones with the "last_td_off" class:

Code: Select all
function highlightTableRow()
   {
      var tableObj = this.parentNode;
      if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;

      if(this!=activeRow){
         this.setAttribute('origCl',this.className);
         this.origCl = this.className;
      }
      this.className = arrayOfRolloverClasses[tableObj.id];
      
      
      
      activeRow = this;
      
      //    loop through cells to get last one
      cells = activeRow.getElementsByTagName('TD');
      for(var no=0;no<cells.length;no++){
         if(cells[no].className=='last_td_off'){
            this_cell=document.getElementById(cells[no].id);
            this_cell.setAttribute((document.all ? 'className' : 'class'), 'last_td_image');

         }
      }
   }

The replace the "resetRowStyle()" function with the following (similar additions to the previous function though with slight changes):
Code: Select all
function resetRowStyle()
   {
      var tableObj = this.parentNode;
      if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;

      if(activeRowClickArray[tableObj.id] && this==activeRowClickArray[tableObj.id]){
         this.className = arrayOfClickClasses[tableObj.id];
         return;   
      }
      
      var origCl = this.getAttribute('origCl');
      if(!origCl)origCl = this.origCl;
      this.className=origCl;
      
      //    loop through cells to get last one
      cells = activeRow.getElementsByTagName('TD');
      for(var no=0;no<cells.length;no++){
         if(cells[no].className=='last_td_image'){
            this_cell=document.getElementById(cells[no].id);
            this_cell.setAttribute("class","last_td_off");
            this_cell.setAttribute("className", "last_td_off");   //for IE
         }
      }
      
   }


If this is not everything, it will pretty much send you on your way.
You can see a working demo based on your code (slightly simplified for working) here: http://www.cbolson.com/tests/dhtml_tabl ... _image.php

I hope this will be of some help and that you can follow my instructions ;)

Chris
Just here to help ;)
cbolson.com
Chris
Moderator
 
Posts: 613
Joined: Sun Nov 26, 2006 6:20 pm
Location: Malaga, Spain

Postby Chris » Thu Feb 01, 2007 2:29 pm

Sorry, me again.
I have just realised that, as with most scripts, it could be simpler.
I have created another demo here http://www.cbolson.com/tests/dhtml_tabl ... age_V2.php which should be slightly simpler to implement and more flexiible as regards csss styles.
This version, however, assumes that you alwys want the LAST cell to have the image.
Bascially, rahter than looping through all the cells in the row, it directly modifies the last one with the rollover image.
This means that you DON'T need to add a default class to the cells where you want the effect to occur, but you STILL need to add a UNIQUE id.

Instrucions:
    1. Create the new css style as before with your image:
    2. Add the UNIQUE id to the last table cells
    3. Add the following lines to the end of the "highlightTableRow()" function (where previously we had added the loop:
Code: Select all
//    get last cell - simple ASLONG as we are only modifying the last cell
      cells = activeRow.getElementsByTagName('TD');
      total_cells=(cells.length-1);
      this_cell=document.getElementById(cells[total_cells].id);
      this_cell.setAttribute((document.all ? 'className' : 'class'), 'last_td_image');

    4. Add the follwoing lines to the end of the "resetRowStyle()" function
Code: Select all
//    get last cell - simple ASLONG as we are only modifying the last cell
      cells = activeRow.getElementsByTagName('TD');
      total_cells=(cells.length-1);
      this_cell=document.getElementById(cells[total_cells].id);
      this_cell.setAttribute((document.all ? 'className' : 'class'), '');


As you will see, this is slightly less flexible, but maybe simpler to understand and implement.

I hope I haven't confused you too much with 2 versions :(

Chris
Chris
Moderator
 
Posts: 613
Joined: Sun Nov 26, 2006 6:20 pm
Location: Malaga, Spain

Postby gailw20 » Thu Feb 01, 2007 4:56 pm

thank u Chris!
the last td image works!! but the toggling & chkall function still not working though. :( sorry to keep bothering u. i'll copy the code here for everyone to take a look and help me out! thank u!!

p.s. i took the code from version 1 http://www.cbolson.com/tests/dhtml_table_rows/with_image.php
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Postby gailw20 » Thu Feb 01, 2007 5:05 pm

html:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><title>Chris Bolson - TABLE ROWS WITH IMAGE</title>
<body>
<form name="frm2" id="frm2" action="">
  <table id="datatable" border="0" cellpadding="0" cellspacing="0" width="600">
    <thead>
      <tr>
        <th scope="col" width="3%"><input onclick="CheckAll()" value="Check All" type="checkbox" name="frm1" id="frm1">
        </th>
        <th scope="col" align="left">Item</th>
        <th scope="col" align="left">Type</th>
        <th scope="col" align="left">Status</th>
      </tr>
    </thead>
    <tbody>
      <tr class="" origcl="" onclick="toggle_list('item_1');" >
        <td align="center"><input name="frm2" id="frm2" value="checkbox" onclick="disable:toggle_list('item_1');" type="checkbox"></td>
        <td style="padding-left: 5px;">aaa</td>
        <td style="padding-left: 5px;">bbb</td>
        <td classname="last_td_off" style="padding-left: 5px;" id="last_td1" class="last_td_off">ccc</td>
      </tr>
     <tr id="item_1" style="display:none;">
        <td colspan="4">frethrhyehtherthrth</td>
      </tr>
      <tr class="tableRowClickEffect1" origcl="" onclick="toggle_list('item_2');">
        <td align="center"><input name="frm2" id="frm2" value="checkbox" onclick="disable:toggle_list('item_2');" type="checkbox"></td>
        <td class="content" style="padding-left: 5px;">ddd</td>
        <td class="content" style="padding-left: 5px;">eee</td>
        <td classname="last_td_off" style="padding-left: 5px;" id="last_td2" class="last_td_image">fff</td>
      </tr>
      <tr id="item_2" style="display:none;">
        <td colspan="4">frethrhyehtherthrth</td>
      </tr>   
    </tbody>
  </table>
</form>
  <script type="text/javascript">
addTableRolloverEffect('datatable','tableRollOverEffect1','tableRowClickEffect1','last_td');
  </script>
</body></html>
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Postby gailw20 » Thu Feb 01, 2007 5:08 pm

"checkAll" & "toggle_list" function:
Code: Select all
function CheckAll()
{
   count = document.frm2.elements.length;
   if(document.frm1.elements[0].checked == 1){
       for (i=0; i < count; i++)
         document.frm2.elements[i].checked = 1;
   }
   else {
      for (i=0; i < count; i++)
         document.frm2.elements[i].checked = 0;
   }
}
function toggle_list(id) {
  var e = document.getElementById(id);
  if(e.style.display == 'none') {
      e.style.display = 'block';
  } else {
      e.style.display = 'none';
  }
}

gail
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Postby Chris » Thu Feb 01, 2007 5:33 pm

As to your CheckAll() function you have a small error in the form name, you start off by referencing frm2 but then frm1. Modify this to be the right name of your form and it should work.

Code: Select all
function CheckAll()
{
   count = document.frm2.elements.length;
   if(document.frm1.elements[0].checked == 1){
       for (i=0; i < count; i++)
         document.frm2.elements[i].checked = 1;
   }
   else {
      for (i=0; i < count; i++)
         document.frm2.elements[i].checked = 0;
   }
}
Just here to help ;)
cbolson.com
Chris
Moderator
 
Posts: 613
Joined: Sun Nov 26, 2006 6:20 pm
Location: Malaga, Spain

Postby Chris » Thu Feb 01, 2007 5:56 pm

The show and hide question was slightly more complicated.
The solution I have come up with is to take advantage of the dhtmlgoodies function already in place for the "onclick" action on the rows.

Firstly you need to give the "shown" rows (ie the ones you will be clicking on) UNIQUE ids, something like "row_1" (very original) where the number mUST conicide with the number of the hidden row - "item_1" in your script.

Then you must add these lines at the end of "clickOnTableRow()" function:
Code: Select all
//    get hiddden row name by replaceing "row" with "item" (names of rows)
row_2_show   = this_row_id.replace(this.id,"item");
toggle_list(row_2_show);


This replaces the "row" from the selected row id and replaces it with "item".

You now DON'T need the hrefs in the table rows that weren't working anyway.

I hope this is what you needed :lol:
I have added it to V1 of my examples here: http://www.cbolson.com/tests/dhtml_tabl ... _image.php

Chris
Just here to help ;)
cbolson.com
Chris
Moderator
 
Posts: 613
Joined: Sun Nov 26, 2006 6:20 pm
Location: Malaga, Spain

Postby gailw20 » Thu Feb 01, 2007 6:29 pm

hi chris,
me again...:oops:
the CheckAll() function works perfect! but the toggle_list() function still doesn't work though(the item doesn't show up when clicking on a row). thank u so much.

gail
gailw20
 
Posts: 17
Joined: Thu Feb 01, 2007 9:45 am

Next

Return to Table script questions

Who is online

Users browsing this forum: No registered users and 0 guests

cron