How to apply or change the confirmation message on admingridview button and call a ajax..Dynamic

You are viewing revision #5 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#6) »

Hi Friends,

if category is assigned to any product and can not delete the category.... or if you want to change the status on dynamic this article is helpful...

In this example liquor_category is assigned to liquor table so if i want to delete the any liquor_category then display the alert message (it's already assigned to liquor) so you can't delete the liquor Category and create the custom message

<?php 
 $alert = 'Are you sure you want to delete this Category?';
?>

1) First apply the ajax on admin grid-view...

array(
            'header' => 'Action',
            'class' => 'CButtonColumn',
            'template' => '{update}{delete}',
            'buttons' => array('delete' =>
                array(
                    'url' => 'Yii::app()->controller->createUrl("delete",array("id"=>$data->primaryKey))',
                    'label' => 'delete',
                    'options' => array(// this is the 'html' array but we specify the 'ajax' element
                        'confirm' => $alert,
                        'class' => 'grid_action_set1',
                        'ajax' => array(
                            'type' => 'POST',
                            'url' => "js:$(this).attr('href')", // ajax post will use 'url' specified above
                            'success' => 'function(data){
                                if(data == "true"){
                                              //update the grid...
                                                $.fn.yiiGridView.update("liquor-category-grid");
                                                return false;
                                }else{
                                               
                                                window.location="admin?del=exist";
                                                return false;
                                }
                            }',
                        ),
                    ),
                ),
                'update' => array(
                    'options' => array('class' => 'grid_action_set'),
                ),
            ),
           
            'htmlOptions' => array('width' => '8%')
        ),

2) Change the delete function on Controller..

public function actionDelete($id)
	{

		if (Yii::app()->getRequest()->getIsPostRequest()) {
                        //fetch the liquor_category_id on liquor table
			$data=Liquor::model()->findAll('liquor_category_id='.$id);
			$flag = 0;
			UtilityHtml::AdminDelete($flag,$data,$id,$model='LiquorCategory');

		} else
		throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

	}

3) Create the common-function file and put the function on AdminDelete

//in my artical i will create the** UtilityHtml.php** (/protocted/comoponets/UtilityHtml.php file)
public function AdminDelete($flag, $data, $id, $model) {
        if (isset($data) && count($data) > 0) {
            $flag = 1;
        }
        if ($flag == 0) {
            echo "true";
            $this->loadModel($id, $model)->delete();
            Yii::app()->end();
        } else {
            echo "false";
            Yii::app()->end();
        }
    }

4) Finally you want to display the alert message on admin view file(admin.php)

<?php
$delete = '';
if (isset($_GET['del']) && !empty($_GET['del'])) {
    $delete = $_GET['del'];
}

?>
<script type="text/javascript">

    $(document).ready(function() {
        var del = '<?php echo $delete; ?>';
        if (del == 'exist')
        {
            alert('Category Can not be deleted as there is already liquor is assigned for particular liquor category.');
            window.location.href = '<?php echo CController::createUrl('admin'); ?>';
        }
    });
</script>

Hope it will be helpful...

1 0
6 followers
Viewed: 64 573 times
Version: Unknown (update)
Category: Tips
Written by: Ankit Modi
Last updated by: jim.
Created on: Aug 9, 2013
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles