[solved] Autoselect a row in HtmlTable

here you will find only solved Help topics

Moderator: 1.2 Moderators

[solved] Autoselect a row in HtmlTable

Postby ibbernik on Mon Mar 01, 2010 3:51 pm

Hi!

Does anyone how to autoselect a row in HtmlTable?

My problem is that i want to select a row in one HtmlTable and the selection will then select a row in another HtmlTable. I can't figure out how to get access to the rows in a HtmlTable.

Regards
Last edited by ibbernik on Tue Mar 02, 2010 8:58 pm, edited 1 time in total.
ibbernik
 
Posts: 4
Joined: Wed Feb 24, 2010 11:56 am

Re: Autoselect a row in HtmlTable

Postby dovereem on Mon Mar 01, 2010 5:49 pm

  1.  
  2. <table id="sometable">
  3.     <tr id="row-1">
  4.         <td>Field1</td>
  5.         <td>Field2</td>
  6.     </tr>
  7.     <tr id="row-2">
  8.         <td>Field1</td>
  9.         <td>Field2</td>
  10.     </tr>
  11. </table>
  12.  


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:
  1.  
  2. // 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)
  3. var selectedRow = false;
  4.  
  5. // Loop through all the "TR" elements in the table.
  6. document.id('sometable').getChildren('tr').each( function( element, index) {
  7.  
  8.     // Add events to all the items
  9.     element.addEvent('click', function(event) {
  10.  
  11.         // Get the number after row-, this is our row id in this case.
  12.         var idArray = element.id.split("-");
  13.         var rowId = idArray[1];
  14.        
  15.         // Store the selected row in the global variable.
  16.         selectedRow = rowId;
  17.  
  18.     }
  19.  
  20. })
  21.  
  22.  


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.
dovereem
 
Posts: 10
Joined: Wed Feb 24, 2010 11:28 am

[solved] Re: Autoselect a row in HtmlTable

Postby ibbernik on Tue Mar 02, 2010 8:58 pm

Thanks for your reply. It was not exactly what i was looking for. I am working with the mootools more HtmlTable object and all you have explaned is already in that object. My problem was that i couldn't find out how to autoselect a row in one HtmlTable from within another HtmlTable. I now have found the answer to my own question and i just want to tell how i did. Please write if you see any problems in the way i have chosen.

  1.  
  2. var selrow=table.body.rows.namedItem("theRowId");
  3. table.focusRow('rowFocus',selrow);
  4.  


Since the HtmlTable behind is a javascript HtmlTableElement i could access the table rows by doing like in the first line:
table.body.rows which accesses the tr elements in the table's tbody tag but it returns a javascript HtmlCollection. With this HtmlCollection i can now call namedItem with an id or name string and the specific row is returned.

The second line in the above code does the same as if you had clicked on the row, with your mouse, in the HtmlTable. By calling the function focusRow, the row in the destination HtmlTable will be selected by adding the table-tr-selected class.

Regards
ibbernik
 
Posts: 4
Joined: Wed Feb 24, 2010 11:56 am


Return to Solutions

Who is online

Users browsing this forum: No registered users and 1 guest