2016-12-09 7 views
1

를 형성하기 위해 나는 순간이있다젠드 프레임 워크 2 추가 제거 버튼 필드

$this->add(array(
    'type' => 'Zend\Form\Element\Collection', 
    'name' => 'attachments', 
    'options' => array(
     'count' => 1, 
     'should_create_template' => true, 
     'allow_add' => true, 
     'allow_remove' => true, 
     'target_element' => new AttachmentFieldset($this->entityManager) 
    ) 
)); 

내가 옆에 제거 버튼을 추가 할 각 양식 필드 그래서 나는 또한 첨부 파일을 제거 할 수있어. 어떻게해야합니까?

+0

안녕하세요! 귀하의 질문은 공식 문서 (https://framework.zend.com/manual/2.4/en/modules/zend.form.collections.html#adding-new-elements-dynamically)에서 적절한 답변을 제공합니다. 보지 못했거나 더 구체적인 질문이 있으시면 더 자세한 정보를 보내주십시오. –

답변

0

allow_add 또는 allow_remove을 지정하면 컬렉션에 모든 요소 수가 포함될 수 있습니다 (count으로 지정된 최소값).

컬렉션에 폼을 추가 한 후에는 클릭 할 때 템플릿을 기반으로 다른 요소를 추가하는 함수를 호출하는 버튼을 추가해야합니다.

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

<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> 

가하는 제거 버튼을 추가 템플릿에 단추를 추가하려면 위의 기능을 변경 및 제거 기능을 만들려면 :

을 수동에서

<script> 
    function add_category() { 
     var currentCount = $('form > fieldset > fieldset').length; 
     var template = $('form > fieldset > span').data('template'); 
     template = template.replace(/__index__/g, currentCount) 
          .replace('</fieldset>', '<button onclick="return remove_category(this)">Remove</button></fieldset>'); 
     $('form > fieldset').append(template); 

     return false; 
    } 
    function remove_category(el) { 
     $(el).parent().remove(); 
    } 
</script>