Reputation: 780
I have a dynamically loaded dropdownlist called type_id
, and I would like that when I select a value in that dropdownlist, my description
textarea field would be updated with a value.
form
echo $form->field($model, 'type_id')->dropDownList(ArrayHelper::map($types,'id','name'),['prompt'=>'Selecione a Área',
'onchange' => '???']);
<?= $form->field($model, 'description')->textarea(['rows' => 8]) ?>
This value will be obtained dynamically through the query:
Controller
public function actionScript($id)
{
$types = Type::find()
->where(['id' => $id])
->One();
return $type->script;
}
That is, in textarea
field I want to show the respective script
column to the selected id
in type_id
dropdonw.
type table
TABLE `mod_helpdesk_type` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`priority` int(1) NOT NULL DEFAULT '0',
`script` text NOT NULL,
`active` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
Upvotes: 0
Views: 433
Reputation: 186
try onchange ajax event
echo $form->field($model, 'type_id')->dropDownList(ArrayHelper::map($types,'id','name'),[
'prompt'=>'Selecione a Área',
'onchange' => '
$.get( "yourControllerName/script&id="+$(this).val(), function( data ) {
$( "#yourTextAreaIdElement" ).val( data );
});
'
]);
and better prevent if request has an ids passed into actionScript
public function actionScript($id)
{
$script = '';
if(!empty($id)) {
$types = Type::find()
->where(['id' => $id])
->one();
if(!empty($types)) {
$script = $types->script;
}
}
return $script;
}
Upvotes: 0
Reputation: 1759
It will help you
echo $form->field($model, 'type_id')->dropDownList(ArrayHelper::map($types,'id','name'),['prompt'=>'Selecione a Área',
['class' => 'your_class', 'id' => 'your_id']
'onchange' => '???']);
<?= $form->field($model, 'description')->textarea(['rows' => 8,'id'=>'textArea']) ?>
$('#your_id').change(function(){
$('#textArea').val('testing');
})
Upvotes: 2
Reputation: 396
Like that?
$('#<?php echo Html::getInputId($model, 'type_id'); ?>').change(function () {
var val = $(this).val();
$.ajax({
url: '<?php echo Url::to('script'); ?>',
type: 'get',
data: {id: val},
success: function (data) {
$('#<?php echo Html::getInputId($model, 'description'); ?>').val(data);
}
});
});
Upvotes: 0