Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2013, 08:37 PM   PM User | #1
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Navigate (highlight) a specified row in a table

Hello,

I need your help,

How can I go about creating a specific function or building onto the existing code below that would allow me to go to (highlight) a specific row in a table. It is noted that the table headers should be always be exempt, therefore, start the row count at '0' after the table headers.

Ie. function goToRow('3')

and this function would highlight row 3

Code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#mstrTable {
     border: 1px solid black
}
#mstrTable td, th {
     border: 1px solid black
}

#mstrTable tr.normal td {
    color: black;
    background-color: white;
}
#mstrTable tr.highlighted td {
    color: white;
    background-color: gray;
}
</style>
</head>
<body>
  <table id="mstrTable">
     <thead>
      <tr>
        <th>File Number</th>
        <th>Date1</th>
        <th>Date2</th>
        <th>Status</th>
        <th>Num.</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>KABC</td>
        <td>09/12/2002</td>
        <td>09/12/2002</td>
        <td>Submitted</td>
        <td>0</td>

      </tr>

      <tr>
        <td>KCBS</td>
        <td>09/11/2002</td>
        <td>09/11/2002</td>
        <td>Approved</td>
        <td>1&nbsp;</td>
      </tr>

      <tr>
        <td>WFLA</td>
        <td>09/11/2002</td>
        <td>09/11/2002</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr>
        <td>WTSP</td>
        <td>09/15/2002</td>
        <td>09/15/2002</td>
        <td>In-Progress</td>
        <td>3</td>
      </tr>
    </tbody>
  </table>

<script type="text/javascript">

var table = document.getElementById("mstrTable");
var thead = table.getElementsByTagName("thead")[0];
var tbody = table.getElementsByTagName("tbody")[0];

tbody.onclick = function (e) {
   e = e || window.event;
   var td = e.target || e.srcElement; //assumes there are no other elements inside the td
   var row = td.parentNode;
    alert('Row is ' + (row.rowIndex - 1))
   if (this.lst&&this.lst!=row){
    this.lst.className='';
   }
   row.className = row.className==="highlighted" ? "" : "highlighted";
   this.lst=row;
}

thead.onclick = function (e) {
   e = e || window.event;
   var th = e.target || e.srcElement;  //assumes there are no other elements in the th
   alert(th.innerHTML);
}
</script>
</body>
</html>
jason_kelly is offline   Reply With Quote
Old 01-18-2013, 09:33 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#mstrTable {
     border: 1px solid black
}
#mstrTable td, th {
     border: 1px solid black
}

#mstrTable tr.normal td {
    color: black;
    background-color: white;
}
.highlighted {
    color: white;
    background-color: red;
}
</style>
</head>
<body>
  <table>
     <thead>
      <tr>
        <th>File Number</th>
        <th>Date1</th>
        <th>Date2</th>
        <th>Status</th>
        <th>Num.</th>
      </tr>
    </thead>
    <tbody id="mstrTable">
      <tr>
        <td>KABC</td>
        <td>09/12/2002</td>
        <td>09/12/2002</td>
        <td>Submitted</td>
        <td>0</td>

      </tr>

      <tr>
        <td>KCBS</td>
        <td>09/11/2002</td>
        <td>09/11/2002</td>
        <td>Approved</td>
        <td>1&nbsp;</td>
      </tr>

      <tr>
        <td>WFLA</td>
        <td>09/11/2002</td>
        <td>09/11/2002</td>
        <td>Submitted</td>
        <td>2</td>
      </tr>
      <tr>
        <td>WTSP</td>
        <td>09/15/2002</td>
        <td>09/15/2002</td>
        <td>In-Progress</td>
        <td>3</td>
      </tr>
    </tbody>
  </table>
<input type="button" name="" value="GoTo 0" onmouseup="GoTo('mstrTable',0);" />
<input type="button" name="" value="GoTo 1" onmouseup="GoTo('mstrTable',1);" />
<input type="button" name="" value="GoTo 2" onmouseup="GoTo('mstrTable',2);" />
<input type="button" name="" value="GoTo 3" onmouseup="GoTo('mstrTable',3);" />

<script type="text/javascript">

 function GoTo(id,nu){
  var obj=document.getElementById(id),trs=obj.getElementsByTagName('TR');;
  if (trs[nu]){
   if (GoTo.lst&&GoTo.lst!=trs[nu]){
    GoTo.lst.className='';
   }
   trs[nu].className = trs[nu].className=="highlighted" ? "" : "highlighted";
   GoTo.lst=trs[nu];
  }
 }
</script>
</body>
</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Users who have thanked vwphillips for this post:
jason_kelly (01-19-2013)
Old 01-19-2013, 12:18 AM   PM User | #3
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Thanks very much Phil.

It works like a charm. But at row 0 it doesn't shade it in. Row 0 should start after the table headers. As I will be doing database work in the back end, it is important that the sequence be followed:

HEADER
ROW0
ROW1
ROW2
ROW3
ROW4
jason_kelly is offline   Reply With Quote
Old 01-19-2013, 12:52 AM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by jason_kelly View Post
Thanks very much Phil.

It works like a charm. But at row 0 it doesn't shade it in. Row 0 should start after the table headers. As I will be doing database work in the back end, it is important that the sequence be followed:

HEADER
ROW0
ROW1
ROW2
ROW3
ROW4
Just add one!
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
jason_kelly (01-19-2013)
Old 01-19-2013, 04:53 AM   PM User | #5
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Like this?

nu = nu + 1

Code:
function GoTo(id,nu){
  var obj=document.getElementById(id),trs=obj.getElementsByTagName('TR');;
  nu = nu + 1
  if (trs[nu]){
   if (GoTo.lst&&GoTo.lst!=trs[nu]){
    GoTo.lst.className='';
   }
   trs[nu].className = trs[nu].className=="highlighted" ? "" : "highlighted";
   GoTo.lst=trs[nu];
  }
 }
jason_kelly is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:57 PM.


Advertisement
Log in to turn off these ads.