Yiic Grid view always giving live is not a functio

2019-07-19 07:49发布

问题:

I know live has been deprecated. Now I am using Yii grid view and i have following code:

   $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'category-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'title',
        array(
            'class' => 'CButtonColumn',
            'viewButtonUrl' => 'Yii::app()->createUrl("/shop/category/view",
            array("id" => $data->category_id))',
            'updateButtonUrl' => 'Yii::app()->createUrl("/shop/category/update",
            array("id" => $data->category_id))',
            'deleteButtonUrl' => 'Yii::app()->createUrl("/shop/category/delete",
            array("id" => $data->category_id))',
        ),
    ),
));

On the browser it gives me error:

typeError: jQuery(...).live is not a function
[Break On This Error]   

jQuery('#category-grid a.delete').live('click',function() {

I am confused as i have searched the entire folder for .live function but i cant find it. Please help!

回答1:

I think this is because of the version of Yii. It may be using .live() which is deprecated.

One Possible solution can be you go to yii/framework/zii/widgets/grid and edit CButtonColumn.php

In this replace .live() by .on()

I think then it should work.



回答2:

live function of jquery is depreciated ... if u are using different version of jquery than the yii's built in ... show documentation of jquery for live() method ... use .on() or .delegate() method instead ... i'm not sure ... but it can be a one solution if u are using other jquery version than the buit in...

from this page Jquery live() documentation As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

so it will not work forjquery version >= 1.7



回答3:

You can re-write the default javascript action by doing this

        //I a'm commenting your delete url config
        //'deleteButtonUrl' => 'Yii::app()->createUrl("/shop/category/delete",array("id" => $data->id))',
        'buttons' => array(
            'delete' => array(
                'url'=>'Yii::app()->createUrl("/shop/category/delete",array("id" => $data->id))',
                'click' => 'js:function(evt)'
                . '{'
                . 'evt.preventDefault();'
                . 'alert("Your functionality Will Come here. Or Call a function");'
                . '}'
            ),
        )