javascript - 在 Symfony2 中嵌入多级表单集合

标签 javascript jquery symfony symfony-forms

我有以下情况:CVCFormType 是 BenefiItemsFormType 的集合。每个 BenefitItemFormType 都有一个字段,它是 BenefitGroupFormType 的集合。

我希望能够动态添加和删除元素。

我按照说明操作 here .当然,当我们谈论嵌套集合时,必须对它们进行调整。

在“固定”方面一切正常。到目前为止,在动态方面(添加和删除元素)我只实现了内部(添加 BenefitGroups)并且只用于添加字段。

这是我得到的(这是不对的)。我在顶部福利项目上有一个双链接(我应该只有一个),加上两组链接(第一个福利 项目和第二个)不是独立的(我点击上面的第二个,它向下面的那个添加了一个字段)。我想我必须动态更改 ul 类名。

有什么帮助吗?

截图如下:

enter image description here

代码如下:

{% extends "internal.html.twig" %}

{% block content %}

{{ form_start(form) }}
<br><b>CVC</b>
{% for benefititem in form.benefititems %}
<br><b>Benefit Item</b>
{{ form_row(benefititem.comment) }}

<br><b>Benefit Groups</b>
    {# <ul class="benefitgroups"> #}
    <ul class="benefitgroups" data-prototype="{{ form_widget(benefititem.benefitgroups.vars.prototype)|e }}">
        {% for benefitgroup in benefititem.benefitgroups %}
            <li>{{ form_row(benefitgroup.name) }}</li>
        {% endfor %}
    </ul>
{% endfor %}

{{ form_end(form) }}
{% block javascripts %}
<script>
var $collectionHolder;

// setup an "add a benefitgroup" link
var $addBenefitGroupLink = $('<a href="#" class="add_benefitgroup_link">Add a Group</a>');
var $newLinkLi = $('<li></li>').append($addBenefitGroupLink);

jQuery(document).ready(function() {
// Get the ul that holds the collection of benefit groups
$collectionHolder = $('ul.benefitgroups');

// add the "add a benefitgroup" anchor and li to the benefitgroups ul
$collectionHolder.append($newLinkLi);

// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);

$addBenefitGroupLink.on('click', function(e) {
    // prevent the link from creating a "#" on the URL
    e.preventDefault();

    // add a new tag form (see next code block)
    addBenefitGroupForm($collectionHolder, $newLinkLi);
});
});

function addBenefitGroupForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');

// get the new index
var index = $collectionHolder.data('index');

// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);

// increase the index with one for the next item
$collectionHolder.data('index', index + 1);

// Display the form in the page in an li, before the "Add a BenefitGroup" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}

</script>
{% endblock %}
{% endblock content %}

如果它可以帮助这里是生成的 HTML:

<html>
<head>
    <meta charset="UTF-8" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <title>Welcome!</title>
            <link rel="icon" type="image/x-icon" href="/favicon.ico" />
</head>
<body>
        <h1>Here you are inside</h1>

<form name="CVC" method="post" action="">
<br><b>CVC</b>
<br><b>Benefit Item</b>
<div>                <label for="CVC_benefititems_0_comment" class="required">Comment</label>    <input type="text" id="CVC_benefititems_0_comment" name="CVC[benefititems][0][comment]" required="required" maxlength="400" value="b1" /></div>

<br><b>Benefit Groups</b>
            <ul class="benefitgroups" data-prototype="&lt;div id=&quot;CVC_benefititems_0_benefitgroups___name__&quot;&gt;&lt;div&gt;                &lt;label for=&quot;CVC_benefititems_0_benefitgroups___name___name&quot; class=&quot;required&quot;&gt;Name&lt;/label&gt;    &lt;input type=&quot;text&quot; id=&quot;CVC_benefititems_0_benefitgroups___name___name&quot; name=&quot;CVC[benefititems][0][benefitgroups][__name__][name]&quot; required=&quot;required&quot; maxlength=&quot;100&quot; /&gt;&lt;/div&gt;&lt;/div&gt;">
                        <li><div>                <label for="CVC_benefititems_0_benefitgroups_0_name" class="required">Name</label>    <input type="text" id="CVC_benefititems_0_benefitgroups_0_name" name="CVC[benefititems][0][benefitgroups][0][name]" required="required" maxlength="100" value="c1b1" /></div></li>
                        <li><div>                <label for="CVC_benefititems_0_benefitgroups_1_name" class="required">Name</label>    <input type="text" id="CVC_benefititems_0_benefitgroups_1_name" name="CVC[benefititems][0][benefitgroups][3][name]" required="required" maxlength="100" value="c2b1" /></div></li>
                </ul>
<br><b>Benefit Item</b>
<div>                <label for="CVC_benefititems_1_comment" class="required">Comment</label>    <input type="text" id="CVC_benefititems_1_comment" name="CVC[benefititems][4][comment]" required="required" maxlength="400" value="b2" /></div>

<br><b>Benefit Groups</b>
            <ul class="benefitgroups" data-prototype="&lt;div id=&quot;CVC_benefititems_1_benefitgroups___name__&quot;&gt;&lt;div&gt;                &lt;label for=&quot;CVC_benefititems_1_benefitgroups___name___name&quot; class=&quot;required&quot;&gt;Name&lt;/label&gt;    &lt;input type=&quot;text&quot; id=&quot;CVC_benefititems_1_benefitgroups___name___name&quot; name=&quot;CVC[benefititems][5][benefitgroups][__name__][name]&quot; required=&quot;required&quot; maxlength=&quot;100&quot; /&gt;&lt;/div&gt;&lt;/div&gt;">
                        <li><div>                <label for="CVC_benefititems_1_benefitgroups_0_name" class="required">Name</label>    <input type="text" id="CVC_benefititems_1_benefitgroups_0_name" name="CVC[benefititems][6][benefitgroups][0][name]" required="required" maxlength="100" value="c2b2" /></div></li>
                </ul>

<div><button type="submit" id="CVC_submit" name="CVC[submit]">Do Something</button></div><input type="hidden" id="CVC__token" name="CVC[_token]" value="MEUAU3VawkCDJ5jTHo5hSTGrgrWS6XUm-UXeEI9onT8" /></form>

我希望用表格而不是列表来完成所有这些工作(因此在表格中添加和删除行)。

最终目标(添加一个附加层)如下: enter image description here

最佳答案

好吧,正如您所说,您的问题出在动态 端,也就是客户端。

我会发布关于您正在尝试做的事情的照片 here .

但在此之前,一个专业提示:永远不会在属性上打印 html,使用另一种技术,例如我正在使用的技术,模板(以替换您的 prototype属性)。有很多技术可以做到这一点,我只解释我的。

var $outerTemplate;
var $innerTemplate;
var $outerContainer;

jQuery(document).ready(function($) {
    $outerTemplate = $('#top-form-template');
    $innerTemplate = $('#inner-form-template');
    $outerContainer = $("#row-container");
    $("#addRow").on('click', function(e){
        addOuterForm();
    });

    $outerContainer.on('click', '.addItem', function (e) {
        addInnerForm(e.target.dataset.rowId);
    })

    $outerContainer.on('click', '.destroy-row', function (e) {
        destroyRow(e.target.dataset.rowId);
    });

    $outerContainer.on('click', '.destroy-item', function (e) {
        destroyItem(e.target.dataset.itemId);
    });
});

function addOuterForm () {
    var compiled = _.template($outerTemplate.html());
    var html = compiled({
        outerId: _.uniqueId(),
        innerId: _.uniqueId()
    });
    $outerContainer.append(html);
}

function addInnerForm (outerId) {
    var compiled = _.template($innerTemplate.html());

    var html = compiled({
        outerId: outerId,
        innerId: _.uniqueId()
    });

    $outerContainer.find('#row-'+outerId).find('.benefitgroups').append(html);
}

function destroyRow(id){
    $("#row-"+id).remove();
}
function destroyItem(id){
    $("#item-"+id).remove();
}

我所做的是创建 2 个模板,一个用于外部 表单(一个带有福利项目和组),另一个用于内部 表单(额外的福利项目).然后使用一些按钮附加和删除它们。我鼓励你看看客户端的模板引擎(我注意到你知道如何使用 Twig 模板引擎,也许 Handlebars 对你来说很容易理解)。

我正在使用 lodash 模板引擎,因为它非常简单,而且 lodash 工具非常强大和有用。

关于javascript - 在 Symfony2 中嵌入多级表单集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29277387/

相关文章:

JQuery 检查是否至少有一个输入已填充(非空且非零)

javascript - Bootstrap 的选项卡放置 - 如果选项卡元素之一具有下拉菜单,则它会在调整大小时中断

javascript - 在 Bootstrap Popover 中调用两次内容函数

JavaScript 倒计时器在 1 秒后卡住

javascript - 无法让 jquery click 事件在 ajax 生成的 HTML 上工作

c# - 用 <br/> 发布 json 会中断

jquery - 使用 jQuery 和 CSS3 在点击时翻译另一个 div

symfony - 在 Symfony 表单类型中设置复选框错误消息

php - 使用多个数据库连接访问存储库和实体

symfony - Doctrine OneToOne 删除实体