Ruby on Rails HABTM 使用 AJAX 的多个下拉菜单

发布于 2024-09-29 17:09:43 字数 548 浏览 0 评论 0原文

我在成员(我们组织的实际成员,并非所有人都有在线帐户)和用户(在线帐户)之间具有典型的 HABTM 关系。我有一个供站点管理员使用的编辑用户页面,其中可以将多个成员分配给用户。

成员太多,无法合理使用复选框或多选下拉列表。我决定使用通过单击“添加成员”按钮添加的下拉列表,该按钮使用 AJAX 函数添加下拉列表。

这是我正在做的事情:

  • 我可以添加下拉菜单并选择任何成员。保存后,关系就建立了。

  • 我可以编辑该用户并在下拉列表中添加更多成员、删除成员和更改成员。

我正在努力解决的最后一个问题是让我的删除链接(每个成员下拉列表旁边)删除用户的下拉列表。原因是删除链接背后的操作依赖于包含下拉列表的 div 的 id。编辑用户时,该 ID 是根据所选成员生成的。对于新用户,我不知道下拉列表中选定的成员,因此我无法为其分配一个在单击删除链接时可以知道的 ID。

下拉菜单是正确的选择吗?有没有关于我所描述内容的好的教程或示例?我是否应该在下拉列表中的 onchange 事件中更新 div ID?

I have a typical HABTM relationship between members (actual members of our organization, not all have online accounts) and users (online accounts). I have an edit users page for site administrators where multiple members can be assigned to users.

There are too many members to reasonably use checkboxes or a multi-select dropdown. I have decided to use dropdowns that are added by clicking an "Add Member" button that uses an AJAX function to add a dropdown.

Here's what I have working:

  • I can add dropdowns and pick any member. On save, the relationships are established.

  • I can edit that user and add more members, remove members, and change members in the dropdown.

The final piece I am struggling with is having my remove link (next to each member drop down) remove a drop down for a new user. The reason being that the action behind the remove link relies on the id of the div that contains the dropdown. When editing a user, this id is generated based on the selected member. For a new user, I do not know the selected member in the dropdown, so I can't assign it an id that I can know about when the remove link is clicked.

Are dropdowns the way to go? Are there any good tutorials or examples of what I am describing out there? Should I perhaps update the div ID in an onchange event in the dropdown?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2024-10-06 17:09:43

我发现最好的方法是使用成员的 object_id 来创建唯一的 div id。然后我的 Javascript 函数就可以使用它来删除 div。

I have found that the best way to do this is to use the object_id of member to create a unique div id. My Javascript function is then able to use this to remove the div.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文