Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgrid dstore collection - “or” filter not working as expected ? #1265

Closed
storecat opened this issue Mar 31, 2016 · 4 comments
Closed

dgrid dstore collection - “or” filter not working as expected ? #1265

storecat opened this issue Mar 31, 2016 · 4 comments

Comments

@storecat
Copy link

I'm working with Dojo dgrid and I have problem with dstore filtering with using "or" statement.

var today = new Date();
today.setHours(0,0,0,0);

var start_date= new Date();
start_date.setDate(today.getDate() - 5);
start_date.setHours(0,0,0,0); 

grid.set('collection', store.filter(
    new store.Filter().or(
            new store.Filter().gte('datefield1', start_date).lte('datefield1',today),
            new store.Filter().gte('datefield2', start_date).lte('datefield2',today)
    )
)); 

Both conditions works perfect when they are separatelly, but when I put them together into "or" filter it works like "and" statement: both have to be "true" to return values.

How to make "or" statement to work properly in dojo dgrid dstore?

@kfranqueiro
Copy link
Contributor

I can't reproduce this issue. Here is an example that shows or working both for numbers and dates:

require(['dstore/Memory'], function (Memory) {
    var formatter = new Intl.DateTimeFormat();
    var data = [];
    for (var i = 1; i <= 10; i++) {
        data.push({
            id: i,
            date1: new Date(Date.now() + (Math.random() - 0.5) * 864000000),
            date2: new Date(Date.now() + (Math.random() - 0.5) * 864000000)
        });
    }
    var store = new Memory({
        data: data
    });

    function logIDs(results) {
        console.log(results.map(function(item) {
            return item.id;
        }));
    }

    function logDates(results) {
        console.log(results.map(function(item) {
            return formatter.format(item.date1) + '~' + formatter.format(item.date2);
        }));
    }

    logDates(data);

    logIDs(store.filter(new store.Filter().or(
        new store.Filter().gt('id', 7),
        new store.Filter().lt('id', 4)
    )).fetchSync());

    logDates(store.filter(new store.Filter().or(
        new store.Filter().lte('date1', new Date()),
        new store.Filter().lte('date2', new Date())
    )).fetchSync());
});

And here it is in a fiddle.

Either way, if this were an issue, it should probably be logged with dstore, not dgrid. If you can provide more information on your problem, please create an issue there. Thanks!

@kfranqueiro
Copy link
Contributor

Sorry, I just came back and realized I wasn't quite testing the same case you were describing.

Indeed, the following seems to not work, even with simple numbers:

store.filter(new store.Filter().or(
    new store.Filter().gt('id', 7).lt('id', 10),
    new store.Filter().lt('id', 4).gt('id', 1)
));

So or works with single filters, and the above works with a single argument under the or (which would generally be pointless). I'm not sure yet whether this is incorrect usage or a bug. I'm going to move this to a dstore issue.

@kfranqueiro
Copy link
Contributor

I've logged SitePen/dstore#179 for this. In the interim, if you're using a Memory store, you can provide a custom filter function to filter to do what you want, e.g.:

store.filter(function (item) {
    return (item.datefield1 >= start_date && item.datefield1 <= today) ||
        (item.datefield2 >= start_date && item.datefield2 <= today);
});

@storecat
Copy link
Author

storecat commented Apr 3, 2016

Thank you! It worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants