我在创建新预订时创建了一个jquery来显示模态窗口,并且它工作正常。 但是我在定义更新操作的URL时遇到了一些问题(根据id它有动态URL)。我在这部分定义了错误的网址:
$.get('reservation/update',{'id'}, function(data)
这是 main.js :
$(function() {
$(document).on('click','.fc-day',function(){
var date= $(this).attr('data-date');
$.get('?r=reservation/create',{'date':date}, function(data)
{
$('#modal').modal('show')
.find('#modalContent')
.html(data);
});
});
$(document).on('click','.fc-day-grid-event',function(){
$.get('reservation/update',{'id'}, function(data)
{
$('#modal').modal('show')
.find('#modalContent')
.html(data);
});
});
$('#modalButton').click(function(){
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value')); }); });
这是ReservationController中的 ActionUpdate :
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->renderAjax('update', [
'model' => $model,
]);
}
}
预订Index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\bootstrap\Modal;
$this->title = 'Reservations';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="reservation-index">
<h2> <?= Html::encode($this->title) ?></h2>
<br>
<?php
Modal::begin([
'header'=>'reservation',
'id'=>'modal',
'size'=>'modal-lg',
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
<?= \yii2fullcalendar\yii2fullcalendar::widget(array(
'events'=> $events,)); ?>
</div>
那么我应该如何将id参数从模型解析为URL?
答案 0 :(得分:1)
获取Javascript的id变量: -
$(document).on('click','.fc-day-grid-event',function(){
var id = $(this).attr('id');
$.get('reservation/update',{id : id}, function(data)
{
$('#modal').modal('show')
.find('#modalContent')
.html(data);
});
});