ExtJS 4.0: Keeping a row at the top position in a GridPanel

I’ve had the need for a grid I am working on keep a row representing the user currently logged in as the top row in the grid. Mind you there is no paging on this grid. I am using a slightly outdated version of ExtJS, i.e 4.0. I tried listening to the viewready event on the grid itself or its view but those events were not firing. Seems the viewready event is only available on the view as of ExtJS 4.1. To alleviate this problem the next best thing available to me was the refresh event, which event though would be called many times over did the job of informing me of when the view rows are actually displayed, which the afterrender event does not provide.

I ended adding the following listener to my grid’s viewConfig definition:


viewConfig:{
     loadingUseMsg:true,
     listeners:{
          refresh:{
                fn:me.moveUserRowToTop,
                scope:this
          }
    }
}

and the moveUserRowToTop function does this:


moveUserRowToTop:function(gridView, eOpts)
{
var store = gridView.getStore();
var currentUserRecordIndex = store.find('id',currentUser.get('id'))
if(currentUserRecordIndex > 0)
{
    var currentUserRecord = store.getAt(currentUserRecordIndex);
    store.removeAt(currentUserRecordIndex);
    store.insert(0, [currentUserRecord]);
    gridView.refresh();

}

}

So as you can see even though the event gets fired a lot depending on your grid usage, mine is average it will only move the row if it is present and at an index greater than 0, which is exactly my use case.

Hope it helps

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s