2015-10-07 45 views
0

我有问题。有没有一种方法可以根据确认消息提供其他操作? 例如,我有这样的:基于Yii确认的渲染动作

<?php $this->widget('booster.widgets.TbButton', array(
     'buttonType'=>'submit', 
     'context'=>'primary', 
         'htmlOptions' => array(
         'id'=> 'createstudent', 
         'confirm' => 'Do you want to register the student to a class' 
        ), 
     'label'=>$model->isNewRecord ? Yii::t('default', 'Create') : Yii::t('default', 'Save'), 
    )); ?> 

而我想做的是当用户点击在确认对话框是的,我想渲染来自其他控制器的另一个动作。例如contract/studentcreate, array($model->studentid)。如果用户点击否,它应该呈现假设呈现的动作。 谢谢你提前

回答

0

是的,这是可能的。 您可以使用jQuery来完成它。你必须捕捉按钮上的“点击”事件,当它发生时,你要求“确认”。如果用户在确认对话框中单击“是”,则对某个控制器/操作执行href,如果用户单击“否”,则对其他控制器/操作执行其他href。换句话说:

<script> 
    $("#createstudent").click(function() { 
    if(confirm('Do you want to register the student to a class?')){ 
     location.href = "BASEURL/controllerOne/actionOne"; 
    } 
    else{ 
     location.href = "BASEURL/controllerTwo/actionTwo"; 
    } 
    }); 
</script> 

凡BASEURL是在你在开发它的网络域名项目的基本URL(本地主机,如果您正在使用本地主机),controllerOne是你的情况下,请求控制器用户点击“是”,并且actionOne是当用户点击“是”时调用的控制器“controllerOne”的动作。对于控制者二者和行动二者同样。

编辑: 您正在使用一个提交按钮。它有自己的href动作(表单的动作)。因此,您可能需要使用“a”链接创建自己的按钮。例如:

<a id="createstudent" style="margin-left: 8px; margin-top:-10px; padding: 5px; background-color: #EA7A00; color: white; width: 62px; height: 25px; border: 1px solid #4B1800;cursor: pointer;">Example</a> 

一旦你点击它,它根据你选择的选项(是或否)href你正确的行动。

当然,您将不得不根据需要编辑CSS样式。

+0

弹出信息发生,但不管我点击了什么。它调用默认操作。它忽略了location.href命令 – thr

+0

也许是因为你正在使用提交按钮。它当然有它自己的href动作(这将是表单的动作)。 这意味着您需要创建自己的按钮作为“a”链接。例如,替换TbButton小部件: Example 一旦你点击它,它href你到正确的行动。 当然,您将不得不根据自己的愿望编辑CSS样式。 – Mundo

0

谢谢答案世界。我认为我通过这样做解决了这个问题:

<script> 
$("#createstudent").click(function(e){ 
    e.preventDefault(); 

bootbox.confirm("<?php echo Yii::t('default', 'Do you want to register this student?');?>", function(result) { 
    if(result){ 
     $('#student-form').submit(); 
     window.location = 'http://localhost/myproject/index.php/contract/studentcreate/'+'<?php echo $maxid ?>'; 

    } else { 
    console.log("Confirmed: "+result); 
     $('#student-form').submit(); 
    } 

}); 
}); 
</script> 

我也会试试你的解决方案!再次感谢您的帮助!

+0

请小心使用此解决方案。如果有人在点击注册按钮之前加载页面,您可能会导致竞争状况。最好让你的'studentcreate'函数得到一个新的'$ maxid',而不是让jquery在页面上有值。 – vortextangent