Using Ajax For Other Elements On Same Page As Cgridview

Was halfway through posting this when as usual, trying to describe the problem in detail pointed me to the solution. Given I spent all day bashing my head against a brick wall all day on this, I think it might prove helpful to someone else, so I’ll edit it and post it.

I’ve got a CGridView, with selectableRows = 2, for multiple selections.

Everything works fine for it - I can filter it using the built in filters, or an external dropdown. Pagination is fine. I’ve a button under the grid which picks up the selected rows using:


$idArray=jQuery($.fn.yiiGridView.getSelection('gridName'));

And that works fine.

The problem is, I’ve another completely separate div on the page which I update using a separate Ajax call, tied to a different control:


		

 			jQuery.ajax(

                {

	                "type":"POST",

	                "url":"/path/to/action",

	                "data": data,

	                "cache":false,

	                "success":function($html)

	                {

	                	($("#separate-div").html($html));

	                }

                 }

             ) 		

The action being called by this returns the data with:


$this->renderPartial('_partialViewname', array('data'=>$data), false, true);

As soon as I trigger that, the selection on the grid stops working. Everything looks ok, pagination and filtering are fine, it looks like the rows are being selected, but trying to access them with the getSelection method as above gives the error:


Uncaught TypeError: Cannot call method 'getSelection' of undefined 

Which is effectively saying $.fn.yiiGridView doesn’t exist in that scope (onClick function for a button beside the gridview), despite the fact that a dropdown elsewhere can still call $.fn.yiiGridView.update in its onChange function.

Eventually looked at the html that was being passed back from the renderPartial, and found it was including the scripts again, as the last parameter was true. Changing this to false meant it only passed back the basic html, which was find for what I wanted.


$this->renderPartial('_partialViewname', array('data'=>$data), false, false);

I assume that including the scripts a second time was causing a conflict somewhere. Not sure why it only happened for some controls, but I;m too tired at this stage to worry about it.

As an aside, while digging through the js code on a wild goose chase, I noticed that $.fn.yiiGridView.getSelection is defined in jquery.yiigridview.js under a section marked:


/******************************************************************************

 *** DEPRECATED METHODS

 *** used before Yii 1.1.9

 ******************************************************************************/

Had a look at the 1.1.9 changelog, but couldn’t see any reference to it. Is there some newer function that should be used instead?

Hi Mick, welcome to the forum.

Yeah, the 4th parameter of renderPartial … welcome back out of the ditch.

jquery.yiigridview.js has this …




	$.fn.yiiGridView.getSelection = function (id) {

		return $('#' + id).yiiGridView('getSelection');

	};



So,




$idArray=jQuery($.fn.yiiGridView.getSelection('gridName'));



could be written like the following:




$idArray=$('#gridName').yiiGridView('getSelection');



Looks much jQuerish …

But, I’ve got a lot of ‘DEPRICATED’ method calls in my source, too. They are working fine.

I’m pretty sure that they won’t be removed honoring BC … at least until Yii 2.0 ;D