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.