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


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...

Be the first person to leave a comment

Please to leave your comment.