Results 1 to 5 of 5

Thread: Filtering a range of dates?

  1. #1
    Sencha Premium Member
    Join Date
    Sep 2013
    Posts
    99

    Default Answered: Filtering a range of dates?

    Hello, I have store with several records that contain a "date" field, so right now if I want to filter a record for a specific date, I'm using:

    Code:
    store.filter('date', '2010-11-22');
    but what if I want to filter all records between the 22nd of November and the 27th for example? How can I do this? if someone could provide an example or specific steps I'd really appreciate it, thank you.

  2. Use the filterBy method:

    Code:
    var min = someDate,
        max = someOtherDate;
    
    store.filterBy(function(rec) {
        var d = rec.get('date');
        return d > min && d < max;
    });

  3. #2
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,239
    Answers
    757

    Default

    Use the filterBy method:

    Code:
    var min = someDate,
        max = someOtherDate;
    
    store.filterBy(function(rec) {
        var d = rec.get('date');
        return d > min && d < max;
    });
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  4. #3
    Sencha Premium Member
    Join Date
    Sep 2013
    Posts
    99

    Default

    Works great, thank you very much. Could you quickly explain how this works please? I mean I understand what goes on in the function, I just don't understand how/why the record gets passed into the function, or in other words, what exactly passes the 'rec' argument into the function? Is this done by the 'filterby' method? I'm sorry if I can't explain myself properly, at any rate I thank you for the solution.

  5. #4
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,239
    Answers
    757

    Default

    You pass us a matcher function, then we call your matcher function with each record.

    The filterBy implementation does (roughly):

    Code:
    filterBy: function(fn, scope) {
        var matched = [];
    
        // allRecs...
        for (i = 0, len = allRecs.length; i < len; ++i) {
            if (fn.call(scope || this, allRecs[i]) === true) {
                matched.push(allRecs[i);
            }
        }
        // Set our records to matched
    }
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  6. #5
    Sencha Premium Member
    Join Date
    Sep 2013
    Posts
    99

    Default

    I see, I thought it was something along those lines, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •