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

You are viewing revision #6 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 or see the changes made in this revision.

« previous (#5)next (#7) »

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

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

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

            'header' => 'Action',
            'class' => 'CButtonColumn',
            'template' => '{update}{delete}',
            'buttons' => array('delete' =>
                    '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...
                                                return false;
                                                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
			$flag = 0;

		} 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();
        } else {
            echo "false";

4) Finally you want to display the alert message on admin view file(admin.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'); ?>';

Hope it will be helpful...

1 0
Viewed: 64 574 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


View all history

Related Articles