简体   繁体   English

如何将我从下拉列表中选择的数据附加到yii2中的下一个输入字段

[英]how will I append data I am selecting from a dropdown list to the next input field in yii2

I want to select courses from the course field, then let it show in my courses field and submit the courses to the database. 我想从“课程”字段中选择课程,然后将其显示在“我的课程”字段中,然后将课程提交到数据库。 or if someone can help me so my courses can appear has a checkbox and once i tick a checkbox, it appears in the courses field 或者如果有人可以帮助我,以便我的课程可以显示一个复选框,而当我勾选该复选框时,它会出现在“课程”字段中

<?php $form = ActiveForm::begin(); ?>
<?=$form->field($model, 'student_id') ?>
<?=
$form->field($model, 'faculty_id')->dropDownList(
        ArrayHelper::map(Faculties::find()->asArray()->all(), 'faculty_id', 'faculty_name'), [
    'prompt' => 'Select Faculty',
    'onchange' => '$.post("' . Yii::$app->urlManager->createUrl('/departments/lists?id=') . '"+$(this).val(), function(data) {
                            $("select#registrations-department_id").html (data);
                        });'
        ]
);
?>
<?=
$form->field($model, 'department_id')->dropDownList(
        ArrayHelper::map(Departments::find()->asArray()->all(), 'department_id', 'department_name'), [
    'id' => 'registrations-department_id',
    'prompt' => 'Select Department',
    'onchange' => '$.post("' . Yii::$app->urlManager->createUrl('/courses/lists?id=') . '"+$(this).val(), function(data) {
                            $("select#registrations-course_id").html (data);
                        });'
]);
?>

<?=
$form->field($model, 'course_id')->dropDownList(
        ArrayHelper::map(Courses::find()->asArray()->all(), 'course_id', 'course_name'), [
    'id' => 'registrations-course_id',
    'prompt' => 'Select Course',
    'onchange' => '$.post("(selected:)."+$(this).val(), function(data){
                            $("#registrations-courses").append(data)
                        });'
]);
?>
<!--        <?//= $form->field($model, 'courses')->checkBoxList([]) ?> -->


<?=$form->field($model, 'courses') ?>
<?=$form->field($model, 'comment') ?>
<?=$form->field($model, 'status') ?>
<?=$form->field($model, 'created_at') ?>
<?=$form->field($model, 'updated_at') ?>

<div class="form-group">
    <?=Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>

EDIT 编辑

this is my controller how do I save the courses in the database in the same column? 这是我的控制器,我如何将课程保存在同一列的数据库中?

 public function actionCreate()
{
    $model = new Registrations();

    if ($model->load(Yii::$app->request->post())) {
        if ($model->validate()) {
            // form inputs are valid, do something here
            $model->student_id = Yii::$app->user->identity->id;
            $model->faculty_id = 1 ;
            $model->department_id = null ;
            $model->course_id = 1;
            $model->save();
            return $this->redirect('../site/profile');
        }
    }
    return $this->render('create', [
        'model' => $model,
    ]);
}

You can use the checkBoxList() to create the list of checkboxes based on the courses list. 您可以使用checkBoxList()根据课程列表创建复选框列表。

Change your course_id field to the following 将您的course_id字段更改为以下内容

$form->field($model, 'course_id')->checkboxList(yii\helpers\ArrayHelper::map(Courses::find()->asArray()->all(), 'course_id', 'course_name'), [
    'inline' => true, 
    'unselect'=>null,
    'item' => function($index, $label, $name, $checked, $value){
        $checked = $checked ? 'checked' : '';
        return "<input type='checkbox' class='my-courses' name='{$name}' value='{$value}' {$checked} data-label='{$label}'>&nbsp;<label>{$label}</label>";
    }]);

add an id attribute to your courses field like below id属性添加到您的courses字段,如下所示

$form->field($model, 'courses', ['inputOptions' => ['id' => 'courses']]);

and then add this javascript code to the top of your page 然后将此JavaScript代码添加到页面顶部

$js = <<< JS
        $(".my-courses").on('click',function(e){
                let course_name=$(this).data('label');
                let course_field=$("#courses");
                if($(this).is(":checked")){
                    $("#courses").val()==''?course_field.val(course_name):course_field.val(course_field.val()+', '+ course_name);
                }else{
                    let regex=new RegExp(',{0,1}\\\s{0,1}('+course_name+')','g');
                    course_field.val(course_field.val().replace(regex,''));
                }
            })
JS;
$this->registerJs($js, \yii\web\View::POS_READY);

Now if you select the checkbox the name of the course would be copied to the input field and when you submit your form you can get the fields in your $_POST array under your specific model index like below. 现在,如果选中此复选框,则课程名称将被复制到输入字段,并且在提交表单时,您可以在$_POST数组中的特定模型索引下获得这些字段,如下所示。

[course_id] => Array
                (
                    [0] => 1
                    [1] => 2
                )

[courses] => asdas, shugal, spoon

You can verify by adding a print_r(Yii::$app->request->post()) in your controller action inside the check 您可以通过在检查中的控制器操作中添加print_r(Yii::$app->request->post())来进行验证

if($model->load(Yii::$app->request->post())){
     print_r(Yii::$app->request->post());

You can iterate on the course_id to save all of the course_id that were selected as checkboxes. 您可以对course_id进行迭代,以保存所有被选中为复选框的course_id

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

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