My fix is the following code in CButtonColumn. The previous handler is removed (if any) before adding the new one. This solution also avoids ('live') which is no longer recommended in jQuery [otherwise 'die' must be used in stead of 'off'].

There are different solutions to this... but adding off()/undelegate()/die() is not optimal at all... why remove a handler just so that you can add the same handler again.

A solution would be to not process the output when using renderpartial but that sometime is not possible as you need some script processed...

In the your code there is one more problem if you noticed... on every ajax call jquery.js, jquery-bbq.js and jquery.yiigridview.js are loaded again and again.

For this a solution would be to check if it's an ajax call and if it is then to prevent loading of those files with the scriptmap.

The best solution when using the grid is to use his update() method - $.fn.yiiGridView.update()
As you want to update some other parts of the page not only the grid, then you need to set the ajaxUpdate - http://www.yiiframew...axUpdate-detail

On the ajax call you just need to return all the containers that are needed to be replaced and all other will do the update method.

There are different solutions to this... but adding off()/undelegate()/die() is not optimal at all... why remove a handler just so that you can add the same handler again.

Either you avoid adding it twice, either you remove any existing one first and then always add the handler. The handler would only be removed when it is not the first time this code is executed (because there is none).
I just think that Yii would be more robust with a solution for this.
Also, undelegating is not new, this is in the 'jquery.yiigridview.js' file:

A solution would be to not process the output when using renderpartial but that sometime is not possible as you need some script processed...

Yep, in the real case there are also some jQuery UI elements that do not render correctly if there is no postprocessing.

mdomba, on 09 January 2012 - 09:47 AM, said:

In the your code there is one more problem if you noticed... on every ajax call jquery.js, jquery-bbq.js and jquery.yiigridview.js are loaded again and again.
For this a solution would be to check if it's an ajax call and if it is then to prevent loading of those files with the scriptmap.

Yes that is another issue, but I fixed that using an extension that does not load the duplicates. I did not overload the example with the fix for that.

mdomba, on 09 January 2012 - 09:47 AM, said:

The best solution when using the grid is to use his update() method - $.fn.yiiGridView.update()
As you want to update some other parts of the page not only the grid, then you need to set the ajaxUpdate - http://www.yiiframew...axUpdate-detail
On the ajax call you just need to return all the containers that are needed to be replaced and all other will do the update method.

As the grid is contained in another div that gets updated under certain circumstances the idea is to update the entire div at once rather the several parts.

Sorry for forgetting to add the 'id' in the second part of the sample data.

Regarding your fixes:
- as the grid is contained in another div, I prefer updating the entire div rather than cutting it in smaller pieces.
- calling yiiGridView.update with a parameter does not seem to update the default settings ' $.fn.yiiGridView.settings[id]; '. I do not know the possible side effects from that.
- I trigger an 'id_change' event so that the UI element responsible for the change does not have to know what needs to change (which is handled in the listeners). I changed tje relevant part in my example code to this:

As I wrote already there is no point in undelegating just to again delegate the same equal method... why do the repetitive work... what is delegate then for?

The undelegate you see in the gridview.js file was a quick fix in previous versions... that is gone in the yii 1.1.9 version as now the new on() method is used.

If to update several parts or just one div... it's up to the developer... you just need to set the ajaxUpdate with appropriate ID(s)... I don't see any problem with any of those... updating several (two in your case) divs makes sense if they are on different parts on the page... you don't get any much faster code if you update just one div instead of two...

I did not understant the problem about updating settings with the parameter... but maybe you mean the gridID... I noticed in your code that you are changing the grid ID on ajax calls.... that is not good... a DOM element should always keep his ID, especialy if some events are binded or delegated to it.

This initialisation is stored in a yiiGridView.settings array and referenced in several locations. The 'url' may need to change as the 'id' changes.

mdomba, on 09 January 2012 - 03:14 PM, said:

but maybe you mean the gridID... I noticed in your code that you are changing the grid ID on ajax calls.... that is not good... a DOM element should always keep his ID, especialy if some events are binded or delegated to it.

The DOM Element (the grid) is in a div that sees its content replaced entirely. The purpose of having a specific id is to allow multiple grids on the same page.

When I asked what is delegate for I was thinking in pure jQuery terms... in jQuery delegating means binding a custom code to an event even for future updates of the DOM elelement... that was introduced so that a developer can delegate his code only one time for all the future changes of the grid... by undelegating and again delegating you are breaking the main idea of that method.... we can talk abou this on and on... but it leads nowhere.. and there are solutions for this without doing it this way.

If you take any DOM element and change its ID... that always the same DOM element not a new one.
In the above example you don't have multiple grids... you have one grid to whom you want to change the ID - this are not multiple grids...

In the end.. I made a working solution... it works without the need to change the grid ID... the pagination works too... so it's up to you now if you want to build upon that solution or not..

I agree that when you change the id of a DOM element it is still the same DOM element. But when you replace the content of a '<div>' the DOM elements that were in the '<div>' no longer exist do they? So any event attached directly to those get lost. As the events created with 'on' are attached to the body these continue to exist even after the replacement.

So maybe attaching the 'on' events to the uppermost level of the grid may be a generic solution. When the grid disappears, the events should disappear too.
Attaching the events related to the grid to the grid itself rather than the body, that should be ok - maybe that is what is been done for 1.1.9.

I'm having an issue with this and using 1.1.9... if an ajax call returns all the code necessary for a GridView, and you click whatever button does this ajax call multiple times, you get a bunch of popup confirmations when you click the default delete button because of this code getting sent multiple times:

I do not remember if I wrote the alternative code or if I got it from somewhere.
It does some accounting regarding 'on' events that are already registered, switches them off if they exist and then recreate the event handler.
The 'else' part is never executed (it is the original code).

I know that when I do not have this code, I have the same issues as you have.