PDA

View Full Version : Detecting single/multiple select in grid



arnair
3 Nov 2006, 12:55 PM
First off: incredible work Jack. I'm having a blast using your libraries in my app, and learning some advanced javascript form your code while I'm at it.

I have an issue that I haven't been able find a solution for as yet - I need to figure out if just one row or multiple rows have been selected in the grid.

My app has a details pane next to the grid and the idea is to retrieve and show details in that pane if a single row has been selected, and disabling it and showing "Multiple rows selected" if more than 1 row has been selected.

I've been playing around with both onRowSelect and onSelectionChange on the DefaultSelectionModel. The problem is that I get a series of events in either case when selecting a range of rows (using Shift+click). Ideally, I'd like to somehow narrow these down to two events:
1. Single row selected - here's the row index, and
2. Multiple rows selected

I tried checking for getCount() in the event listeners, but if I select a row and then Shift-click two rows down I get the selection counts as 0, then 1, then 2 (the grid seems to deselect the currently selected row, then select it again). The sel count=1 event is interpreted by my app as a single row select and so it fetches the details for that row from the server, but then it immediately gets the sel count=2 event and hides the details pane.

It's not a showstopper, but an annoyance. Anyway I can eliminate this? Thanks!

BTW, library version is 0.32.3.1.

jack.slocum
3 Nov 2006, 1:09 PM
For multi select your best option is to just use the selectionchange event and interpret the results.

If your application is sensitive to selections, you may want to implement a buffering mechanism using a DelayedTask object so you are not executing lots of updates in succession.


var task = new YAHOO.ext.util.DelayedTask(yourSelectionHandler, youScope);
grid.getSelectionModel().on('selectionchange', function(){
task.delay(100);
});

What this does is buffer selection events so you only get the last one. This is a good idea for keyboard (up/down) selection anyway. In your handler, you would then call to get the current selection state.

Does that make sense?

arnair
3 Nov 2006, 1:18 PM
That's exactly what I was thinking I could do. I wasn't aware of your DelayedTask class though - that'll definitely save me some work. Thanks a lot for the quick reply.

MANOJAPPLE
16 Jul 2007, 11:00 PM
Hi,
I want to select multiple rows.
At the same time i want to make sure that the user is not able to select more than two rows.
Can you suggest me a solution for this.

Regards,
Manny