如果在yii中取消它,如何在控制器中放置确认警报并重定向到特定链接

时间:2015-05-16 09:34:19

标签: javascript php yii

如果点击取消按钮,我想在控制器中输入确认警报并重定向到特定链接。 我的admin.php代码是:

         array(
            'header' => 'Action',
            'class' => 'CButtonColumn',
            'template' => '{update}',
            'updateButtonUrl' => 'Yii::app()->createUrl("attendance/inout", array("id"=>$data->supplier_master_id))'
         )

这里我必须在控制器中设置确认警报,因为还有一些额外的条件。

我的控制器代码是:

public function actionInout($id) { 
?>
<script> 
if(confirm("Do you want to add record?")) {
  // means proceed
} else {         
  // Should be redirect back to the grid view
} 
</script>
<?php // further code

3 个答案:

答案 0 :(得分:1)

您可以点击按钮打开确认对话框,请将您的按钮代码更改为:

array(
    'header' => 'Action',
    'class' => 'CButtonColumn',
    'template' => '{update}',
    'buttons' => array(
        'update' => array(
            'url' => 'Yii::app()->createUrl("attendance/inout", array("id"=>$data->supplier_master_id))',
            'click' => 'js:function(){if(!confirm("Do you want to add record?")) {return false;}}'
        )
    )
)

并从控制器中删除脚本

答案 1 :(得分:1)

我自己找到答案

因为我必须在控制器中输入确认警报,我在控制器中做了:

   public function actionInout($id,$final_status) { ?>
   <script>
       if(!confirm("Do you want to check- <?php echo $final_status; ?> to well sites.")) {
            window.location.href = url; // back url
       } else {
            window.location.href = url1; // further process
       }
 </script>
 <?php // code

答案 2 :(得分:0)

你不能这样做。 控制器处理数据,但用户交互通过视图。 在这个意义上,正确的方法是在视图中实现相同的逻辑。通过这种逻辑,您将从控制器激活警报问题的出现。在你的答案之后,你将再次从控制器采取行动。我已经尝试通过列出行动来说明这一点:

1. Controller makes initial tasks and calls view
2. User enters what needed and finish with view
3. Controller analyze input data and initialize alert event for view - again calls view. Of course in call controller keeps and sends to view already filled data.
4. View understood that it called with alert action and shows the alert
5. After user action the view sends all data ( already containing and answer ) to the controller.
6. Controller understands alert and goes where needed.

我希望我能理解你的问题,我的答案会给你一个感觉。

当然它可以是一个解决方案,其中所有警报/答案逻辑都是通过javascript进入视图,但它非常依赖于具体案例。