Ext


Go Back   Ext JS Forums > Ext JS Community Forums (2.0) > Ext 2.x: Help

Reply
 
Thread Tools
  #1  
Old 02-13-2008, 05:26 PM
fallenrayne fallenrayne is offline
Ext User
 
Join Date: Nov 2007
Posts: 72
fallenrayne is on a distinguished road
Default Setting a store SortKey

Hi,

Is is possible to set up a sort key for a store based on the data. i.e. I am recieving: ['Bob','Tom','Fred','Jim'] but I want it to sort as Jim,Fred,Bob,Tom. So somehow I want to put a sortkey to those like:
switch(name){
  case 'Jim':
    store.data.sortKey = 1;
    break;
  case 'Fred':
    store.data.sortKey = 2;
    break;
  case 'Bob':
    store.data.sortKey = 3;
    break;
  case 'Tom':
    store.data.sortKey = 4;
    break;
  default:
    store.data.sortKey = 5;
}
and then sort the store for my grid based off of that sortKey. I hope I explained that well enough.

Thanks for your time and support,
__________________
------------
Brandon
Reply With Quote
  #2  
Old 02-13-2008, 06:16 PM
devnull devnull is offline
Ext JS - Support Team
 
Join Date: Jul 2007
Posts: 3,128
devnull is on a distinguished road
Default

yess, i dont see why not. I also dont see why you couldnt just apply the sorting on the server before you send it though...
Reply With Quote
  #3  
Old 02-13-2008, 06:23 PM
fallenrayne fallenrayne is offline
Ext User
 
Join Date: Nov 2007
Posts: 72
fallenrayne is on a distinguished road
Default

This data is being pulled from another source so I don't have control over the sorting that I recieve. With that, what do you think is the best method for doing this? I already have it set up to make the sortKey in a renderer and that seems to be working fine but then when I try to sort the grid using :
grid.store.sort('sortKey','ASC'); or grid.getStore().sort('sortKey','ASC')
it won't sort the grid. Am I doing the sorting wrong or is something else entirely wrong with this.

Thank you for your help,
__________________
------------
Brandon
Reply With Quote
  #4  
Old 02-13-2008, 06:38 PM
devnull devnull is offline
Ext JS - Support Team
 
Join Date: Jul 2007
Posts: 3,128
devnull is on a distinguished road
Default

i think after you sort the store, the grid wont get an event telling it to update its view so you have to do it manually. (grid.getView().refresh() i think, check the api).
Reply With Quote
  #5  
Old 02-13-2008, 07:04 PM
fallenrayne fallenrayne is offline
Ext User
 
Join Date: Nov 2007
Posts: 72
fallenrayne is on a distinguished road
Default

I'm getting a "this.grid is null or not an object" error when I try to do grid.getView().refresh. That looked like it was the proper way to do it from the API, I'm not quite sure why I would be getting this error at this point.

Thanks for your help,
__________________
------------
Brandon
Reply With Quote
  #6  
Old 02-20-2008, 10:19 AM
fallenrayne fallenrayne is offline
Ext User
 
Join Date: Nov 2007
Posts: 72
fallenrayne is on a distinguished road
Default

Ok, I need to rehash this one. I have it now built so that it creates a sortKey column and fills it based on the status column. The problem is that when I try to set that column as the default sort it will show it as selected but it wasn't sorted.

My renderer:
function sortKey(val,p,record){
  switch(val.toLowerCase()){
    case 'new':
      record.data.sortKey = 1;
      break;
    case 'broken':
      record.data.sortKey = 2;
      break;
    case 'destroyed':
      record.data.sortKey = 3;
      break;
    default:
      record.data.sortKey = 4;
  }
  return val;
}
This does add the right data into the sortKey column but when I try myGrid.getStore().setDefaultSort('sortKey') or try myGrid.getStore().sort('sortKey') it will not sort that column.

Anyone have any idea about this one or have a better idea of how to do this?

Thanks for your help,
__________________
------------
Brandon
Reply With Quote
  #7  
Old 02-20-2008, 10:24 AM
Condor's Avatar
Condor Condor is offline
Ext JS - Community Support Team
 
Join Date: Mar 2007
Location: The Netherlands
Posts: 14,249
Condor is on a distinguished road
Default

Why not define a custom sortType, e.g.

function sortStatus(value){
  switch(value.toLowerCase()){
    case 'new':
      return 1;
    case 'broken':
      return 2;
    case 'destroyed':
      return 3;
  }
  return 4;
}

var store = new Ext.data.Store({
  fields: [
    {name: 'status', sortType: sortStatus},
    ...
  ],
  ...
});

store.sort('status', 'ASC');
Reply With Quote
  #8  
Old 02-20-2008, 11:14 AM
fallenrayne fallenrayne is offline
Ext User
 
Join Date: Nov 2007
Posts: 72
fallenrayne is on a distinguished road
Default

That would definately be the alternate solution I was looking for. Works like a charm. Thank you very much for showing me that one as that will come in real handy with a few of the grids I am building.
__________________
------------
Brandon
Reply With Quote
  #9  
Old 02-20-2008, 11:27 AM
fallenrayne fallenrayne is offline
Ext User
 
Join Date: Nov 2007
Posts: 72
fallenrayne is on a distinguished road
Default

Real quick for someone who has access to the API, it probably needs to be updated. Under Ext.data.Record.create when it talks about sortType it does not mention that it can be a custom sortType nor does Ext.data.SortTypes. Actually Ext.data.Record.create says for sortType that it is "A member of Ext.data.SortTypes". Granted, I should have looked at the code to figure it out but it would be nice to see that in the API.

Just a suggestion Thanks,
__________________
------------
Brandon
Reply With Quote
  #10  
Old 02-29-2008, 04:49 AM
jezmck's Avatar
jezmck jezmck is offline
Ext User
 
Join Date: Jan 2008
Location: UK
Posts: 128
jezmck is an unknown quantity at this point
Question

I hope this isn't seen as thread hijacking, my question is closely related.

In the app that I'm building I am using a custom renderer in a grid to show a column of company IDs as company names.
Unfortunately this means that the sort order in the grid is wrong.

I was hoping that I'd be able to write a simple comparison function like those used by PHP's usort function.

Is there something else that I'm missing?
Reply With Quote
Reply

Thread Tools

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

All times are GMT -5. The time now is 09:17 PM.

© 2006-2009 Ext, LLC
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.