如何在yii中管理动态div

时间:2014-02-12 05:46:43

标签: php yii

在我的视图页面中有一个div来获取人员的详细信息。

查看页面div是:

<div class="text-group">  
            <ol>  
                <li>  
                    <div class="form-group">   
<?php echo $form->labelEx($adscontact, 'contact_name'); ?>  
<?php  echo $form->textField($adscontact, 'contact_name', array('class' => 'form-control','multiple' => true)); ?>  
                        <div class="errormsg"><?php echo $form->error($adscontact, 'contact_name'); ?></div>   
                    </div>   
                    <div class="form-group">   
<?php echo $form->labelEx($adscontact, 'designation'); ?>  
<?php echo $form->textField($adscontact, 'designation', array('class' => 'form-control','multiple' => true)); ?>   
                        <div class="errormsg"><?php echo $form->error($adscontact, 'designation'); ?></div>   
                    </div>   
                    <div class="form-group">  
<?php echo $form->labelEx($adscontact, 'email'); ?>  
<?php echo $form->textField($adscontact, 'email', array('class' => 'form-control','multiple' => true)); ?>  
                        <div class="errormsg"><?php echo $form->error($adscontact, 'email'); ?></div>   
                    </div>  
                </li>  
            </ol>  
        </div>

及以下有一个按钮ADD用于添加多个联系人详细信息当我们ckick添加上面的div时将再次出现

添加按钮

<div class="form-group">
                           <button id="btn2" class="btn-darkorange btn" type="button">add</button>
                          </div>`

javascript功能为

   $("#btn2").click(function(){  
    $("ol").append('<li><div class="form-group">   
<label for="InputContactPerson">Contact Person</label>'+
                                       '  
 <input type="fax" class="form-control" id="InputContactPerson" >'+
                                     ' </div><div class="form-group"> <label for="InputDesignation">Designation </label>'+
                                       ' <input type="fax" class="form-control" id="InputDesignation" > </div> <div class="form-group">'+
                                      '  <label for="Email">Email</label> <input type="fax" class="form-control" id="Email" >'+
                                      '</div></li>');   
  });

如何读取控制器中的所有输入?

1 个答案:

答案 0 :(得分:2)

你应该使用renderPartial函数而不是通过javascript添加内容....首先在控制器中创建另一个动作,它将对表单进行处理...就像在那个actiuon启动你的模型并在你的视图文件夹中创建一个视图对于那个表单...你要动态加载的数据......现在使用

//this can be action in your controolller
function createForm(){ 
$adscontact=new adscontact();

if(isset($_POST['adscontact']))
{
//validation and saving 
}

$this->renderPartial('_view',array('adscontact'=>$adscontact),false,true)
//here i added false as third and true as 4 param ..to use validation.
}

此代码将帮助您从主视图文件中调用该表单。

//it will be in ur main view file
//ajax to call the controller
$('#btn2').click(function(){

  $.ajax({
  type:"GET",
  url:"<?php echo $this->createUrl('controllerName/createForm'); ?>",
  dataType : "html",
  success : function(response){

     $('.text-group').html(response);

  } 
  });

});

并且_view文件将具有

<div class="text-group">  
        <ol>  
            <li>  
                <div class="form-group">   
<?php echo $form->labelEx($adscontact, 'contact_name'); ?>  
<?php  echo $form->textField($adscontact, 'contact_name', array('class' => 'form-  control','multiple' => true)); ?>  
                    <div class="errormsg"><?php echo $form->error($adscontact, 'contact_name'); ?></div>   
                </div>   
                <div class="form-group">   
<?php echo $form->labelEx($adscontact, 'designation'); ?>  
<?php echo $form->textField($adscontact, 'designation', array('class' => 'form-control','multiple' => true)); ?>   
                    <div class="errormsg"><?php echo $form->error($adscontact, 'designation'); ?></div>   
                </div>   
                <div class="form-group">  
<?php echo $form->labelEx($adscontact, 'email'); ?>  
<?php echo $form->textField($adscontact, 'email', array('class' => 'form-control','multiple' => true)); ?>  
                    <div class="errormsg"><?php echo $form->error($adscontact, 'email'); ?></div>   
                </div>  
            </li>  
        </ol>  
    </div>

这是做这件事的最好方法......每次你想要创建加载......