以下是相关代码:
查看页面:
<script type="text/javascript">
$(document).on("click", ".calendarview", function () {
var roomid = $(this).data('id');
//alert(roomid);exit;
$('#calendar_view').fullCalendar({
//events: [
// {
// title: 'All Day Event',
// start: roomid
// }
// ]
events: {
url: base_url+'home/calview',
dataType: 'json',
data: {roomid: roomid},
type: 'POST', // Send post data
error: function() {
alert('There was an error while fetching events.');
}
}
});
});
</script>
IN CONTROLLER
function calview()
{
$room_id =$this->input->post('roomid');
$events=$this->Home_model->find_room_availability($room_id);
$data_events = array();
foreach($events as $r) {
$data_events[] = array(
//"title" => $r->available,
"description" => 'available',
"start" => $r->dt);
}
echo json_encode(array("events" => $data_events));
exit();
}
MODEL:
function find_room_availability($room_id){
$result = $this->db->query("SELECT x.dt , r.room_cnt - COALESCE(SUM(`booking_cnt`),0) available FROM calendar_table x LEFT JOIN bookinglist y ON x.dt >= y.`date_from` AND x.dt < y.`date_to` LEFT JOIN rooms r ON r.id=$room_id WHERE x.dt BETWEEN now() - interval 3 month AND now() + interval 5 month GROUP BY dt ");
//echo $this->db->last_query();
//exit;
return $result->result();
}
Ajax响应就像
[[{"title":"10","start":"2017-04-26"},{"title":"10","start":"2017-04-27"},
{"title":"10","start":"2017-04-28"},{"title":"10","start":"2017-04-29"},
{"title":"10","start":"2017-04-30"},{"title":"10","start":"2017-05-01"},
{"title":"10","start":"2017-05-02"},{"title":"10","start":"2017-05-03"},
{"title":"10","start":"2017-05-04"},{"title":"10","start":"2017-05-05"},
{"title":"10","start":"2017-05-06"},{"title":"10","start":"2017-05-07"},
{"title":"10","start":"2017-05-08"},{"title":"10","start":"2017-05-09"},
{"title":"10","start":"2017-05-10"},{"title":"10","start":"2017-05-11"},
{"title":"10","start":"2017-05-12"},{"title":"10","start":"2017-05-13"},
{"title":"10","start":"2017-05-14"},{"title":"10","start":"2017-05-15"},
{"title":"10","start":"2017-05-16"},{"title":"10","start":"2017-05-17"},}]]
任何人都可以帮我找到解决方案
答案 0 :(得分:0)
您的JSON是数组中的数组,fullCalendar无法理解。它需要一系列事件。
这是问题所在:
echo json_encode(array("events" => $data_events));
由于$data_events
已经是一个数组,因此无需将其包含在另一个数组中。
echo json_encode($data_events);
应该足够了,并产生fullCalendar所期望的格式。
N.B。在JSON示例的末尾似乎还有一个额外的支架:,}]]
但我认为这只是一个错字。