Yii::app()->clientScript->registerScript

Hi Guys

I have been search to understand this code




Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});


$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('invoice-grid', {

		data: $(this).serialize()

	});

	

	return false;

});


");



I need to know line by line the above code which is generated by gii but there is no good documentation.

Any idea?

Thanks

Hi. I don’t know whether there’s any official doc or wiki. But those are simply 2 blocks of jQuery code that:

  1. toggle (show/hide) the search form (typically in your admin.php) when you click on "Advanced Search"

  2. Submit the contents of your search form, and updates the grid below accordingly.

Cheers

Thank you. What are those return false are for?

Thank you

When I click search it also toggles the search fields. Any one knows why??

Thank you

also I dont get why we need

$.fn

before grid view

What if I want to update another filed? Let say we add another div and wants to update it, too.

Thank you

We are using jQuery to handle events and are in these two cases preventing natural behavior (and propagation) by adding return false.

You should try to remove them and see what happens. I think it’s better than my explanation anyway.

Edit:

Allow me to say the following: from your other questions, I can imagine that you don’t have a JavaScript/jQuery background. I think you have three options:

  1. Use blindly the JS codes that are embedded into Yii (client validation, ajax, UI widgets) and generated by gii.

  2. Learn basics of JavaScript/jQuery (not really hard especially if you have a programming background)

  3. Learn on-the-fly and to your own risk, by playing with code in your local environment. You can learn a lot this way.

My 2 cents.

You are absolutely right. I have to learn in a very very short time :(

let me ask a question

At this line. What is the ‘search’?

Yii::app()->clientScript->registerScript(‘search’,

I have an experience at jquery. but I don’t know what is “‘search’”.

Regards

Thu Ra

search - is name for the current script (i think yii send scripts to array and it’s a script key in array. It s only my propose), it can avoid including one script more than one time.

just follow this link to read about this feature:‘ID that uniquely identifies this piece of JavaScript code’

It is the unique script key to refer to the registered script. Correct me if I am wrong but Yii models itself from ASP.NET. And ASP.NET has a similar function to register javascript. Oh, and you can also specify where the script should appear as the 3rd parameter.