如何在代码点火器中显示从控制器到完整日历的数据



这是我的控制器

function Test_Function()
{

// Event id;
// event.title is the only place where you can "display" text;
// event.start: What date should the event be placed
$data = array
(
array
(
'id' => 1234, 
'title' => "Mr Name xxxxxnDate 2016-06-04T00:00:00nBirthdaynemail sasa@asfda.comnTelephone 1234567890",
'start' => "2016-06-15"
),
);
// add more array(...events...) as required
echo json_encode($data);    
}

我需要使用ajax在完整日历中显示这些详细信息,如何显示结果

<script>
$(document).ready(function() {
$('#calendar').fullCalendar({
events: {
url: '<?php echo site_url('Air/Get_calander_fare'); ?>',
type: 'POST',
data: {
value1: 'aaa',
value2: 'bbb'
},
success: function(data) {
console.log(data);
},
error: function() {
alert('there was an error while fetching events!');
},     
}
});
});

我需要在$data的日期中显示姓名,数据,电子邮件,电话

您有两个选项可以在日历中显示数据,并且必须根据文档 http://fullcalendar.io/docs/event_data/Event_Object/设置事件数据的格式。http://fullcalendar.io/docs/event_data/events_function/和 http://fullcalendar.io/docs/event_data/events_json_feed/中介绍了这两个选项

我将用 php 格式化数据并将事件用作 json,因为对我来说它更简单,但是,它需要更改您的Test_Function()。您可以通过以下方式Test_Function()回显数据:

function Test_Function()
{
// Event id;
// event.title is the only place where you can "display" text;
// event.start: What date should the event be placed
$data = array
(
array
(
'id' => 1234, 
'title' => "Mr Name xxxxxnDate 2016-06-04T00:00:00nBirthdaynemail sasa@asfda.comnTelephone 1234567890",
'start' => "2016-06-06"
),
);
// add more array(...events...) as required
echo json_encode($data);   
}

然后在jQuery中

作为 JSON 提要(旁注:您缺少事件的封闭 }:{ ...

$('#calendar').fullCalendar({
events: {
url: '<?php echo site_url('Test/Test_Function'); ?>',
type: 'POST',
data: {
value1: 'aaa',
value2: 'bbb'
},
success: function(data) {
console.log(data);
},
error: function() {
alert('there was an error while fetching events!');
},
}
}); 

较短的版本

$('#calendar').fullCalendar({
events: '<?php echo site_url('Test/Test_Function'); ?>'
});

选项:事件作为函数。Test_Function()可以按原样使用,但字符串$data被双引号括起来," "因此您的$data变量不会被解释为字符串,并且很可能 php 会抛出另一个答案中提到的错误。若要解决此问题,请将周围的双引号" "更改为' '或将转义序列"到字符串内的所有双引号,以便将其解释为字符串的整个内容。修复$data后,您可以通过以下方式创建事件 jQuery:

$('#calendar').fullCalendar({
events: function(start, end, timezone, callback) {
$.ajax({
url: '<?php echo site_url('Test/Test_Function'); ?>',
type: 'POST',
data: {
value1: 'aaa',
value2: 'bbb'
},
success: function(data) {
var events = [];
/* loop though all the events received from php and parse them and push them into an array
var event = {
id = data[index]
events.push(...);
*/
callback(events);
},
error: function() {
alert('there was an error while fetching events!');
},
});
}
}); 

更新
我正在使用FullCalendar v2.7.3和jQuery v2.1.4。下面是我用来获取事件的代码

Test_Function()在 PHP 中

<?php
Test_Function();
function Test_Function()
{
// Event id;
// event.title is the only place where you can "display" text;
// event.start: What date should the event be placed
$data = array
(
array
(
'id' => 1234, 
'title' => "Mr Name xxxxxnDate 2016-06-04T00:00:00nBirthdaynemail sasa@asfda.comnTelephone 1234567890",
'start' => "2016-06-06"
),
);
// add more array(...events...) as required
echo json_encode($data);        
}
?>

在 html 中。根据您的设置更改事件网址。

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<link href='../fullcalendar.css' rel='stylesheet' />
<link href='../fullcalendar.print.css' rel='stylesheet' media='print' />
<script src='../lib/moment.min.js'></script>
<script src='../lib/jquery.min.js'></script>
<script src='../fullcalendar.min.js'></script>
<script>
$(document).ready(function() {
$('#calendar').fullCalendar({
events: {
url: './php/test-function.php',
type: 'POST',
data: {
value1: 'aaa',
value2: 'bbb'
},
success: function(data) {
console.log(data);
},
error: function() {
alert('there was an error while fetching events!');
},     
}
});
});
</script>
<style>
body {
margin: 40px 10px;
padding: 0;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
font-size: 14px;
}
#calendar {
max-width: 900px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id='calendar'></div>
</body>
</html>

在PHP过程中,您必须回显json编码的字符串或任何其他字符串,但字符串除外。AJAX 需要字符串,但字符串格式不正确。您可以手动输入example.tld/[index.php/]controller_name/test_function的 URL 进行检查(方括号部分是以防您在 apache 中没有设置重写规则的情况),看看什么是回显或输出。如果您在 Ubuntu 中有解析错误,可以默认在/var/log/apache2/error.log中检查,但您可以检查它并在需要时在/etc/php5/apache2/php.ini中重新配置。位置取决于您的操作系统。您应该将$data字符串包装成单引号,而不是用双引号括起来。有效输出如下所示:

$data = '{"Results":{"Results":[{"Title":"Mr","Name":"xxxxx","Date":"2016-06-04T00:00:00","Data":"Birthday","email":"sasa@asfda.com","Telephone":1234567890},{"Title":"Mr","Name":"yyyyy","Date":"2016-06-05T00:00:00","Data":"Birthday","email":"qwee@rrrrr.com","Telephone":7412589630},{"Title":"Mrs","Name":"zzzzzzz","Date":"2016-06-07T00:00:00","Data":"Anniversary","email":"asdaf@qwe.com","Telephone":1234567890}]}}';
echo $data;

您可以在eventRendar函数中附加json数据。

eventRender: function (event, element) {    
element.find('.fc-content').parent().append('<span>'+event.name+'</span>');
}

而不是使用echo $data使用将自动返回所需值的return $data

最新更新