2013-10-29 100 views
1

我需要使用引导模式来加载窗体,我怎么能通过链接调用引导模态参数?通过引导模式渲染视图

查看:

<?php echo CHtml::link(Yii::t('app','addaction'),'#myModal',array('class'=>'btn btn-primary','data-toggle'=>'modal')) ;?> 
<br/><br/><br/> 

<!-- Bootstrap modal dialog --> 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
       <h4 class="modal-title"><?php echo Yii::t('app','AddAction'); ?></h4> 
      </div> 
      <div class="modal-body"> 
       <?php echo $this->renderPartial('_form', array('model'=>$model,'productId'=>$productId)); // I need $productId to by dynamic related to link 
       ?>    
      </div> 

     </div><!-- /.modal-content --> 
    </div><!-- /.modal-dialog --> 
</div><!-- /.modal --> 

控制器:

public function actionCreate() 
    { 
    $model=new Actions; 
    $productId=intval($_GET['productId']); 

    // Uncomment the following line if AJAX validation is needed 
    $this->performAjaxValidation($model); 

    if(isset($_POST['Actions'])) 
    { 
     $model->attributes=$_POST['Actions']; 

     $model->product_id=$productId; 
     if($model->validate()){ 
      $model->save(false); 
      $message=Email::setJavaMessage('success',Yii::t('app','sm'),Yii::t('app','actionWasAdded')); 
      echo CJSON::encode(array('status' => 'success','message'=>$message)); 
      Yii::app()->end(); 
     }else{ 
      $error = CActiveForm::validate($model); 
      if($error!='[]') 
       echo $error; 
      Yii::app()->end(); 
     } 
    } 
    if(Yii::app()->request->getIsAjaxRequest()) 
     echo $this->renderPartial('_form',array('model'=>$model,'productId'=>$productId),false,true);//This will bring out the view along with its script. 

    else 
     $this->render('create',array(
       'model'=>$model,'productId'=>$productId)); 
} 
上面代码的形式

所以是验证还,如果我需要添加动态参数链接如何将问题的工作? 例如:通过更新函数在CRGidview中使用它,那么对于与数据库中的值相关的每一行,$ product_id都会更改。

+1

您错过了链接上的'data-target'属性 – tinybyte

回答

0

我不知道你是否已经找到了解决方案,但这里是我会怎么做:

在你的链接添加参数,如“数据的productId”(例如:CHtml::link('Addaction','#myModal',array('id'=>'link','data-toggle' => 'modal','data-productId'=>$id, 'class' => 'link')))。

绑定一个JavaScript函数来click事件你的链接是这样的:

$("a#link").click(function() 
{ 
    var productId = $(this).data('productId'); 

    //you can change the value of a form component like this 
    //in the below example I have a hidden input field whose val I want to change to be submitted with the form 
    $("input#FormId_idHiddenInput").val(productId); 

    $('#myModal').modal('show'); 
}); 

在这个例子中工作,你将不得不从部分移到你的表格到您的对话框的文件。

让我知道这是否有助于您,或者是否遇到其他问题。