Set Value For Textfield From Popup Window

Hi,

as a newbie to Yii I still have problems with the right syntax. I hope you can help me. I have two tables ‘auftraege’ and ‘kunden’ (these are german words for orders and customers). I made the models and CRUD with Gii. Table ‘auftraege’ has a column ‘kundenid’ which refers to table ‘kunden’. If an user wants to create an new record for table auftraege he needs to insert a value for kundenid. As the table kunden has more than 200.000 records, the user needs a complex search function to find the right customer and if he doesn’t find one he should be able to insert a new record to ‘kunden’.

In my create/update form for ‘auftraege’ I inserted an onclick event for the ‘kundenid’ field:




<?php $form=$this->beginWidget('CActiveForm', array('id'=>'auftraege-form',	'enableAjaxValidation'=>false,)); ?> 

<!-- other values here -->

<div class="kasten">

	<?php echo $form->labelEx($model,'kundenid'); ?>

	<?php echo $form->textField($model,'kundenid',array('onclick'=>'window.open("/kunden/auswahl/","Auswahl","resizable=1,titlebar=0,menubar=0,width=1200,height=700,scrollbars=1")')); ?>

	<?php echo $form->error($model,'kundenid'); ?>

</div> 

<!-- other values here -->

<div class="senden">

	<?php echo CHtml::submitButton($model->isNewRecord ? 'Anlegen' : 'Aktualisieren'); ?>

</div>

<?php $this->endWidget(); ?>



My action ‘/kunden/auswahl/’ is similar to the admin action created by Gii. Here the user can search for a customer. In this gridview I want to have only one button which sends the kundenid to the textfield of my auftraege-form. I tried something like this:







<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'kunden-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		// several fields here

		array(

			'class'=>'CButtonColumn',

            'template'=>'{take}',

            'buttons'=>array(

                        'take' => array(

                                    'label'=>'Take this customer',

                                    'imageUrl'=>Yii::app()->request->baseUrl.'/images/icons/take.png',

                                    'url'=>'"#"',

                                    'click'=>'js:function(){window.opener.document.Auftraege.kundenid.value=800; return false; }',

                                   ),

            ),             

		), 

	),

)); ?>



This was a first try still not sending the real kundenid, just a fixed value, to see if the function is working.

Now my questions:

My button-function doesn’t work (I also tried window.opener.document.getElementByID(“Auftraege_kundenid”).value=…). What is my mistake? I have to say, that I never used jQuery so I still don’t really anderstand this syntax.

How do I get the real kundenid to this javascript function if it is working?

If my user doesn’t find a customer and opens the “create” action inside my popup, how can I send the kundenid which will be new?

Is there overall a better way to make a search function for my field ‘kundenid’? I need to have this in one form, so it is no solution to split my form in different steps.

Any help would be highly appreciated.

Just for everyone who has the same problem. I could solve my linkproblem after understandig jQuery a litte bit more an after finding a post here in this forum where it was said that in ‘click’ you cannot user $data->id.

My code to set the value now looks like this:





array(

	'class'=>'CButtonColumn',

        'template'=>'{take}',

        'buttons'=>array(

                   'take' => array(

                           'label'=>'Diesen Kunden in den Auftrag übernehmen',

                           'imageUrl'=>Yii::app()->request->baseUrl.'/images/icons/messen.png',

                           'url'=>'"$data->id"',

                           'click'=>'js:function(){window.opener.$("#Auftraege_kundenid").val($(this).attr("href")); return false;}',

                           ),

            ),             

),




The trick was to define the id through ‘url’ because here you can use $data and to refer in ‘click’ through DOM to the href-Attribute. Of course this is a workaround, because you cannot user $data for ‘click’.