javascriptdatefilterdojodstore

dstore filter date range


I have a data array which has a date attribute, I need to filter the data based on date.

I am using dstorejs to store the data as below

this.employeeStore = new Memory({data:[emplist],idProperty:"emp_num"});

I need to make a filter based on employee's joining date , like who joined from 1st of Jan 2014 till 3rd of March 2015

this.employeeStore.filter({joinDate:'01/01/2014'});

This gives all employees who joined on 1st of Jan 2014 but if I need to filter in a range like

this.employeeStore.filter.gte({joinDate:'01/01/2014'});

This will not work because it is not a number, it is a string or date object

To achieve this do I need to write the custom store as they mentioned in tutorial?

Is there any other way of doing this?

Or is there any other framework like dstore to achieve this ?


Solution

  • You can achieve this just by using dojo/store Query filter , exactly query with callback function to manage comparing date

    and also using the dojo/date compare function to compare date of your emplist array to a specific date like below :

    this.employeeStore = new Memory({data: someData});
    
    var compareDate = new Date(2014,1,1);
    
    var results = store.query(function(object){
        // compare return -1 if lower 
        // return 0 if equals
        // return 1 if greater
        return date.compare(object.date, compareDate) > -1;
    });
    
    results.forEach(function(yourResultData){
        // some stuff looping results
    });
    

    you can see a Fiddle to have a better understanding .