Javascript 如何在集合字段集设置属性(id)(条令2,zend framework 2)

Javascript 如何在集合字段集设置属性(id)(条令2,zend framework 2),javascript,collections,attributes,doctrine-orm,zend-framework2,Javascript,Collections,Attributes,Doctrine Orm,Zend Framework2,是否可以在集合生成的字段集上设置id?我有两个实体(用户、地址、附件等),在我的用户实体中,我有几个字段集,这些字段集由集合组成。因此,用户可以有多个地址或附件。我的收藏是这样建立起来的: $addressFieldset = new AddressFieldset($serviceManager); $this->add(array( 'type' => 'Zend\Form\Element\Collection', 'name' => 'addresses',

是否可以在集合生成的字段集上设置id?我有两个实体(用户、地址、附件等),在我的用户实体中,我有几个字段集,这些字段集由集合组成。因此,用户可以有多个地址或附件。我的收藏是这样建立起来的:

$addressFieldset = new AddressFieldset($serviceManager);

$this->add(array(
   'type' => 'Zend\Form\Element\Collection',
   'name' => 'addresses',
   'options' => array(
       'label' => 'Choose address for user',
       'count' => 1,
       'should_create_template' => true,
       //'template_placeholder' => '__placeholder__',
       'allow_add' => true,
       'target_element' => $addressFieldset
       ),
   ));

$this->add(array(
     'name' => 'addAddress',
     'type' => 'button',
     'options' => array('label' => 'Add Address',
            ),
      ));
$this->get('addAddress')->setAttribute('onclick', 'return add_address()');
我的问题是我的userfieldset中有多个集合。因此,当我想要动态添加一些地址(我使用的示例:)时,该示例具有以下javascript:

function add_address() {
    var currentCount = $('form > fieldset > fieldset').length;
    var template = $('form > fieldset > span').data('template');

    template = template.replace(/__index__/g, currentCount);

    $('form > fieldset').append(template);

    return false;
}
但是,我的问题是,如果我使用这个示例,它还会在附件下添加AddressFieldSet。我想要的是:

function add_address() {
    var currentCount = $('form > #addressFieldset > fieldset').length;
    var template = $('form > #addressFieldset > span').data('template');

    template = template.replace(/__index__/g, currentCount);

    $('form > #addressFieldset').append(template);

    return false;
}
这样,我只能访问addressFieldset,但如何在addressFieldset设置ID? 我的树应该是这样的:

<form>
    <fieldset id="addressFieldset">
        <legend>Choose addresses</legend>
        <fieldset>
            //Data
        </fieldset>
    </fieldset>
    <fieldset id="attachmentFieldset">
        <legend>Choose attachments</legend>
        <fieldset>
            //Data
        </fieldset>
    </fieldset>
</form>

选择地址
//资料
选择附件
//资料

我不知道如何在字段集中设置id。请帮助

只需将其添加到元素的
属性下即可:

$this->add(array(
    'attributes' => array(
        'id' => 'someId',
        'onclick' => 'return add_address()';
    ),
    // your other stuff, type, name, options, etc...
));

一种可能的方法是扩展
Zend\Form\View\Helper\FormCollection
,并将扩展注册为默认
FormCollection
帮助程序的替代程序

在本例中,我假设您的模块名为
Application

首先,需要定义一个扩展上述Zend helper的自定义类。对于我的扩展/重写,我通常保持与Zend相同的目录结构,只是我用模块名替换了“Zend”,因此,在我的例子中,类将驻留在
module/Application/src/Application/Form/View/Helper/FormCollection.php

完成后,您可以通过向
module/Application/config/module.config.php
文件添加以下行来替换帮助程序:

'view\u helpers'=>数组(
'invokables'=>数组(
'formCollection'=>'Application\Form\View\Helper\formCollection',
),
),
这告诉Zend,每当调用名为
formCollection
的视图帮助程序时,使用该类而不是默认类

接下来,您需要重写该方法。不幸的是,您必须将原始方法的内容复制/粘贴到您的类中(因为
字段集
标记的生成是硬编码的),但是如果您想要该功能,这是一个很小的代价

在返回结果之前,您需要修改将
$markup
变量包装在标记中的位

但在执行此操作之前,我们需要在factory数组中定义字段集的属性。我决定将我的字段集属性放入一个名为
fieldset\u attributes
的键中,如下所示:

$addressFieldset = new AddressFieldset($serviceManager);

$this->add(array(
   'type' => 'Zend\Form\Element\Collection',
   'name' => 'addresses',
   'options' => array(
       'label' => 'Choose address for user',
       'count' => 1,
       'should_create_template' => true,
       //'template_placeholder' => '__placeholder__',
       'allow_add' => true,
       'target_element' => $addressFieldset
       ),
   ));

$this->add(array(
     'name' => 'addAddress',
     'type' => 'button',
     'options' => array('label' => 'Add Address',
            ),
      ));
$this->get('addAddress')->setAttribute('onclick', 'return add_address()');
$this->添加(数组)(
'类型'=>'集合',
“名称”=>“地址”,
“选项”=>数组(
“标签”=>“为用户选择地址”,
“计数”=>1,
“允许添加”=>true,
“允许删除”=>true,
“创建新对象”=>true,
“是否应创建模板”=>true,
“目标元素”=>$addressFieldset,
'属性'=>数组(
'id'=>'addressFieldset',
),
),
));
当然,如果需要或重组数组,您可以对键进行不同的命名,这完全取决于您自己。只需确保更新我们将要相应修改的
render
方法即可

因此,最后要做的事情是在包装
$markup
时检查自定义属性,并在找到任何属性时附加它们。为此,我们将修改线条,使其如下所示:

$addressFieldset = new AddressFieldset($serviceManager);

$this->add(array(
   'type' => 'Zend\Form\Element\Collection',
   'name' => 'addresses',
   'options' => array(
       'label' => 'Choose address for user',
       'count' => 1,
       'should_create_template' => true,
       //'template_placeholder' => '__placeholder__',
       'allow_add' => true,
       'target_element' => $addressFieldset
       ),
   ));

$this->add(array(
     'name' => 'addAddress',
     'type' => 'button',
     'options' => array('label' => 'Add Address',
            ),
      ));
$this->get('addAddress')->setAttribute('onclick', 'return add_address()');
$attributeString='';
$options=$element->getOptions();
如果(!empty($options['attributes'])){
$attributeString='.$this->CreateAttributeString($options['attributes']);
}
$markup=sprintf(
“%s%s”,
$attributeString,
$label,
$markup
);
如果现在刷新页面,您将看到
已更改为


希望有帮助

不知道为什么ZF1的表单装饰器会被丢弃在ZF2中

在我看来,修饰表单元素最有效的方法是“否决”ZF2的表单视图助手。。。尽管我做得很野蛮

    <?php
    namespace Application\Helpers\View;

    use Zend\Form\ElementInterface;
    use Zend\Form\View\Helper\FormCollection as BaseFormCollection;

    class FormCollection extends BaseFormCollection
    {
        public function render(ElementInterface $element)
        {
            if($element->getAttribute('name') == 'usertype')
                return str_replace('<fieldset>', '<fieldset class="noborder">', parent::render($element));
            else
                return parent::render($element);
        }
    }

我已在视图中创建了id为的字段集。在zend中,无法在formcollection中设置id,因为viewhelper仅在字段集本身中设置图例。因此,字段集上没有属性。