Zii.cgridview Loses Its Functionality After Changing Page

I have following JavaScript snippet code in above the View file that i used CGridView. Its functionality is only alert a message to user when user clicked on update CButtonColumn.


  

<script>

	$(document).ready(function(){

    	$('.update').click(function(){

        	alert('Will be ready!');

        	return false;

    	});

	})

</script>



For example if we have a table with 20 records and want display records in GridView and CActiveDataProvider->pageSize=10, two page will be create. now if we click on page 2 for fetching other records, the snippet JavaScript code don’t act. why?

Because the grid is replaced by the new grid containing records from the new page.

You’ll want to register your js code in the grid’s afterAjaxUpdate property: http://www.yiiframework.com/doc/api/1.1/CGridView#afterAjaxUpdate-detail

I usually use jQuery.on for that:


  

    	$('#div_that_holds_grid').on('click', '.update', function(){

        	alert('Will be ready!');

        	return false;

    	});



By doing this way, the event handler will survive safe outside of the grid even when the grid gets updated by ajax.