“编辑Laravel”上的负载相关下拉列表

ehxuflar  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(125)

如何自动加载或检索子类别/依赖下拉列表的基础上选择的值在编辑中的父下拉列表。如果我点击编辑,医生应立即加载。我已经搜索了几个小时的解决方案,请帮助。
"刀锋"

<select name="clinic_id" data-placeholder="Search" data-allow-clear="true"
   class="form-control select2bs4" style="width: 100%;" id="load_clinic">
   <option selected="selected"></option>
       @foreach ($clinic as $id => $item)
         <option value="{{ $id }}"
             {{ $schedule->clinic_id == $id ? 'selected' : '' }}>
              {{ $item }}
         </option>
       @endforeach
</select>

<select name="doctor_id" data-placeholder="Search" data-allow-clear="true"
    class="form-control select2bs4" style="width: 100%;" id="load_doctor">
</select>

Javascript语言

$(document).ready(function() {
        $('#load_clinic').on('change', function(e) {
            var clinic_id = e.target.value;
            if (clinic_id) {
                $.ajax({
                    url: "{{ route('getDoctor') }}",
                    type: "POST",
                    data: {
                        clinic_id: clinic_id,
                        _token: "{{ csrf_token() }}"
                    },
                    success: function(data) {
                        if (data) {
                            $('#load_doctor').empty();
                            $('#load_doctor').append(
                                '<option value=""> Select Doctor</option>');
                            $.each(data, function(key, value) {
                                $('#load_doctor').append($(
                                    "<option/>", {
                                        value: key,
                                        text: value
                                    }));

                            });
                        } else {
                            $('#load_doctor').empty();
                        }
                    }
                })
            } else {
                $('#load_doctor').empty();
            }
        });
    });

控制器

public function edit($id)
{
    $schedule = Schedule::findOrFail($id);
    $clinic = Clinic::pluck('name', 'id');
    $doctor = Doctor::get()->pluck('full_name', 'id');
    return view('admin.schedule.edit', compact('schedule', 'doctor', 'clinic'));
}

public function getDoctor(Request $request)
{
    $id = $request->clinic_id;
    $doctor = Doctor::where('clinic_id', $id)->get()->pluck('full_name', 'id');
    return response()->json($doctor);
}
ioekq8ef

ioekq8ef1#

在使用 AJAX 之前,可以使用PHP获得初始选择选项。
使用get()时,请考虑使用复数名词;使用i.e. first()时,请考虑使用单数名词
此外,请考虑在blade中使用@if(condition)@endif而不是三元运算符
这是PHP的解决方案,如果你想用 AJAX 获得医生,除了onChange监听器,你还应该在页面加载时调用它,

控制器

public function edit($id)
{
    $schedule = Schedule::findOrFail($id);
    $clinic = Clinic::pluck('name', 'id');
    $doctor = Doctor::where('clinic_id', $schedule->clinic_id)->get();
    return view("admin.schedule.edit", compact('schedule', 'doctor', 'clinic'));
}

"刀锋"

<select name="clinic_id" data-placeholder="Search" data-allow-clear="true"
   class="form-control select2bs4" style="width: 100%;" id="load_clinic">
   <option selected="selected"></option>
       @foreach ($clinic as $id => $item)
         <option value="{{ $id }}"
             {{ $schedule->clinic_id == $id ? 'selected' : '' }}>
              {{ $item }}
         </option>
       @endforeach
</select>

<select name="doctor_id" data-placeholder="Search" data-allow-clear="true"
    class="form-control select2bs4" style="width: 100%;" id="load_doctor">
    @foreach($doctor as $item)
        <option value="{{$item->id}}">{{$item->full_name}}</option>
    @endforeach
</select>

相关问题