Search code examples
many-to-manygoogle-app-maker

How add / edit / remove many to many relationships in google app maker using tables?


I have the following scenario centered around the relationship between 2 models: Assets and Activities.

Many Assets can be INPUT of Many Activities at parallel times.

Having that in mind - I want to create UI, which allows me to:

  • from the perspective of Activity:

  • add Assets as related to the Activity into a table by selecting them from an existing table of Assets showing all created Assets.

  • Be able to remove the relationship by clicking a button (which would not delete the whole record in the table).

Please help. Thank you in advance!

I tried binding custom code to an onClick event to a button, which is on the same row as the Asset record I would like to un-relate from an associated activity:

var index = widget.root.datasource.item.Assets.indexOf(widget.datasource.item); widget.root.datasource.item.Assets.splice(index, 1);

This returns:

Cannot read property 'indexOf' of undefined at Strategy_TableView.Panel1.Table2.Table2Body.Table2Row.Button5.onClick:1:51

I also have yet to try to find a way to add existing Assets to the related to Activities table.


Solution

  • Suggested code edits to get this working correctly would be:

    var datasourcerelation = widget.root.datasource.relations.Services;
    var index = datasourcerelation.items.indexOf(widget.datasource.item);
    datasourcerelation.items.splice(index, 1);
    

    For whatever reason referencing datasource.item.Services in relation to JS array functions does not give you the result needed. I am not sure why this is, but you have to use datasource.relations.Services instead.

    In regards to adding to the relation use:

    var datasourcerelation = widget.root.datasource.item.Services;
    datasourcerelation.push(widget.datasource.item);
    

    Note that in the case of adding a relation datasource.item.Services works fine. I am not sure why this is different.