简体   繁体   English

当用户从yii2的下拉列表中选择名称时,如何获取并在另一个表单字段中显示用户名?

[英]how to get and display username in another form field when user select name from dropdownlist in yii2?

I want to display the lecturer username in another text input field form when the user selects the lecturer name in the drop-down list above. 当用户在上面的下拉列表中选择讲师姓名时,我想以另一种文本输入字段形式显示讲师用户名。 这是我的表格

this is my code for drop-down list lecturer name 这是我的下拉列表讲师姓名的代码

<?= $form->field($model, 'LecturerName')->dropDownList(
        ArrayHelper::map(User::findAll(['category' => 'lecturer']),'fullname','fullname'),
        ['prompt'=>'Select Lecturer']
        ) ?>

this is my code for lecturer username 这是我讲师用户名的代码

<?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>

I want to get and display the LecUsername based on the selected drop-down list above. 我想根据上面选择的下拉列表获取并显示LecUsername。 the LecUsername is from the database. LecUsername来自数据库。

As you are using the dropdown you need to bind the change event for the dropdown to insert the value from the dropdown to the input text. 使用下拉菜单时,您需要绑定下拉菜单的change事件,以将下拉列表中的值插入到输入文本中。 And to make sure that you are inserting the LecUsername for the LecturerName you need to have the LecUsername as the value for the dropdown, means the drop-down data should have the username field as the index, currently you are using the fullname as the index and the value so change the code according to the field name you have for the username, i assume it is username for the example. 为了确保您要为LecturerName插入LecUsername ,您需要将LecUsername作为下拉列表的值,这意味着下拉数据应具有username字段作为索引,当前您使用fullname作为索引和值,因此根据您为用户名使用的字段名称更改代码,我假设它是示例的username

ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname')

so your dropdown code will look like 因此您的下拉代码如下所示

<?= $form->field($model, 'LecturerName')->dropDownList(
        ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
        ['prompt'=>'Select Lecturer']
        ) ?>

then add the below on top of your view file where you have the form 然后在您拥有表单的视图文件顶部添加以下内容

$ddId=Html::getInputId($model, 'LecturerName');
$inputId=Html::getInputId($model, 'LecUsername');

$js = <<< JS
$(document).ready(function(){
    $("#{$ddId}").on('change',function(e){
        $("#{$inputId}").val($(this).val());
    });
});
JS;
 $this->registerJs($js, \yii\web\View::POS_READY);

A better way for dropdowns is to use the library Kartik-v\\select2 and use the event pluginEvents option to specify the event select2:select , your code should look like below in that case. 下拉菜单的更好方法是使用库Kartik-v \\ select2并使用event pluginEvents选项指定事件select2:select ,在这种情况下,您的代码应如下所示。

// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
    'options' => ['placeholder' => 'Select Lecturer'],
    'pluginOptions' => [
        'allowClear' => true
    ],
    'pluginEvents'=>[
        "select2:select" => 'function() { $("#{$ddId}").on('change',function(e){
             $("#{$inputId}").val($(this).val());
         }); }',
    ]
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM