Search code examples
extjsmodelstoreextjs4.2

ExtJS 4.2.3: Get the next and previous record from a store, based on which record is currently selected


I have a store containing a few records. And I want two buttons, one for up one record (unless first record) and one for down one record (unless last record).

I hoped that .next() and .prev() would work as they're part of the AbstractElement, but they didn't. I basically just need to select the next or the previous records based on what record I'm currently on.

This is the code I have so far (for upping the priority only, the other one would be very similar):

Controller

stores: [
    'Ratings'
],
...
refs: [
    {
        ref: 'RatingCombo',
        selector: '#comboRating'
    }
],
...
onPriorityUpClick: function(button, e, eOpts) {
    var me = this,
        current_rating = me.getRatingCombo(),
        s = me.getRatingsStore(),
        r = s.findRecord('id', current_rating.getValue()),
        first_r = s.first();

    // If top rating, do nothing
    if(r !== first_r) {
        var prev_r = r.prev();
        // Set new rating
        ...
    }
},
...

As you can see I already have the "is first" part under control. Just need a way to get the previous record. If anyone has any ideas, it would be greatly appreciated.


Solution

  • IIRC Sencha itself used the following code to get the next/previous record:

    idx = store.indexOf(record),
    nextRecord = store.getAt(idx+1),
    prevRecord = store.getAt(idx-1)