【问题标题】:Render View Via Bootstrap Modal通过引导模式渲染视图
【发布时间】:2013-10-29 06:52:37
【问题描述】:

我需要使用 bootstrap modal 加载表单,如何通过链接调用带参数的 bootstrap modal?

查看:

<?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 将针对与数据库中的值相关的每一行发生变化。

【问题讨论】:

  • 您错过了链接上的“数据目标”属性

标签: php jquery twitter-bootstrap yii


【解决方案1】:

我不知道您是否已经找到解决方案,但我会这样做:

在您的链接中添加一个参数,如“data-productId”(例如:CHtml::link('Addaction','#myModal',array('id'=&gt;'link','data-toggle' =&gt; 'modal','data-productId'=&gt;$id, 'class' =&gt; 'link')))。

将 javascript 函数绑定到链接上的点击事件,如下所示:

$("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');
}); 

要使这个示例正常工作,您必须将表单从部分文件移动到对话框文件中。

如果这对您有帮助,或者您遇到其他问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多