簡體   English   中英

如何在控制器中放置確認警報並在yii中將其取消重定向到垂直鏈接

[英]how to put confirm alert in controller and redirect to perticular link if cancel it in 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

單擊按鈕后可以打開確認對話框,請將按鈕代碼更改為:

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

並從控制器中刪除腳本

我自己找到答案

因為我必須在控制器中放置確認警報,所以我在控制器中這樣做:

   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

您不能以這種方式執行此操作。 控制器處理數據,但是用戶交互通過視圖進行。 因此,正確的方法是在您的視圖中實現相同的邏輯。 通過這種邏輯,您將從控制器激活警報問題的出現。 之后,沿着答案,您將再次從控制器執行操作。 我試圖通過列出動作來說明這一點:

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制作到視圖中,但是它非常依賴於具體情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM