You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

yii2fullcalendar的eventDrop导致event.color与event.description丢失问题求助

解决Yii2 FullCalendar eventDrop后丢失color和description的问题

你遇到的核心问题是拖拽事件后,后端没有正确保存color和description(对应你的descripcion)属性,或者前端重新加载事件时未正确获取这些字段,导致刷新后丢失。结合你的代码,我整理了几个关键修复点:

1. 修复AJAX请求的参数传递方式

你当前在eventDrop里把POST参数直接拼在URL里,既不符合POST请求规范,还容易引发参数编码错误或丢失(比如特殊字符场景)。应该把参数放在data字段中传递:

修改后的$JsEventDrop代码:

<?php $JsEventDrop = 'function(event, delta, revertFunc) { 
    var event_data = { 
        id: event.id, 
        titulo: event.title, 
        descripcion: event.description, 
        fecha_inicio: $.fullCalendar.formatDate(event.start, "YYYY-MM-DD"), 
        hora_inicio: $.fullCalendar.formatDate(event.start, "HH:mm"), 
        hora_termino: $.fullCalendar.formatDate(event.end, "HH:mm"), 
        fecha_termino: $.fullCalendar.formatDate(event.end, "YYYY-MM-DD"), 
        color: event.color, 
    }; 
    if (!confirm("¿Está seguro que desea modificar la fecha y/o hora?")) { 
        revertFunc(); 
    } else { 
        $.ajax({ 
            type: "POST", 
            url: "index.php?r=calendario/update&id=" + event_data.id, 
            data: event_data, // 将参数放在data中,替代URL拼接
            success: function(json) { 
                if (json.success) {
                    alert("Fecha y/o hora modificada correctamente");
                    // 可选:无需刷新页面,直接重新加载日历事件
                    $("#calendar").fullCalendar("refetchEvents");
                } else {
                    alert("Error al modificar el evento: " + json.message);
                    revertFunc();
                }
            },
            error: function() {
                alert("Error en la conexión");
                revertFunc();
            }
        }); 
    } 
}' ?>

2. 修复后端actionUpdate的逻辑

你的actionUpdate存在两处逻辑问题:

  • 先直接调用$model->save(),之后又判断表单提交,逻辑混乱(AJAX请求不是表单提交,load()不会触发)
  • 未处理保存失败的情况,也没有返回JSON结果给前端做状态判断

修改后的actionUpdate:

public function actionUpdate($id) {
    $model = $this->findModel($id);
    
    // 处理AJAX POST请求
    if (Yii::$app->request->isPost) {
        $postData = Yii::$app->request->post();
        $model->titulo = $postData['titulo'];
        $model->descripcion = $postData['descripcion'];
        $model->fecha_inicio = $postData['fecha_inicio'];
        $model->hora_inicio = $postData['hora_inicio'];
        $model->hora_termino = $postData['hora_termino'];
        $model->fecha_termino = $postData['fecha_termino'];
        $model->color = $postData['color'];
        
        if ($model->save()) {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return ['success' => true, 'message' => 'Evento actualizado'];
        } else {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return ['success' => false, 'message' => implode('<br>', $model->getFirstErrors())];
        }
    }
    
    // 处理页面请求(如果需要)
    return $this->renderAjax('update', [
        'model' => $model,
    ]);
}

3. 确保事件列表返回description和color字段

在生成$events的代码中,要确保每个事件对象包含description(对应模型的descripcion)和color字段,比如:

$events = [];
foreach ($calendarioModels as $model) {
    $events[] = [
        'id' => $model->id,
        'title' => $model->titulo,
        'description' => $model->descripcion, // 与前端event.description对应
        'color' => $model->color, // 与前端event.color对应
        'start' => $model->fecha_inicio . 'T' . $model->hora_inicio,
        'end' => $model->fecha_termino . 'T' . $model->hora_termino,
    ];
}

4. 优化eventRender避免无效类

如果event.description或event.color为undefined,会给元素添加undefined类,建议先判断字段存在性:

<?php $JsEventRender = 'function(event, element) { 
    if (event.description && typeof event.description === "string") {
        element.addClass(event.description);
    }
    if (event.color && typeof event.color === "string") {
        element.addClass(event.color);
    }
}' ?>

5. 提升自定义CSS类优先级

你修改.fc-event无效,可能是自定义类优先级不足,可以使用更具体的选择器:

#calendar .fc-event.your-custom-color-class {
    background-color: #your-color;
    border-color: #your-color;
}

做完这些修改后,拖拽事件后端会正确保存color和descripcion,前端重新加载事件时也能获取到这些字段,刷新页面后就不会丢失属性了。

内容的提问来源于stack exchange,提问作者jc.reyes.suazo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:24:26