Issue
I want to attribute kodepos and kabupaten in one dropdown. and kodepos and kabupaten attributes are included in the database. Can anyone help me? thank you
<?= $form->field($model, 'origin')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),'kodepos','kodepos'),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kode Pos','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?= $form->field($model, 'origin_name')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),'kabupaten','kabupaten'),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kota Asal','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
Solution
You need to change the ArrayHelper::map() to use the closure for the third parameter or the $to parameter and return the 2 fields concatenated as you want.
The field names are not clear that you want to show as text in the first and second drop-down so I assume the integer value to be coming from the id field.
I will add the code that will show the id and kodepos concatenated as the text for the first drop-down and show the id and kabupaten concatenated as the text for the second drop-down. Change them accordingly in the code
<?= $form->field($model, 'origin')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),'kodepos',function($model){return $model->id.$model->kodepos}),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kode Pos','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?= $form->field($model, 'origin_name')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),'kabupaten',function($model){return $model->id.$model->kabupaten}),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kota Asal','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
EDIT:
You want the kodepos and kabupaten in one drop-down rather than making 2 drop-downs so change the code to the following
<?= $form->field($model, 'origin')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),function($model){return $model->kodepos.$model->kabupaten}),function($model){return $model->kodepos.$model->kabupaten}),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kode Pos','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
Answered By - Muhammad Omer Aslam
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.