Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yiiGridView javascript event 'updated' #1313

Closed
vitalets opened this issue Aug 30, 2012 · 6 comments
Closed

yiiGridView javascript event 'updated' #1313

vitalets opened this issue Aug 30, 2012 · 6 comments
Assignees

Comments

@vitalets
Copy link

It would be nice if yiiGridView will trigger js event updated when it has been successfully updated via ajax.
Current afterAjaxUpdate is not enough in case of several different functions to be executed on grid update.
What do you think?

@lightglitch
Copy link
Contributor

It would be nice if all the javascript plugins had custom events ...

@vitalets
Copy link
Author

Agree. It seems to be not complicated, just add

$(this).trigger('update.yiiGridView');

@MonkeyMaster
Copy link

I also need these grid events: afterAjaxUpdate and selectionChanged callbacks are good but it's not possible to configure them on js side!
Another solution is to add $.yiiGridView('option') for getting/setting options at runtime

@vitalets
Copy link
Author

$.yiiGridView('option') is also good!
For triggering events I'm using now this code, may be helpfull for someone:

protected function attachAjaxUpdateEvent()
{
    $trigger = '$("#"+id).trigger("ajaxUpdate");';

    //check if trigger already inserted by another column
    if(strpos($this->grid->afterAjaxUpdate, $trigger) !== false) return;

        //inserting trigger
        if(strlen($this->grid->afterAjaxUpdate)) {
            $orig = $this->grid->afterAjaxUpdate;
            if(strpos($orig, 'js:')===0) $orig = substr($orig,3);
            $orig = "\n($orig).apply(this, arguments);";
        } else {
            $orig = '';
        }
        $this->grid->afterAjaxUpdate = "js: function(id, data) {
        $trigger $orig
    }";
}

@ghost ghost assigned mdomba Sep 26, 2012
@jackhad
Copy link

jackhad commented Jul 19, 2013

//Agree. It seems to be not complicated, just add

$(this).trigger('update.yiiGridView');//

where and how to add this code?can you give me detailed example please ?

@vitalets
Copy link
Author

@jackhad
currently I'm using more unified method:

    /**
    * injects ajaxUpdate event into widget
    *
    * @param mixed $widget
    */
    public static function attachAjaxUpdateEvent($widget)
    {
        $trigger = '$("#'.$widget->id.'").trigger("ajaxUpdate");';

        //check if trigger already inserted by another column
        if(strpos($widget->afterAjaxUpdate, $trigger) !== false) return;

        //inserting trigger
        if(strlen($widget->afterAjaxUpdate)) {
            $orig = $widget->afterAjaxUpdate;
            if(strpos($orig, 'js:')===0) $orig = substr($orig,3);
            $orig = "\n($orig).apply(this, arguments);";
        } else {
            $orig = '';
        }
        $widget->afterAjaxUpdate = "js: function(id, data) {
            $trigger $orig
        }";

        $widget->registerClientScript();
    } 

You can put it in controller and call statically in view, e.g.

$grid = $this->widget('CGridView', array(
'id' => 'mygrid',
...
));
$this->attachAjaxUpdateEvent($grid);
Yii::app()->getClientScript()->registerScript('ajax-update', '
$("#mygrid").on("ajaxUpdate", function() {
... //do whatever after ajax update
});
');

@samdark samdark closed this as completed Nov 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants