简体   繁体   中英

Yii2 Update Action JQuery

I created a jquery to show a modal window when creating new reservation, and it worked fine. But I'm having some trouble defining the url for the update action (it has dynamic url according to id). And I'm defining the url wrong in this part:

$.get('reservation/update',{'id'}, function(data)

Here is 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'));   });  });

Here is ActionUpdate in the ReservationController:

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,
        ]);
    }
}

Reservation 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>

So how should I parse the id parameter from model to the URL?

Get Your id variable of Javascript as:-

$(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);
        });     
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM