【问题标题】:Allow_Remove doesn't remove the collectionAllow_Remove 不会删除集合
【发布时间】:2014-02-27 07:52:58
【问题描述】:

我在表单中设置了一个字段集(集合),我希望允许添加和删除元素。所以,我的代码是:

    $hydrator = new Hydrator($this->getObjectManager(), 'Base\Entity\MyElements');

        $fieldset = new MyElements();

        $fieldset->setObjectManager($this->getObjectManager())
                 ->setHydrator($hydrator)
                 ->setObject(new \Base\Entity\MyElements())
                 ->init();       

 $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'myElements',
            'options' => array(
                'label' => 'My Elements',
                'count' => 1,
                 'should_create_template' => true,
                 'allow_add' => true,
                 'allow_remove' => true,
                'target_element' => $fieldset
            )
        ));

我可以添加元素,但是没有出现删除按钮。我做错了还是忘记了什么?

PS:我的英语很差,但我正在努力提高它。对不起。谢谢

【问题讨论】:

    标签: php collections zend-framework2


    【解决方案1】:

    allow_remove 选项不会直接添加按钮。请记住,allow_add 也不会添加按钮。正如您在the docs 中看到的,您必须添加按钮

    <button onclick="return add_category()">Add a new category</button>
    

    以及添加元素的js函数:

    <script>
        function add_category() {
            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;
        }
    </script>
    

    正是这样,你必须添加删除按钮

    <button onclick="return remove_category()">Remove</button>
    

    和功能:

    <script>
        function remove_category() {
           //write your logic to remove the last, or the current element, for isntance:
    
             $('form > fieldset > fieldset').last().remove();
            return false;
        }
    </script>
    

    【讨论】:

    • 非常感谢!我现在意识到这不是自动的!在我的巨大项目中,有人实现了 allow_add 方法,但是 allow_remove 还没有完成!谢谢你!
    • 是的!很多人都有 add 功能,因为它在 zf2 文档中可用,但另一个没有! :) 请接受答案,以便对其他用户有用!
    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多