tags:

views:

33

answers:

2

I'd like to have the first 6 columns of a GridView perform an action, but I need to highlight the entire row when it is clicked. The entire row highlighting is working, but I can't quite get the capturing of the first 6 columns. How do I capture the first 6 columns click in the following where the testing variable is located?:

$("#<%= JobStreamSelectedDealsGridView.ClientID %> tr").filter(function() {
    return $('td', this).length && !$('table', this).length
})
.bind('click', function(e) {
    if (_activeRow) _activeRow.removeClass('gridviewrow-highlighted');
    _activeRow = $(this).addClass('gridviewrow-highlighted');

    var testing = $('td:lt(6)', this);

});
A: 

Try this. You are binding the click even to only the first 6 TD's.

$("#<%= JobStreamSelectedDealsGridView.ClientID %> tr").filter(function() {
    return $('td', this).length && !$('table', this).length
})
find("td:eq(0), td:eq(1), td:eq(2), td:eq(3), td:eq(4), td:eq(5)").bind('click', function(e) {
    if (_activeRow) _activeRow.removeClass('gridviewrow-highlighted');
    _activeRow = $(this).addClass('gridviewrow-highlighted');

    var testing = $(this);

});
Zacho
This will capture the first 6 rows, but it won't highlight the entire row. But I could use this and change _activeRow to get the parent TR for highlighting I guess. Still wondering how I could move the find filter down to the "var testing" line and do a function within that? Like a filter within a filter. How do I do something like this on the "var testing" line:$(this).find("td:eq(0), td:eq(1), td:eq(2), td:eq(3), td:eq(4), td:eq(5)") { function() { doStuff(); } }
RSchmitt
This will actually get the first 6 columns, of whatever row is clicked. I guess I am confused about what you are after.
Zacho
@Zacho - For future reference, [`:lt(6)`](http://api.jquery.com/lt-selector/) :)
Nick Craver
+1  A: 

You can do it like this:

var _activeRow;
$("#<%= JobStreamSelectedDealsGridView.ClientID %> tr")
  .delegate('td:not(:has(table)):lt(6)', 'click', function(e) {
     if (_activeRow) _activeRow.removeClass('gridviewrow-highlighted');
     _activeRow = $(this).closest('tr').addClass('gridviewrow-highlighted');
  });​

You can try it out here. I'm not sure about your parent-row-with-child-table exclusion, but I've replicated it here since I'm sure you had a reason :)

This uses .delegate() to reduce the number of event handlers, it attaches an event handler to each row, and when a <td> that's :lt(6) (less than 6th index, 0-based) gets clicked we go up to the nearest <tr> using .closest() and do the class manipulation there.

Nick Craver
I didn't know of the delegate function - that will do the trick! Thanks!
RSchmitt