I have a dropdown that I want to populate when an item in another dropdown is selected. Both the dropdown are tied to data/model passed on from controller. and the first dropdown is populated from DB by calling a function in the model. Heres' the form,
echo $form->dropDownListRow($modelunit,
'superunit',
$model->getSunits(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('user/getunits'),
'update'=>'#unit_id',
)));
echo CHtml::dropDownList('unit_id','', array());
Here's the action user/getunits called by Ajax.
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['superunit']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
I keep getting an error "Undefined index: superunit" when first dropdown is selected. Also, you may notice I am using form->dropDownListRow for the first dropdown while using CHtml::dropDownList for the second. That's cause I am clueless on the syntax of how exactly to make sure the dropdown is populated correctly with ajax and at also properly bind to the model.
You use $form->dropDownListRow that's why you will get $_POST['MyModelName']['superunit'] on your server side
Change you code like
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['MyModelName']['superunit']));
Where MyModelName is a model that you use)
Or like
echo CHtml::dropDownList('superunit'.....
For others - this wiki may help.
Related
Iam having
<?= $form->field($model, 'parentid')
->dropDownList(ArrayHelper::map(User::find()->asArray()->all(), 'id', 'firstName'),
['prompt'=>'-Select a Parent-'])
?>
When i submit the form,iam getting parentid value is empty.
I have disable the dropdown by jquery
$("#register-parentid").attr("readonly", "readonly");
Im getting empty value and i have referred, as said , we cannot get the value from disable field. Then how can i get the values from disable.
Iam using same form for two functions. One for disable field and another one for non disable .
I tried lot. but i didn't get the solutions for this one.
Finally, I have playing with jquery.
When i submit the form, i have disabling the readonly field
$('button[type=submit]').on('click', function (event, attribute) {
$("#register-parentid").prop("disabled", false);
});
I merge create and admin views and method in my controller,
so in admin.php I have following lines:
echo $this->renderPartial('_form', array('model'=>$create));
echo $this->renderPartial('_admin', array('model'=>$search));
and in _form I edit submit button to
echo CHtml::ajaxSubmitButton('Submit', Yii::app()->createUrl('money/income'), array('update' => '#money-grid'));
and in money controller,income method implement create method and send CGridView as ajax response.
if ($createModel->save()) {
$this->renderPartial('_admin', array('model' => $searchModel), false, true);
if data saved into database,it render _admin view which contain CGridView,this works prefect ,but problem exist, if client create something and insert into database, so after it click on delete of CGridView it will trigger more than one and it makes problem,
for example if user insert n rows ,then click on the each row to delete,it will trigger n + 1 times.(but it should be 1)
where is the problem?is it for Yii?
The problem solved by updating CGridView by jQuery instead of rendering it,so I changed my ajaxSubmitButton to
echo CHtml::ajaxSubmitButton('Submit', Yii::app()->createUrl('money/income'), array('success'=>'$.fn.yiiGridView.update("money-grid")'));
and it solved problem.
'success'=>'$.fn.yiiGridView.update("money-grid")'
i got a problem while updating a record for a model in yii. i have 2 models. im working on one model now. I'm creating a record for one model using create controller. in the form i've 1 fields which is the name field(im getting this name from other model(table). im getting the names from this second table and showing them in dropdown list. and storing them.
when it comes to update its again coming up with the same dropdown what i've shown using the _form.php for creating a record. can anyone pls tell me how can i show the name instead of dropdown list again??
thanks in advance.
Add a simple check in your view to see if the value has already been added or not. Something like this:
<?php if ($model->attribute && $model->attribute != ''): ?>
// Code to display a normal textfield here
<?php else: ?>
// Code to display dropdown
<?php endif; ?>
I have a dynamic dropdown, data coming from the database.
<?php $sel_id = $selected_id_array[0]->UPR_RelationType;?>
My dropdowm looks like this
<?php echo CHtml::dropDownList('RelationType_'.$pat_id[0]->PAT_ID,'U2U_RelationType',CHtml::listData(MasterTypeItems::model()->findAllByAttributes(array('MSTT_MST_ID'=>$relationship_type_array[0]->MST_ID),array('order' => 'MSTT_Name')), 'MSTT_ID', 'MSTT_Name'),array('id'=>'select','class'=>'relation_type','style'=>'width:50px'));
In this dropdown i have to select defaultly $sel_id;
for example am getting $sel_id=5; In the drop down i have to select 5th option as selected in yii. please give me any suggestion what i have to write in dropDown to select $sel_id;
If I understand your question correctly, what you want to achieve is a dropdownlist which already has a pre-selected option. If so, then this piece of code should work.
echo dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
where $select would be your default selected item.
More information can be found here: http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail
I am using a multi form model. A $model array is passed to the view and for each model object I am trying to have an errorsummary. See the code below.
foreach ($model as $f=>$edu):
echo $form->errorSummary($edu,'');
echo $form->textField($edu,"[$f]schoolname",array('size'=>30,'maxlength'=>128));
endforeach;
When I submit the form an error summary for only one form is displayed. Any ideas.
Maybe you need to rethink your form structure, for example - put all into one form, then inside controller make validation of every model you use and then show error messages.