-
- <table id="sometable">
- <tr id="row-1">
- <td>Field1</td>
- <td>Field2</td>
- </tr>
- <tr id="row-2">
- <td>Field1</td>
- <td>Field2</td>
- </tr>
- </table>
-
Lets assume the above html.
First of all there are no "official" ways to "select" a row in a html table. So you will have to script the whole thing yourself.
You will have to add events to all the children of the table. I'm not in the habbit of giving all the code, since that doesnt teach anyone anything. So i'll explain the basics (atleast a way how i would do it).
First of all you will have to go through all the tr's of the form and add events to it.
Something like this would probably do:
-
- // selectedRow will contain the id of the selected row. I'm putting it in a global scope, but ofcourse this should be inside some class (its the neater way)
- var selectedRow = false;
-
- // Loop through all the "TR" elements in the table.
- document.id('sometable').getChildren('tr').each( function( element, index) {
-
- // Add events to all the items
- element.addEvent('click', function(event) {
-
- // Get the number after row-, this is our row id in this case.
- var idArray = element.id.split("-");
- var rowId = idArray[1];
-
- // Store the selected row in the global variable.
- selectedRow = rowId;
-
- }
-
- })
-
-
The above code (should) set the global variable "selectedRow" with the number in the row id (i.e. when clicking on the row with id "row-1", selectedRow will be "1")
Things to take care of yourself:
- Remember the old selected row, so you can do something with background colors.
- Build this mess into a decent class
- Extend the event to do something with your other table
Had to write this in about 5 minutes. So I have no time to either test, nor make it more decent.
Hope this gets you in the right direction.
Ill try and answer your questions later on if you have any.