【发布时间】:2021-05-06 08:30:42
【问题描述】:
您好,我目前正在做一个预约表格,用户可以在其中选择他想要预约的日期和时间范围。我想在单击搜索按钮时在模式上显示结果数组,并将我的模式上的结果数组填充为
@forelse($dt 为 $dt)。
下面是我的blade.php查看页面:
@section('content')
<div class="card-body">
<form action="{{ route('assistant.newappts.new') }}" method="POST">
@method('post')
@csrf
<label for="select_date">Select Date:</label>
<input type="text" id="select_date" class="form-control" name="select_date" placeholder="Select Date">
<div class="spacer"></div>
<div class="half-form">
@foreach ($operatinghrs as $operatinghr)
<div class="form-group">
<label for="starttime">Choose Start Time </label>
<br>
<input type="time" id="start_time" name="start_time" min="{{display24HTime($operatinghr->start_time)}}" max="{{display24HTime($operatinghr->end_time)}}" value="{{ display24HTime(old('start_time', $operatinghr->start_time))}}">
</div>
<div class="form-group">
<label for="starttime">Choose End Time</label>
<br>
<input type="time" id="end_time" name="end_time" min="{{display24HTime($operatinghr->start_time)}}" max="{{display24HTime($operatinghr->end_time)}}" value="{{ display24HTime(old('end_time', $operatinghr->end_time))}}">
<div class="form-update" style="float:right;">
<button type="button" class="btn btn-primary" data-toggle="modal" id="find_appointment_btn">Search</button>
</div>
</div>
@endforeach
</div>
<div class="spacer"></div>
</form>
</div>
@overwrite
@section('modal')
<!-- MODAL NEW APPOINTMENT -->
<div class="modal fade" id="myModalNewAppointment">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Select Appointment</h4>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<div class="timeslots-section">
<div class="timeslots text-center">
@forelse ($dt as $dt)
<div class="timeslot">
<input type="checkbox" class="timeslotchk" name="timeslot" value="{{$dt}}">
{{$dt}}
</div>
@empty
<div class="empty" style="text-align: left">No appointments available.</div>
@endforelse
</div> <!-- end timeslots -->
</div>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="new_appt_save_btn" data-loading-text="<i class='fa fa-spinner fa-spin'></i> saving..">Save</button>
<button type="button" class="btn btn-outline-primary" id="close_btn" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
@overwrite
下面是我的 Blade.php 视图页面的 javascript
<script>
$(document).ready(function() {
$('#select_date').datepicker({
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
todayHighlight: true,
toggleActive: true
});
$('#find_appointment_btn').click(function() {
$('#myModalNewAppointment').modal({backdrop: 'static', keyboard: false});
$('#myModalNewAppointment .modal-title').html('New Appointment');
});
})
</script>
下面是我用于页面加载的控制器类方法和用于获取约会时间的发布请求方法:
public function index()
{
if ($_SESSION['user_role'] != 'Assistant') {
return "Unauthorized Access";
}
$workinghr = WorkingHour::where('clinic_id', $_SESSION['clinic_ID'])->where('off_day', false)->pluck('day')->toArray();
$todayday = Carbon::now('Asia/Singapore')->format('l');
$operatinghrs = $this->getWorkDayTiming($todayday);
$data = [
'link_menu_appt' => 'open active',
'link_newappt' => 'active',
'operatinghrs' => $operatinghrs,
];
return view('patient_appointment.available-appointment', $data);
}
public function filterByDate(Request $request)
{
$dateselected = $request->select_date;
$starttime = $request->start_time;
$endtime = $request->end_time;
$dtarray = $this->getSelectedDate($dateselected, $starttime, $endtime);
$dt = array_slice($dtarray, 0, 12, true);
$todayday = Carbon::now('Asia/Singapore')->format('l');
$operatinghrs = $this->getWorkDayTiming($todayday);
$data = [
'link_appt' => 'active',
'operatinghrs' => $operatinghrs,
'dt' => $dt,
];
return view('patient_appointment.available-appointment', $data);
}
【问题讨论】:
-
即使这种方法可行,您通常也会向服务器发出 ajax 请求并将数据作为响应返回,而不是再次加载整个页面。 Vue.js 在这里很有用,但你也可以让它与基本的 JS 和 JQuery 一起工作。如果您真的想以当前方式执行此操作,您将向视图传递一个变量以指示必须自动打开模式以及可能的结果。或者是什么让你成为问题?
-
嗨@Aless55 感谢您的意见!是否有文档可以让我了解如何向服务器发出 ajax 请求并将数据作为响应返回?我想让页面加载只是表单,在提交所需的输入后,模态将与数据数组一起显示。
-
没有官方文档,因为它与 Laravel 没有直接关系。如果你在谷歌上搜索“Laravel 发布 ajax 教程”,你应该会找到一些很好的教程来指导你。否则 Vue.js 总是值得一试,那里也有无数的教程。
标签: javascript php laravel