簡體   English   中英

Yii2更新動作jQuery

[英]Yii2 Update Action JQuery

我創建了一個jquery以在創建新的預留時顯示模式窗口,並且效果很好。 但是我在定義更新操作的URL時遇到了麻煩(根據ID具有動態URL)。 我在這部分定義的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> &nbsp;<?=  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?

獲取您的Java的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);
        });     
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM