select2 widget selected will updated other select2

喜欢而已 提交于 2020-01-25 12:03:46

问题


Hi all i am newbie in yii2 need your advise i use select2 widget, when i selected value it will throw another value that i had set in array before. so how can i do this in yii2. so far i doing this.i have try using jquery function using pluginevents in select2 but still stuck..here is my code

<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
<?= $form->field($model, 'npwp')->widget(Select2::classname(), [
    'language' => 'id',
    'data' => $idnpwp,
    'options' => ['placeholder' => 'Select a NPWP ...'],
    'pluginOptions' => [
    'allowClear' => true
    ],
    'pluginEvents' => [
        "change" => 'function(data) { 
            var data_id = $(this).val();
            $("input#target").val($(this).val());
        }',
    ]
]);  
?>

<?= $form->field($model, 'nama_wp')->textInput(['id' => 'target']) ?>

how can i insert 'nama_wp' that already set in array into field nama_wp thx for helping


回答1:


First thing you need to change the

<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>

to

<?php $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>

The you need to access the current value of the select2 via data.currentTarget.value, so change the code for change to the following.

<?php echo $form->field($model, 'npwp')->widget(Select2::classname(), [
    'language' => 'id',
    'data' => $idnpwp,
    'options' => ['placeholder' => 'Select a NPWP ...'],
    'pluginOptions' => [
    'allowClear' => true
    ],
    'pluginEvents' => [
        "change" => 'function(data) { 
            var data_id = data.currentTarget.value;
            $("#target").val(data_id );
        }',
    ]
]);  
?>


来源:https://stackoverflow.com/questions/58189617/select2-widget-selected-will-updated-other-select2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!