How to Display a selected grade with its subject?

心已入冬 提交于 2021-02-11 12:42:35

问题


I want to when a user select a dropdown from the list, a group of subjects available for that grade must be displayed with checkboxes next to them

My controller

public function create()
    {
       
        $grades = Grade::with('subjects')->orderBy('slug', 'asc')->get();
        
        return view('admin.users.create', compact( 'grades'));
    }

Blade file

<div class="form-group">
         <select id="grade" name="grade" class="form-control @error('grade') is-invalid @enderror" v-model="selectedSubjects">
                 <option value="">Choose a Grade...</option>
                            @foreach($grades as $grade)
                  <option value="{{ $grade->id }}" {{ old('grade', $grade) == $grade->name ? 'selected' : "" }}>{{ $grade->name }}</option>                                            
                              @endforeach
               </select>
</div>


<div class="custom-control custom-checkbox mt-2">
        @foreach($grade->subjects as $subject)
                 <input type="checkbox" class="custom-control-input" id="{{$subject->slug}}" name="subjects[]" :value="selectedSubjects" /> 
                   <label class="custom-control-label" for="{{$subject->slug}}">{{$subject->name}}</label>
           @endforeach
  </div>

vue

<script>
    window.addEventListener('load',function(){
        var app = new Vue({
            el: '#app',
            data:{
                selectedSubjects: {!! $grade->subjects->pluck('name') !!},
            }
        });
    });
</script>

THIS IS IMPOSSIBLE... I GIVE UP


回答1:


As per I have understood, you want to select grades from dropdown & show its corresponding checkbox as per subjects for the grades.

I would suggest to create a vue component for that lets say grades-component, in your blade you can add,

<form action="" class="form-control">
       @csrf
       <grade-component :grades='@json($grades)'></grade-component>
</form>

here in blade, $grades is the object(or array) you are passing via compact. Basically it is to pass your data to the component, we will use that with help of props.

Now you can add your GradeComponent.vue in resources->js->components->GradeComponent.vue

GradeComponent.vue

<template>
<div class="container">

  <select v-model="selected_grade" @change="onChange($event)">
  <option v-for="grade in grading" :value="grade.slug">{{grade.name}}</option>
  </select>
    <div class="custom-control custom-checkbox mt-2" v-if="subjects !== null" v-for="subject in subjects">
          <input type="checkbox" :id="subject.slug" :value="subject.slug"/> 
          <label :for="subject.slug">{{subject.name}}</label>
        
    </div>
</div>

</template>
<script>

export default{
      props: ['grades'],
 data: function() {
    return {
      grading: this.grades,
      selected_grade: null,
      subjects : null
    }
  },
  methods: {
        onChange(event) {
          this.grading.forEach((obj, index) => {
            if (obj.slug === event.target.value){
                this.subjects = obj.subjects;
            }
          })
        }
    }
}
</script>

Now finally you can add it in app.js

Vue.component('grade-component', require('./components/GradeComponent.vue').default);

Then compile your vuejs code, I would use npm run watch

A similar one but with fake data, you can see https://jsfiddle.net/bhucho/2yu4nmws/3/,



来源:https://stackoverflow.com/questions/64677075/how-to-display-a-selected-grade-with-its-subject

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