1. #1
    Ext User
    Join Date
    Nov 2007
    Posts
    72
    Vote Rating
    0
    fallenrayne is on a distinguished road

      0  

    Default Setting a store SortKey

    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:
    Code:
    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

  2. #2
    Ext User
    Join Date
    Jul 2007
    Posts
    3,128
    Vote Rating
    0
    devnull has a little shameless behaviour in the past

      0  

    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...

  3. #3
    Ext User
    Join Date
    Nov 2007
    Posts
    72
    Vote Rating
    0
    fallenrayne is on a distinguished road

      0  

    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 :
    Code:
    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

  4. #4
    Ext User
    Join Date
    Jul 2007
    Posts
    3,128
    Vote Rating
    0
    devnull has a little shameless behaviour in the past

      0  

    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).

  5. #5
    Ext User
    Join Date
    Nov 2007
    Posts
    72
    Vote Rating
    0
    fallenrayne is on a distinguished road

      0  

    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

  6. #6
    Ext User
    Join Date
    Nov 2007
    Posts
    72
    Vote Rating
    0
    fallenrayne is on a distinguished road

      0  

    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:
    Code:
    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

  7. #7
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Why not define a custom sortType, e.g.

    Code:
    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');

  8. #8
    Ext User
    Join Date
    Nov 2007
    Posts
    72
    Vote Rating
    0
    fallenrayne is on a distinguished road

      0  

    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

  9. #9
    Ext User
    Join Date
    Nov 2007
    Posts
    72
    Vote Rating
    0
    fallenrayne is on a distinguished road

      0  

    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

  10. #10
    Ext User jezmck's Avatar
    Join Date
    Jan 2008
    Location
    UK
    Posts
    128
    Vote Rating
    0
    jezmck is an unknown quantity at this point

      0  

    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?