删除嵌套 Appmaker 列表项的多对多关系(而不是项目本身)

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

我在 Cloud table 中有 3 个 table:人,标签,person_tag

在 Appmaker 中,我有一个表单小部件 (datasource:person) 和一个列表小部件 (datasources.person.relations.tag_id),显示绑定到 datasource.item.name 的文本。这很好用,只显示分配给所选人员的标签。

我在标签列表项上放置了一个删除按钮,这样我就可以从那个人的记录中删除标签。但是,我不知道如何设置 onClick 事件来删除关系(person_tag 记录)而不是标签本身(来自标签 table)。想法?

App Maker 将生成此代码以删除当前项目 :

widget.datasource.deleteItem();

但是正如问题中提到的,我们不需要删除该项目,而是打破两条记录之间的关系。为此,您可以修改项目数组,App Maker 会智能地同步您的更改:

// 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);

您可以在 Vendor Ratings template

中找到此模式

我是这样做的:给定人物和标签之间的 many-to-many 关系,select 来自两个表的当前 selected 项目。查找关系数组中第一项的索引,然后将其删除。

var person = widget.root.descendants.Table1.datasource.item;
var tag = widget.root.descendants.Table2.datasource.item;

personIndex = person.Tags.indexOf(person);
if (personIndex !== -1) person.Tags.splice(personIndex, 1);