ListGrid sort listener

Hello all,

Is there any way to add a listener to (or be notified of) a change in sorting in a listgrid? I need to execute some logic when the sorting of any column in a list grid has changed. Is this possible?

thanks

I need to do the same thing. Anyone have an idea how to accomplish this? I figured I might put a click handler on the column headers, but I don’t know how to access them as yet.

Hi,

The ListGrid header clicks are not yet handled in SmartGWT.

Here is the thread related to the issue:
http://forums.smartclient.com/showthread.php?t=3055

And the bug report on SmartGWT’s google code page:
http://code.google.com/p/smartgwt/issues/detail?id=24&q=header&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary%20Reporter

If you find a work around, I’m interested…

Also, I’ve been experiencing some issues when calling ListGrid.getSortDirection().
http://forums.smartclient.com/showthread.php?t=4409
Thought you might have come accross the same problem.

We’re looking into exposing this in SmartGWT, and the issue when calling ListGrid.getSortDirection()

workaround

add a ClickHandler and look for the event row equal to -1

-1 means the header…

new ClickHandler() {
   public void onClick(ClickEvent event) {  
      int eventColumn = ((ListGrid)event.getSource()).getEventColumn();
      int eventRow = ((ListGrid)event.getSource()).getEventRow();
      if (eventRow == -1) {
         SC.say("Event Column: "+eventColumn + " - Event Row: " + eventRow);
      }
   }
}

im still new to SmartGWT so im hoping that was correct…

Hi rasgallego,

Good idea!
Unfortunately, that doesn’t help stopping a column from being sorted (and replacing that by a custom action).

Hi dn,

Yeah, unfortunately it won’t.
But for tracking if a user sorted the grid then its ok for now.

I just recently found out that there is also a problem with that workaround.
When the user clicks on the submenu in the column, the event also gets triggered.

You can add a clickHandler at the level of the MenuItem

Hi,

I also need to change the functionality of sorting. What i did is to override the ListGrid’s getHeaderContextMenuItems(final Integer fieldNum) method. In it, I check the MenuItems that correspond to the Sort Ascending and Sort Descending. For those, i added a ClickHandler. In my click handler, i implement my own sorting functionality.
I don’t know if that might be helpful to you.

However, my problem is that since my clickhandler does not change the sorting that takes place when the header is clicked, what happens is:
If i click on column B’s header, the client-side sorting takes place.
Then if i choose column A’s modified “Sort Descending” menu item, the server side sorting that i implemented is done, but the records retrieved are re-sorted in the grid by column B’s field and direction and the sorting arrow is still showing on Column B, not column A.

The grid’s new sort field and sort direction is not being set to the new values, although i tried adding all the following lines, but nothing worked:

ListGridWithButtonsCol.this.unsort();
ListGridWithButtonsCol.this.setSortField(fieldNum);
ListGridWithButtonsCol.this.setSortDirection(SortDirection.DESCENDING);
ListGridWithButtonsCol.this.setShowSortArrow(SortArrow.NONE);
ListGridWithButtonsCol.this.refreshRow(0);
ListGridWithButtonsCol.this.adjustForContent(true);
ListGridWithButtonsCol.this.headerClick(fieldNum);

Any idea why and how i can fix this?
Any help is greatly appreciated.
Thanks.

There is a designed-in way to customize sorting - setting a SortNormalizer. Unless you have very unusual requirements, this is the right approach and you should abandon your attempts to customize the menu.

Hi,

Thanks for your reply.

I tried it. But, this normalizer is called for every record in the grid. So, if i implement in it my RPC call to the server’s sort method, it will be called 10 times for a 10-sized page, 20 times for a 20-sized page, etc.
So, this wouldn’t be efficient at all.

Please let me know if there’s another solution or if I’ve misunderstood anything about this suggested solution.

Thanks in advance,
Nicole.

Hi Nicole,

All sort algorithms visit every item in the dataset, so this is normal and efficient, and this is how all the built-in sort routines work.

Here is a potentially unusual approach that doesn’t seem achievable without handling the header click:

Given an initial set of N records (can be a big number), a subset of P < N records is shown on the ListGrid.
On a header click, the sort is applied to the whole set of N records and the ListGrid updated accordingly, showing again a subset of P records.

Any updates on the bugfix? :slight_smile:

Thanks,

Dogan

@dn we are adding a handler for this, but your proposed use case seems pretty much unrelated?

Hi again,

Isomorphic, the thing is that i wouldn’t want to visit the sorting for every record, since i am doing the sorting on the server side. In fact, the scenario mentioned by dn is exactly what i am doing. When sorting is chosen, i have an RPC call to the server, where the sorting on the full set of records is done, and P sorted records are returned. Since they’re already sorted, i don’t want the sortnormalizer to resort anything.

I also just want to override the HeaderClick as dn mentioned. I tried it, but my overridden method never gets called. Is that a bug?

Thanks.

Hi Nicole,

You seem like you may be re-inventing the wheel. When you use databinding your grid has a ResultSet that automatically switches between client and server-side sorting as necessary. See the ResultSet docs.

ListGrid.addHeaderClickHandler is now in SVN.

Sanjiv

Thank you!

server side sorting

I have a similar problem regarding sorting.
All my sorting is done server-side.
I want to somehow force the menu item to kick off a new RPC call to my server everytime Sort Ascending or Sort Descending is clicked.
How do I force server side sorting rather than the default client side sorting ?

I have tried using transformRequest in DataSource to inject the right parameters for server-side sorting- the issue is that this method is not called when I click on the sort menu item. How can I force a data update ?

Try calling listGrid.invalidateCache() first.

Thanks.
Could you be more specific ?
Should I override the getHeaderContextMenuItems method in ListGrid and add listeners to the Sorting menu items as suggested above ?
Or is there a cleaner way ?
I kind of liked the Menu items as they were. I just wanted the RPC call to happen when I clicked “Sort Ascending” for example rather than client soide sorting.
Are you suggesting adding an invalidateCache() call via a clickHandler on the header menu item ?

I’ve also tried setting this to force server side calls:

	  isc.ResultSet.addProperties({
		    useClientFiltering:false,
    		useClientSorting:false
		});

But although upon clicking “Sort Ascending” the grid seems to invalidate (in that it blanks and provides a loading message) no actual server calls are made.

Any help gratefully received,
Phil