How to query a dgrid on multiple columns
Asked Answered
D

1

2

Is it possible to create a dgrid query that will search for a regex string on more than one column? Usually the syntax specifies {column1: "foo", column2: "foo"} but that will look for rows where both column1 AND column2 are "foo". But I want to use the same search string against both columns... is that possible and how would I do it? I need something like {column1 || column2 : "foo"}.

Distiller answered 31/8, 2012 at 0:17 Comment(0)
T
5

You can write your own function as I mentioned in my answer to your previous question Is it possible to filter data in a dgrid like you can in a datagrid?

Or you can use Resource Query Language (RQL).

dgrid queries its store, e.g. dojo/store/Memory, which uses dojo/store/util/SimpleQueryEngine by default. All you need to do is to feed store.queryEngine property with QueryEngine of your choice. In the case of RQL, it looks like this (source):

require(["dojo/store/Memory", "rql/js-array", "dgrid/OnDemandGrid"], function(Memory, rql, Grid) {

    var store = new Memory({ data: { /* some data here */}});
    store.queryEngine = rql.query;

    var grid = new Grid({
        store: store,
        columns: { /* some columns here */}
    }, "grid")

});

Now you can query dgrid with RQL syntax. The query to answer your question: (column1=foo|column2=foo).

Please have a look at my jsFiddle (http://jsfiddle.net/phusick/VjJBT). Feel free to fork and experiment either with dgrid itself (there are some predefined queries) or with DOH unit tests (open JS console to see an output):

enter image description here

Tempe answered 1/9, 2012 at 10:40 Comment(2)
Thanks for your suggestions and help! I'm finding that my dgrid displays a subset of the total record set of 122 recs. This is before anything is typed into the filterString input. In my case I'm sticking with the default simple query engine rather than the rql engine. Initially on page load the scrollbar handle is tiny indicative of about 122 rec set. But as soon as I start to scroll it, it grows in length and the record set changes and ends up being about 45 recs. I have no query applied yet so I'm not sure what would account for this.Distiller
I'm guessing it's the OnDemandGrid doing a lazy load but what query is it applying and why? Other than this glitch my filter/query mechanism is working great over multiple columns. I basically applied what you had in that other post of mine (that you responded to) using the ~(item.Name + "").toLowerCase().indexOf(filterString.toLowerCase()) for each column name using "||" between each.Distiller

© 2022 - 2024 — McMap. All rights reserved.