Search code examples
google-app-maker

Delete many-to-many relation of nested Appmaker list item (instead of the item itself)


I have 3 tables in Cloud SQL: person,tag,person_tag

In Appmaker, I have a form widget (datasource:person) and a list widget (datasources.person.relations.tag_id) displaying text bound to datasource.item.name. This works great, showing only the tags assigned to the person selected.

I've placed a delete button on the tag list item so I can remove the tag from that person's record. However, I can't figure out how to set the onClick event to delete the relationship (person_tag record) instead of the tag itself (from the tag table). Ideas?


Solution

  • App Maker will generate this code to delete current item :

    widget.datasource.deleteItem();

    But as it is mentioned in the question we don't need to delete the item, but break the relation between the two records. In order to do this you can modify array of items and App Maker will intelligently sync your changes:

    // Here widget is delete button and
    // widget.parent will be list's row (or grid's cell depending on the UI)
    // and by getting its position in the list/grid
    // we will get index of datasource's item (relation) we need to break
    var row = widget.parent;
    var index = row.childIndex;
    
    // remove one item at index, this will force
    // App Maker to break the relation
    app.datasources.Person.item.Tags.splice(index, 1);
    

    You can find this pattern in Vendor Ratings template