tags:

views:

326

answers:

3

1.how to highlight the row when mouse is on,then de-highlight when mouse is out

2.how to update a specified row with new values?

3.how to get number of rows in table?

EDIT: the one with best answer for q2 will be marked as answer for this post:)

A: 

1. http://docs.jquery.com/Events/mouseover

I believe you can use this or .hover.

$('tr').mouseover(function() {

    $(this).addClass('over');

}).mouseout(function() {

    $(this).removeClass('over');

});

And add a over class in your CSS.

2. You don't update a row, you update the table cells inside the row.

$('tr:first td:first').text( 'something' )

3.

alert( $('table tr').length ); // count all descendant table rows
meder
For 2,if there are 6 columns,how to update them most efficiently?
Shore
Unless you are noticing significant slowness and can provide said demo, I wouldn't worry about it.
meder
I don't mean it'll be slower,but looks very un-compact
Shore
your comment code in the other answer seems compact. un-compact usually means multiple lines of dom scripting, I say it's good enough as is.
meder
Multiple lines of similar script,feels very un-compact.
Shore
+2  A: 

One:

$('#mytable').find('tr').hover(function() {
    $(this).addClass('active');
}, function() {
    $(this).removeClass('active');
});

Along with this CSS:

#mytable tr.active td {
    background-color: #ccc;
}

Two:

You said update a "row" but all you can really update is cells, unless you want to create whole new cells.

$(cell).html('Contents');

Or:

var $cell = $('<td>').html('Contents');
$(row).html($cell);

Or if a table row has 3 cells, to update the first one:

$(row).find('td').eq(0).html('Contents');

Three:

$('#mytable').find('tr').length;
Paolo Bergantino
When you use $(row).html('Contents');,will the listeners still work?I mean will the hover function be overwritten by this?
Shore
Yes, the listeners will still work.
Paolo Bergantino
How to most efficiently update 6 cells?$(row).find('td').eq(0).html('Contents'); will make it very very long.
Shore
It's not a big deal if you're just updating one row. You can cache the row cells by doing something like: var $cells = $(row).find('td'); and then $cells.eq(0).html('..'); and $cells.eq(1).html('...');
Paolo Bergantino
One of the principles of jQuery is to make it compact,right?
Shore
You can write a quick plugin to "compact" it if you really want.
Paolo Bergantino
+1  A: 

For the first question:

$("#table1 tr").hover(
   function()
   {
    $(this).addClass("highlight");
   },
   function()
   {
    $(this).removeClass("highlight");
   }

For the third question:

var count = $("table1 tr").length
rahul
How does hover decide which function for mouseover and which for mouseout?
Shore
First one is hover second one is out. hover is not simply a wrapper for mouseover and mouseout, it takes care of a lot of problems that can be caused by children elements of the element you are hovering.
Paolo Bergantino
Right,and now let's focus on the second question:)
Shore
I am not sure about the second question. 'How to update the specified row with new values'? Where are the values stored and what would be the structure of the values? Will it be some plain values or some HTML content?
rahul
plain values,but the final code should be compact:)
Shore