Search code examples
javascriptyiicbuttoncolumncbutton

Prompt for confirmation before executing cutsom CbuttonColumn action


I have a custom delete button, all i want is some sort of confirmation before delete action takes place.. I have tried multiple ways of doing so with no success so far.

here is my code, I am using CArrayDataProvider thus had to create a template for delete button.

 array(
       'class' => 'CButtonColumn',
       'template' => '{delete}{reset}',
       'deleteConfirmation'=>"js:'Are You Sure?'",
       'afterDelete'=>'function(link,success,data){ if(success) alert("Delete completed successfully"); }',
       'buttons' => array(
           'delete' => array(
               'label'=> 'Remove this device',
               'imageUrl'=> Yii::app()->request->baseUrl.'/img/delete.png',
               'url' => 'Yii::app()->controller->createUrl("controller/action", array("trace_id"=>$data["trace_id"], "mac"=>$data["mac"]))',
               'click'=><<<EOD

                      function(){
                      confirm('Are you sure?')
                      }EOD
                ),

Solution

  •             'status' => array(
                    'label'=>"<i class='fa fa-eye-slash'></i>",     // text label of the button
                    'url'=>function ($data)
                    {
                        return $this->createUrl("counters/changeStatus",array('id'=>$data->counter_id, "status"=>$data->status ? 0 : 1  ));
                    },       // a PHP expression for generating the URL of the button
                    'imageUrl'=>false,  // image URL of the button. If not set or fa lse, a text link is used
                    'options'=>array(
                        'class'=>'btn roundPoint4 btn-xs green btn-warning statusBtn',
                        'title'=>"Activate/Deactivate",
                    ), // HTML options for the button tag
                    'click'=>'function(e){
                                                      e.preventDefault();
    
                                //open confirmation box write ur code here
    
    
                                           }',     // a JS function to be invoked when the button is clicked
                    'visible'=>function ()
                    {
                        return true;
                    },   // a PHP expression for determining whether the button is visible
                ),
    

    NOW I SHOW YOU WHAT I DO IN MY CODE FOR THE THING YOU WANT TO DO

                'status' => array(
                    'label'=>"<i class='fa fa-eye-slash'></i>",     // text label of the button
                    'url'=>function ($data)
                    {
                        return $this->createUrl("counters/changeStatus",array('id'=>$data->counter_id, "status"=>$data->status ? 0 : 1  ));
                    },       // a PHP expression for generating the URL of the button
                    'imageUrl'=>false,  // image URL of the button. If not set or fa lse, a text link is used
                    'options'=>array(
                        'class'=>'btn roundPoint4 btn-xs green btn-warning statusBtn',
                        'title'=>"Activate/Deactivate",
                    ), // HTML options for the button tag
                    'click'=>'function(e){
                                                      e.preventDefault();
    
                                                      $(this).parents("table").find("tr.workingRowClass").removeClass("workingRowClass");
    
                                                      $("#secretForm").html("");
                                                      var parts =  getMyIdNew($(this).attr("href"), "/status/", "/id/") ;
                                                      setAction($("#secretForm"), "POST",  parts[2], 1)
                                                      moslakeFormInput( $("#secretForm") , "Counters[id]", "hidden", parts[1] , "id");
                                                      moslakeFormInput( $("#secretForm") , "Counters[status]", "hidden", parts[0], "status");
                                                      moslakeFormInput( $("#secretForm") , "operation", "hidden", "statusChange", "operation");
    
                                                      $("#promptAlert").find(".modal-body").html("<p>Are you sure you want to change status of the this Item ?</p>");
                                                      $("#promptAlert").modal("show"); $(this).parents("table").find("tr").removeClass("deleteMode");
                                                      $(this).parents("tr").addClass("workingRowClass");
    
    
                                           }',     // a JS function to be invoked when the button is clicked
                    'visible'=>function ()
                    {
                        return true;
                    },   // a PHP expression for determining whether the button is visible
                ),
    

    I have copy paste code. so it will be extra. When some one clicks on "Status" button. It will open bootstrap modal. and ask for a confirmation. While executing this i have created a form with action and some fields. and in that modal i have proceed button. on proceed button click the form will be submitted. and on close the form will be made empty. the form will be display none form. and will have all the fields hidden.. I know it is more complex then urs... but I do it with post... BUT YOU CAN ASSIGN YOUR HREF TO POST BUTTON LINK IN THIS FUNCTION AND ON CLICK ON THAT IT WILL BE REDIRECTED