duandou1903 2012-11-23 12:53
浏览 52
已采纳

Symfony2表单:在每个元素附近添加删除图标

I would like to display in my form a list of elements from the entity. Near each element, it should be a delete button which will delete the specific element from the entty.

Do you know how I could do this?

Example: 3 entities: Person, Affiliation and PersonAffiliations. A person has 0 or many affiliations. Lets say I created 10 affiliations for the person and now I would like to delete one of them. How to create the form in order to display a list of all the affiliations for the specific person and near each affiliation display a small delete icon. When click on this icon, the affiliation should be deleted for this person.

  • 写回答

1条回答 默认 最新

  • douren1928 2012-11-23 13:05
    关注

    You have to embed a collection of forms and use a lot of Javascript. It is in the Symfony2 Cookbook!

    Follow the example with the tags provided in the link above! Its easy, you have to put one anchor with the "remove" or "x" string per each sub-form to be removed and handle the deletion with Javascript.

    Form insertion is done by using the Prototype of the subform that you need to add, but seems you don't need insertion.

    Notice that in your example you have to create an empty Person object and to add to it the list of predefined Affiliation object(s) such that the FormBuilder will be capable to render the forms related to Affiliation. Then you can delete the sub-forms with Javascript. Of course, it is in general better to define dedicated form types.


    If you only need to deselect a list of entities that are in relations with the main entity, then you can use the entity field type and you can render is as a list of checkboxes with the expanded option. Your controller will bind the entity and the related relationships and so it Symfony will cancel the precedeing relationships.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分