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

如何在CodeIgniter中结合Webix Scheduler实现点击Done更新数据库表?

实现Webix Scheduler日程更新到CodeIgniter后端的步骤

1. 绑定Scheduler弹窗Done按钮的提交事件

Webix Scheduler的Done按钮点击会触发onEventSave事件,直接在Scheduler初始化代码里绑定该事件,收集修改后的日程数据并发送请求到后端接口:

// 在calendar.php视图的Scheduler初始化代码中添加
webix.ui({
  view: "scheduler",
  id: "scheduler",
  // 其他原有配置项...
  on: {
    onEventSave: function(id, event) {
      // 收集日程数据,确保字段名和后端、数据库表一致
      var postData = {
        appointment_id: id, // 对应数据库表的主键字段
        title: event.text,
        start_datetime: event.start_date,
        end_datetime: event.end_date
        // 按需添加其他字段,比如description、status等
      };

      // 发送POST请求到CodeIgniter控制器接口
      webix.ajax().post("<?= base_url('appointments/update') ?>", postData, function(response) {
        var result = response.json();
        result.success ? webix.message("日程更新成功") : webix.message({type:"error", text:"更新失败"});
      });
    }
  }
});

如果需要自定义弹窗按钮行为,也可以直接修改弹窗配置:

webix.ui({
  view: "scheduler",
  id: "scheduler",
  popup: {
    buttons: [
      { value: "Cancel", click: function(){ $$("scheduler").hideLightbox(); } },
      { value: "Done", click: function(){
          var event = $$("scheduler").getEvent();
          var id = event.id;
          // 复制上述收集数据、发送请求的代码
          $$("scheduler").hideLightbox();
        }
      }
    ]
  },
  // 其他原有配置...
});

2. 修复前后端变量名不匹配问题

  • 前端传参的字段名必须和后端控制器接收变量、数据库表字段完全统一。比如数据库表appointments字段是appointment_id(主键)、title、start_datetime,前端就不能传start_date后端用start_time。
  • 在AppointmentsController的update方法中统一接收参数:
// AppointmentsController.php
public function update() {
  // 接收前端参数,确保变量名和前端传参一致
  $data = [
    'title' => $this->input->post('title'),
    'start_datetime' => $this->input->post('start_datetime'),
    'end_datetime' => $this->input->post('end_datetime')
    // 按需添加其他字段
  ];
  $appointment_id = $this->input->post('appointment_id');

  // 调用模型执行更新
  $result = $this->CitaModel->update_appointment($appointment_id, $data);
  // 返回JSON格式响应给前端
  echo json_encode(['success' => $result]);
}

3. 给模型更新方法添加主键条件

修改CitaModel的更新方法,必须以主键为条件,避免误更新全表数据:

// CitaModel.php
public function update_appointment($appointment_id, $data) {
  // 主键过滤,确保只更新指定ID的记录
  $this->db->where('appointment_id', $appointment_id);
  // 执行更新,返回受影响行数(成功返回true,失败返回false)
  return $this->db->update('appointments', $data);
}

4. 配置后端路由

在application/config/routes.php中添加对应路由,确保前端请求能正确映射到控制器方法:

$route['appointments/update'] = 'AppointmentsController/update';

额外验证建议

可以在控制器中添加参数合法性校验,避免无效请求:

// AppointmentsController.php 的update方法中添加
if(empty($appointment_id) || !$this->CitaModel->check_exists($appointment_id)) {
  echo json_encode(['success' => false, 'msg' => '无效的日程ID']);
  return;
}

// 同时在CitaModel中新增check_exists方法
public function check_exists($appointment_id) {
  return $this->db->where('appointment_id', $appointment_id)->get('appointments')->num_rows() > 0;
}

内容的提问来源于stack exchange,提问作者zaraterdz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:14:52