簡體   English   中英

Yii2上的Ajax for fullcalendar

[英]Ajax on Yii2 for fullcalendar

我正在使用Fullcalendar for Yii2( https://github.com/philippfrenzel/yii2fullcalendar-demo ),當我點擊日期時,我想用Ajax保存這個事件。 數據庫來自下拉列表。

似乎我的代碼在我的控制器中找不到該功能,也許它是Url?

我對JS的看法以及我在控制器上使用Ajax的鏈接:

<?php 
        $form = ActiveForm::begin(); 
    ?>

    <div class="row">
        <div class="col-md-4">
            <?= $form->field($feuille_de_jour_responsable, 'ID_Categorie')->dropDownList(CategorieFdj::find()->select(['Nom', 'ID_Categorie'])->indexBy('ID_Categorie')->column(), ['id'=>'catId']); ?>
        </div>
        <div class="col-md-4">
            <?= $form->field($feuille_de_jour_responsable, 'ID_Poste_FDJ')->dropDownList(PosteFdj::find()->select(['Nom_Poste_FDJ', 'ID_Poste_FDJ'])->indexBy('ID_Poste_FDJ')->column(), ['id'=>'postId']); ?>
        </div>
        <div class="col-md-4">
            <?= $form->field($feuille_de_jour_responsable, 'Code_Personnel')->dropDownList(Personnel::find()->select(['Nom_Personnel', 'Code_Personnel'])->indexBy('Code_Personnel')->column(), ['id'=>'codePers']); ?>
        </div>
    </div>


    <?php ActiveForm::end();?>



<?php 
    $JSCode = <<<EOF

    function(start,end) {
        //alert ($("select[id=catid] option:selected").text());
        var title = $("select[id=codePers] option:selected");
        var codePersonnel = $("select[id=codePers] option:selected").val();
        var posteId = $("select[id=postId] option:selected").val();
        var categorieID = $("select[id=catId] option:selected").val();
        //alert($('#catid').val());
        var eventData;
var obj = { 
                    Date_Calendaire : start.format(),
                    ID_Poste_FDJ : posteId,
                    ID_Categorie : categorieId,
                    Code_Personnel : codePersonnel
                    };
$.ajax({
                url : 'index.php?r=feuille-de-jour-responsable/create',
                dataType: 'json',
                data: obj,
                success: function (data, response, event, date) {
                    alert("success here");
                    /*$('#calendar').fullCalendar('renderEvent',
                    {
                        title: title,
                        start: start.format()
                        //end: thedate1
                    }, true);
                    eventData = {
                        title: title,
                        start: start.format(),
                        end: start.format(),
                    };
                    $('#calendar').fullCalendar('renderEvent', eventData, true);*/
                },
                error: function () {
                    alert("Oops! Something didn't work");
                }
            });
}

EOF;

$JSEventClick = <<<EOF
function(calEvent, jsEvent, view) {
    alert('Event: ' + calEvent.title);
    alert('Coordinates: ' + jsEvent.pageX + ',' + jsEvent.pageY);
    alert('View: ' + view.name);
    // change the border color just for fun
    //$(this).css('border-color', 'red');
}
EOF;


?>

    <?= yii2fullcalendar\yii2fullcalendar::widget([
        'id' => 'calendar',
        'clientOptions' => [
            'height' => 650,
           // 'language' => 'fa',
            //'eventLimit' => TRUE,
            'selectable' => true,
            'selectHelper' => true,
            'droppable' => true,
            'editable' => true,
//          'theme'=>true,
            'fixedWeekCount' => false,
            'defaultDate' => date('Y-m-d'),
            'eventClick' => new JsExpression($JSEventClick),
            'select'=>new JsExpression($JSCode)
        ],            

    ]);
?>

    <?= Html::encode($JSCode); ?> 

    <?= Html::encode($JSEventClick); ?>

和我的控制器上的功能(FeuilleDeJourResponsableController)

        public function actionCreate()
{

     $feuille_de_jour_responsable = new FeuilleDeJourResponsable();

    if ($feuille_de_jour_responsable->load(Yii::$app->request->post()) && $feuille_de_jour_responsable->save()) {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        //return $this->redirect(['view', 'Date_Calendaire' => $feuille_de_jour_responsable->Date_Calendaire, 'ID_Poste_FDJ' => $feuille_de_jour_responsable->ID_Poste_FDJ, 'ID_Categorie' => $feuille_de_jour_responsable->ID_Categorie, 'Code_Personnel' => $feuille_de_jour_responsable->Code_Personnel]);
        return ['success' => $feuille_de_jour_responsable->save()];
    } else {
        return $this->render('create', [
            'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
        ]);
    }
  }

我開始使用firebug,當我點擊時,它沒有保存...沒有錯誤。 我正在考慮與“POST”的鏈接(這與表單中的“普通”鏈接相同),我的函數“創建”將保存數據,但沒有任何事情發生。 “if”沒有用,我不知道為什么。

螢火蟲

我發現異常:“SyntaxError:意外的令牌<在位置0的JSON中”

謝謝你的幫助=)莎拉

在您的情況下,失敗的save操作的返回值不是很有效。

if ($feuille_de_jour_responsable->load(Yii::$app->request->post()) && $feuille_de_jour_responsable->save()) {
    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    return ['success' => $feuille_de_jour_responsable->save()];
} else {
    return $this->render('create', [
        'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
    ]);
}

您需要以JSON格式返回保存結果,而不是頁面的HTML代碼。 由於您嘗試顯示整個HTML內容,因此您將獲得SyntaxError: unexpected token < in JSON in position 0

為了成功運作:

return ['success' => true];

順便說一下,你不能在返回響應中再次使用$feuille_de_jour_responsable->save() ,因為它會嘗試保存兩次。

對於失敗的操作:

return ['success' => false];

你不能使用render因為這是一個整頁,你正在使用Ajax請求。

暫無
暫無

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

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