This is a migrated thread and some comments may be shown as answers.

Everlive Filtering Query with Optional Multiple Conditions

2 Answers 48 Views
JavaScript SDK
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
David Weinberg
Top achievements
Rank 2
David Weinberg asked on 26 Dec 2017, 07:59 AM

Hi,

Sorry if this has been asked before, I did try searching first but didn't find anything.

I am creating a filter for a list screen. The contains a number of fields, currently 7, that the user may or may not assign a value too. For each filter field given a value, the data should be filtered to only show the matching records.

The app is NativeScript Angular . I tried the code below but only one where() is respected. I need to chain more than one together as an AND. I see in the docs the syntax for this is to do filter.where().eq("Field1", "Value1").eq("Field2", "Value2")... but how do I best do this in the case where I need Field 1, 3 and 5 without creating very verbose code to cover every combination of the 7.

getFilter () {
    let filter = this._provider.newQuery;
    if (this._priorityFilter) {
        filter.where().eq("Priority", this._priorityFilter);
    }
    if (this._jobTypeFilter) {
        filter.where().eq("JobType", this._jobTypeFilter);
    }
    if (this._statusFilter) {
        filter.where().eq("Status", this._statusFilter);
    }
    if (this._referredToFilter) {
        filter.where().eq("ReferredTo", this._referredToFilter);
    }
    return filter;
}

 

Thanks in advance,

David

2 Answers, 1 is accepted

Sort by
0
Accepted
Martin
Telerik team
answered on 28 Dec 2017, 09:40 AM

Hi David,

When constructing the Everlive query, once you end the method chaining (by adding a semicolon ";") you would not be able to add additional clauses to the same where().and().eq() filter so it is normal that only one where().eq() clause is respected.

When executing the request, the Everlive Query would construct a JSON object and add it as a filter to the request. In your use case when you want to add multiple filters based on conditions, it would be better to construct this JSON object filter yourself and pass it to the request as a filter. The code would look like this:

var el = new Everlive('your-app-id');
var data = el.data('collection-name-here');
 
var filter = {};
 
if (this._priorityFilter) {
    filter.Priority = this._priorityFilter;
}
if (this._jobTypeFilter) {
    filter.JobType = this._jobTypeFilter;
}
if (this._statusFilter) {
    filter.Status = this._statusFilter;
}
if (this._referredToFilter) {
    filter.ReferredTo = this._referredToFilter;
}
 
data.get(filter)
    .then(data => console.log(data))
    .catch(error => console.log(data));

The created filter is a JSON object like this (all key-value pairs are looked as if they were in an AND clause):

{
    "Priority": "_priorityFilter-value",
    "JobType": "_jobTypeFilter-value",
    "Status": "_statusFilter-value",
    "ReferredTo": "_referredToFilter-value"
}

If you would like to create a more sophisticated filter (using or() and different than eq() expressions), please refer to the documentation here. For instance, the same results as the above could also be assembled like this:
var el = new Everlive('your-app-id');
var data = el.data('collection-name-here');
 
var filter = { "$and": [] };
 
if (this._priorityFilter) {
    filter["$and"].push({ "Priority": { "$eq": this._priorityFilter } });
}
if (this._jobTypeFilter) {
    filter["$and"].push({ "JobType": { "$eq": this._jobTypeFilter } });
}
if (this._statusFilter) {
    filter["$and"].push({ "Status": { "$eq": this._statusFilter } });
}
if (this._referredToFilter) {
    filter["$and"].push({ "ReferredTo": { "$eq": this._referredToFilter } });
}
 
data.get(filter)
    .then(data => console.log(data))
    .catch(error => console.log(data));

The created filter would look like this:

{
    "$and": [
        { "Priority": { "$eq": "_priorityFilter-value" } },
        { "JobType": { "$eq": "_jobTypeFilter-value" } },
        { "Status": { "$eq": "_statusFilter-value" } },
        { "ReferredTo": { "$eq": "_referredToFilter-value" } }
    ]
}

Let me know if this has helped.

Regards,
Martin
Progress Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
David Weinberg
Top achievements
Rank 2
answered on 28 Dec 2017, 11:00 AM

Hi Martin,

Thanks, that is working perfectly. 

David

Tags
JavaScript SDK
Asked by
David Weinberg
Top achievements
Rank 2
Answers by
Martin
Telerik team
David Weinberg
Top achievements
Rank 2
Share this question
or