Changing Cgridview.enablehistory / Html5 History Object Behaviour

When you set CGridView.enableHistory to true, Yii uses HTML5 History object (registers "jquery.history.js" file) and changes browser URL each time user filters or paginates / sorts CGridView.

I would like to change this behaviour – in particular, I would like to strip/filter off all the model’s / filter’s fields, that are having no real value (null or empty string).

So for example, URL (generated by Yii):


http://127.0.0.1/ustv/lookup/manage/index.html?Lookup[type]=Category&Lookup[code]=&Lookup[position]=

would become:


http://127.0.0.1/ustv/lookup/manage/index.html?Lookup[type]=Category

(removed empty parameters)

By overriding CController.beginRequest() I managed to purge these empty parameters out of $_GET. But how can I prevent Yii from actually adding them to browser’s address bar, when CGridView initiates AJAX request (to filter / sort CGridView or change page)?

Can this be done at application / framework level or do I have to directly change "jquery.history.js" code?

Hi Trekder

Did you try overriding the createUrl method of UrlManager ?

Create a class UrlManager and extends the CUrlManager adn then use it

you could manipulate every you want about url parameters in this method

Hi, thanks for the tip.

Yes, I tried using custom UrlManager:


class UrlManager extends CUrlManager

{

	public $showScriptName = false;

	public $appendParams = false;

 

	public function createUrl($route, $params = array(), $ampersand = '&')

	{  

		$params = array_filter($params, function($var){return isset($var) && $var !== '';});


		return parent::createUrl($route, $params, $ampersand);

	}

}

to remove all parameters that are null or empty strings, but it doesn’t work. I.e. my URL in browser isn’t affected and stil contains parameters with empty values.

I don’t know, if createUrl method of CUrlManager is affected by mentioned behavior? I’m more to think, that these extra additions added to URL, when you’re using HTML5 History object, are done purely on client side, with jquery.history.js plugin and does not go through Yii app life cycle.

Anyway, I tried to echo $params array and among many dumps I got on screen, I found no trace of these empty paramters added to URL. So this would support my theory, that this part of the game goes through client side and does not touches Yii.

BTW: I only want to filter empty strings or nulls, but to leave those parameters, who has integer value = 0, that is why I used callback function. Using array_filter() without custom callback function does also filter "zeros", which I want to avoid.

Disable javascript on your browser. what happens? if it still appear the empty variables then we have to check Yii method and behaviours. In any other case you have to modify the javascript or add extra javascript to remove the empty variables again…

Trejder, Thanks for your vote :)

I have a rich-content, totally Javascript-depended application. If I disable JavaScript, I won’t be able to click a single button! :]

But thanks for the idea. I’ll further investigate options, you provided.

I said that only for testing,

after of that you have to focus on a part of the problem.

Let inform me if I could help :)