无法使用 Yii bootstrap 小部件 TbGridView 添加删除确认 ajax 弹出窗口

问题描述 投票:0回答:1

我正在尝试使用ajax添加删除确认弹出窗口,标准的Yii GridView弹出窗口;

我使用 Yii Bootstrap 链接

尝试了很多方法,似乎都不起作用;

我也必须在控制器中编写代码吗?

'deleteConfirmation' => 'are you sure?',

    $this->widget('bootstrap.widgets.TbGridView', array(
        'id' => 'url-m-grid',
        'type' => array('striped', 'bordered', 'condensed'),
        'dataProvider' => $model->searchDomainUrls($id),
        'template' => '{pager}{items}{pager}',
//        'filter' => $model,
        'pager' => array(
            'header' => '',
            'hiddenPageCssClass' => 'disabled',
            'maxButtonCount' => 5,
            'cssFile' => false,
            'prevPageLabel' => '<i class="icon-chevron-left"></i>',
            'nextPageLabel' => '<i class="icon-chevron-right"></i>',
            'firstPageLabel' => 'First',
            'lastPageLabel' => 'Last',
            'htmlOptions' => array('class' => 'pagination'),
        ),
        'pagerCssClass' => 'pagination',
        'columns' => array(
            array(
                'type' => 'raw',
                'header' => 'Url',
                'value' => 'CHtml::link($data->location,array("/user/url/view","url_id"=>$data->id))',
            ),
            array(
                'header' => 'Status',
                'value' => 'Url::$active[$data->active]',
            ),
            array(
                'class' => 'CButtonColumn',
                'template' => '{view}{update}{delete}',
                'buttons' => array(
                    'view' => array(
                        'label' => 'View',
                        'url' => 'Yii::app()->createUrl("/user/url/view", array("url_id"=>$data->id))',
                    ),
                    'update' => array(
                        'label' => 'Update',
                        'url' => 'Yii::app()->createUrl("/user/url/update", array("url_id"=>$data->id))',
                    ),
                    'delete' => array(
                        'label' => 'Delete',
                        'url' => 'Yii::app()->createUrl("/user/url/delete", array("url_id"=>$data->id))',
                        'options'=>array(
                            'ajax'=>array(
                                'type'=>'POST',
                                'url'=>"js:$(this).attr('href')",
                                'success'=>'function(data) { console.log("evrika"); }'
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ));
php twitter-bootstrap yii
1个回答
0
投票

错误出在删除按钮类名处;

不是

delete
,而是
delete_item

<?php

class MyCButtonColumn extends CButtonColumn {

      //public $template = '{domains}';


/**
     * Initializes the default buttons (view, update and delete).
     */
    protected function initDefaultButtons()
    {
        if($this->viewButtonLabel===null)
            $this->viewButtonLabel=Yii::t('zii','View');
        if($this->updateButtonLabel===null)
            $this->updateButtonLabel=Yii::t('zii','Update');
        if($this->deleteButtonLabel===null)
            $this->deleteButtonLabel=Yii::t('zii','Delete');
        if($this->viewButtonImageUrl===null)
            //$this->viewButtonImageUrl=$this->grid->baseScriptUrl.'/view.png';
            $this->viewButtonImageUrl='glyphicon glyphicon-search';
        if($this->updateButtonImageUrl===null)
            //$this->updateButtonImageUrl=$this->grid->baseScriptUrl.'/update.png';
            $this->updateButtonImageUrl='glyphicon glyphicon-pencil';
        if($this->deleteButtonImageUrl===null)
            //$this->deleteButtonImageUrl=$this->grid->baseScriptUrl.'/delete.png';
            $this->deleteButtonImageUrl='glyphicon glyphicon-remove';
        if($this->deleteConfirmation===null)
            $this->deleteConfirmation=Yii::t('zii','Are you sure you want to delete this item?');

        foreach(array('view','update','delete') as $id)
        {
            $button=array(
                'label'=>$this->{$id.'ButtonLabel'},
                'url'=>$this->{$id.'ButtonUrl'},
                'imageUrl'=>$this->{$id.'ButtonImageUrl'},
                'options'=>$this->{$id.'ButtonOptions'},
            );
            if(isset($this->buttons[$id]))
                $this->buttons[$id]=array_merge($button,$this->buttons[$id]);
            else
                $this->buttons[$id]=$button;
        }

        if(!isset($this->buttons['delete']['click']))
        {
            if(is_string($this->deleteConfirmation))
                $confirmation="if(!confirm(".CJavaScript::encode($this->deleteConfirmation).")) return false;";
            else
                $confirmation='';

            if(Yii::app()->request->enableCsrfValidation)
            {
                $csrfTokenName = Yii::app()->request->csrfTokenName;
                $csrfToken = Yii::app()->request->csrfToken;
                $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
            }
            else
                $csrf = '';

            if($this->afterDelete===null)
                $this->afterDelete='function(){}';

            $this->buttons['delete']['click']=<<<EOD
function() {
    $confirmation
    var th = this,
        afterDelete = $this->afterDelete;
    jQuery('#{$this->grid->id}').yiiGridView('update', {
        type: 'POST',
        url: jQuery(this).attr('href'),$csrf
        success: function(data) {
            jQuery('#{$this->grid->id}').yiiGridView('update');
            afterDelete(th, true, data);
        },
        error: function(XHR) {
            return afterDelete(th, false, XHR);
        }
    });
    return false;
}
EOD;
        } //print_r($this->buttons);die();
    }

    /**
     * Renders a link button.
     * @param string $id the ID of the button
     * @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
     * See {@link buttons} for more details.
     * @param integer $row the row number (zero-based)
     * @param mixed $data the data object associated with the row
     */
    protected function renderButton($id,$button,$row,$data)
    {
        if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
            return;
        $label=isset($button['label']) ? $button['label'] : $id;
        $url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
        $options=isset($button['options']) ? $button['options'] : array();
        if(!isset($options['title']))
            $options['title']=$label;
        //if(isset($button['imageUrl']) && is_string($button['imageUrl']))
        //  echo CHtml::link(CHtml::image($button['imageUrl'],$label),$url,$options);
        if(isset($button['imageUrl']) && is_string($button['imageUrl']))
            echo '<a class="'.$options['class'].'" href="'.$url.'" title="'.$label.'"><i class="'.$button['imageUrl'].'"></i></a>'."\n";
        else
            echo CHtml::link($label,$url,$options);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.