Search code examples
google-app-maker

Filter items of relation on query


Given scenario below:

Models:

Projects
Tasks

and

Relationship:

Projects (one) <--> (many) Tasks

I have an accordion with the datasource that only shows Projects with uncompleted tasks. (e.g., query.filters.Tasks.Completed._equals = false)

In the accordion detail I have a list where I only want to show tasks that meet a certain criteria (eg. Task.Category = "Marketing" OR "Sales").

enter image description here

Is this possible, and if so, how would this be done?


Solution

    1. Create a dedicated datasource for filtered Tasks
    2. Set it to manual load mode
    3. Drop a table inside Accordion's details prototype
    4. Bind the table to the datasource from the step #1
    5. Add this snippet to the onDataLoad event of the Accordion's details prototype
    // Accordion details onDataLoad event handler
    var ds = app.datasources.TasksFitered;
    var projectKey = widget.datasource.item._key;
    
    ds.query.filters.Category._in = ['Marketings', 'Sales'];
    ds.query.filters.Project._key._equals = projectKey;
    ds.load();
    

    Most likely you'll need to unload the datasource on onDetach event of the details, to hide old results when user switches between projects:

    // Accordion details onDetach event handler
    app.datasources.TasksFitered.unload();