2013-02-02 64 views
0

我想在jQuery ajax中添加一個新的ZF2'Zend \ Form \ Element。但是,當我使用Zend表格時,我不知道如何製作它。這是add.phtml文件。如何在jQuery中添加新的ZF2'Zend Form Element ajax

<script type="text/javascript"> 
$(document).ready(function(){ 
    $(".pid").change(function(){ 
    var id = $('.pid').val(); 
    var $_this = $(this); 
    $.ajax({ 
      type:"POST", 
      url:"<?php echo $this->url('Element/default',array('action'=>'change'));?>", 
      data:"pid="+id, 
      dataType:'json', 
      async:false, 
      success:function(data){ 
       if(data.response){ 
        //here I want to add a new select component after select conponent "pid" using like "$this->formRow($form->get('mid'))" or else . 
       } 
      } 
    }); 
    }); 
}); 
</script> 

以下是html的其餘部分。

<?php 
$title = 'add'; 
$this->headTitle($title); 
?> 
<h1><?php echo $this->escapeHtml($title); ?></h1> 

<?php 
$form = $this->form; 

$form->setAttribute('action', $this->url(
'Element/default', 
array(
    'action'  => 'add' 
) 
)); 
$form->prepare(); 

echo $this->form()->openTag($form); 
echo $this->formRow($form->get('pid')); 
echo $this->formRow($form->get('name')); 
echo $this->formRow($form->get('desc')); 
echo $this->formSubmit($form->get('submit')); 
echo $this->form()->closeTag(); 

如何在jquery ajax中添加新的zend表單元素?謝謝。

+0

對不起我很懶,但它內的所有解釋在這裏:http://www.michaelgallego.fr/blog/2012/07/ 04/new-zendform-features-explained/ – Sam

+0

我未能打開網站.. – Leo

+0

也許這將有助於:http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html #添加新元素 - 動態 – Andy0708

回答

0

你有腳本接收數據使用ajax和視圖/ HTML應該被這個腳本接收。您需要控制器/操作來呈現數據並作爲響應返回。

use Zend\View\Model\JsonModel;  
//some controller 
public function changeAction(){ // your requested action 
    //1. get partial helper to rendering html; 
    $partial = $this->getServiceLocator()->get('ViewHelperManager')->get('partial'); 
    $form = new Form();// your form 
    //2. render html 
    $html = $partial('path/to/your/file/phtml',['form'=>$form]); 
    //3. return data as JSON because in your ajax configuration dataType: 'json' 
    return new JsonModel([ 
     'html' => $html, 
    ]); 
} 
在你的js成功函數

應該是:

success:function(data){ 
    if(data.html){ 
     $_this.find('blockToAppend').append(data.html); 
    } 
} 
相關問題