【发布时间】:2017-08-07 10:33:23
【问题描述】:
我知道如何将变量从刀片模板传递到 jQuery 模态,但我不知道如何以其他方式进行。例如,我可以检查用户是否已注册:
@if(\Illuminate\Support\Facades\Auth::user())
open modal with options
@else
open basic modal message "you are not authorized"
@endif
但是现在,我想检查一下注册用户是否可以使用他点击的设备。我有所有必要的模型关系,所以我可以做类似Auth::user()->equipment->where('equipment_id', $equipment->id) != null...但问题是我可以像data-equipment-id = ... 这样将变量转发给jQuery并在脚本中获取它...但是我怎么得到它回到 PHP 变量,所以我可以像这样访问它:
@if(\Illuminate\Support\Facades\Auth::user())
@if(Auth::user()->equipment->where('equipment_id', $equipment->id) != null)
allowed!
@else
not allowed!
@endif
@else
open basic modal message "you are not authorized"
@endif
编辑:
这是我将数据转发到模态的方式:
@foreach($equipment as $instrument)
<td>
<a href="#" style="display: block;"
data-href="{{route('reserve-me', [$instrument->id, $scheduler_start_time->timestamp])}}"
data-toggle="modal"
data-target="#confirm-reservation"
data-start-time="{{$scheduler_start_time->toW3cString()}}"
data-end-time="{{$scheduler_end_time->toW3cString()}}"
>
@if($instrument->reservations
->where('reserved_from','<=', $scheduler_start_time)
->where('reserved_to','>=', $scheduler_start_time)->first() != null)
HERE
@else
@endif
</a>
</td>
@endforeach
然后是模态脚本:
$('#confirm-reservation').on('show.bs.modal', function (e) {
$(this).find('.dropdown-menu li').remove();
$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
var startTime = moment($(e.relatedTarget).data('start-time')).utc();
var endTime = moment($(e.relatedTarget).data('end-time')).utc();
while (startTime < endTime) {
$(this).find('.dropdown-menu').append('<li><a href="#">' + startTime.format("HH:mm") + '</a></li>');
startTime.add(30, 'minutes');
}
$(document).on('click', '#confirm-reservation .dropdown-menu a', function () {
$('#insert_text').text($(this).text());
var href = $(e.relatedTarget).data('href');
var time = moment().year(startTime.format("Y")).month(startTime.format("M")).date(startTime.format("D"))
.hour(($(this).text().split(':')[0]) - 2).minutes($(this).text().split(':')[1]).seconds("0");
console.log(time);
$('.btn-ok').attr('href', href + '/' + time.unix());
});
});
【问题讨论】:
-
你可以使用
AJAXapi.jquery.com/jquery.ajax