我在控制Full Calendar模块时遇到了一些麻烦。目前我拥有它,以便日历getEvents方法联系一个SQL表并返回用户的所有事件 - 该部分工作正常。
我想添加的功能是允许用户编辑/删除事件,并将这些更改反映在数据库中!我的意思是在我的表中,用户可以拖放事件来改变他们的时间,当他们点击一个事件时,我希望出现一个对话框,询问他们是否要删除此事件。我希望这些更改能够在SQL表中表示。
我该怎么做?我是JQuery,JavaScript和DatePicker的新手。从我的谷歌搜索和尝试学习,我发现了一个类似的线程here
function (calEvent) {
removeRequestedEvent($(this), calEvent);
},
It just passes in the calendar event and the calendar itself.
removeRequestedBooking: function (cal, calEvent) {
if (!confirm("Delete?"))
return;
cal.fullCalendar("removeEvents", calEvent.id);
cal.fullCalendar("rerenderEvents");
// Re-show draggable element
$("#requests #" + calEvent.id).show();
}
它提供了这段代码,我认为它与我需要的类似,但是我希望在调用removeEvents时从数据库中删除该事件。我假设我需要一些类似于从数据库中检索事件时的代码(代码如下所示),但我不确定代码应该如何构造。任何人都可以帮我解决这个问题吗?
var db = Database.Open("users");
var result = db.Query("SELECT * FROM events");
var data = result.Select(x => new
{
id = x.id,
title = x.title,
start = x.start.ToString("s"),
end = x.end.ToString("s"),
allDay = false
}).ToArray();
Json.Write(data, Response.Output);
Response.ContentType = "application/json";
答案 0 :(得分:3)
我通过AJAX调用从日历和数据库中删除事件。这是一些示例代码
点击活动
eventClick: function(event){
var start = $.fullCalendar.formatDate(event.start, "yyyy-MM-dd HH:mm");
var end = $.fullCalendar.formatDate(event.end, "yyyy-MM-dd HH:mm");
var id = event.id;
var title = event.title;
$("#edit_start").val(start); //this just populates the value into my dialog form
$("#edit_end").val(end);
$("#edit_title").val(title);
$("#edit_event_id").val(id);
$("#edit_class" ).dialog( "open" ); //open the dialog
这是对话框信息
$( "#edit_class" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Delete Class": function() {
var event_id = $("#edit_event_id").val();
$.ajax({
type:"POST",
url: "delete_class.php",
data: "event_id=" + event_id,
});
$('#calendar').fullCalendar('refetchEvents'); //the event has been removed from the database at this point so I just refetch the events
$( this ).dialog( "close" );
},
},
});
编辑打开对话框时显示的类div
<div id="edit_class" title="Edit Class">
<form action="">
<fieldset>
</select>
<p>
</p>
<label for="edit_start">Start</label>
<input type="text" name="edit_start" id="edit_start" class="text ui-widget-content ui-corner-all" />
<p>
</p>
<label for="edit_end">End</label>
<input type="text" name="edit_end" id="edit_end" class="text ui-widget-content ui-corner-all" />
<p>
</p>
<label for="title">Class Name</label>
<input type="text" name="edit_title" id="edit_title" class="text ui-widget-content ui-corner-all" />
<p>
</p>
<label for="edit_event_id"></label>
<input type="hidden" name="edit_event_id" id="edit_event_id" class="text ui-widget-content ui-corner-all" />
</fieldset>
然后在delete_class.php页面上,我有类似下面的内容
$event_id = $_POST['event_id'];
try
{
$dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare(
"DELETE FROM events
WHERE event_id = :event_id ");
$stmt->bindParam(':event_id', $event_id, PDO::PARAM_STR);
$stmt->execute();
}
catch(Exception $e)
{
echo ("error");
}
答案 1 :(得分:0)
执行此操作的最佳方法是将AJAX与jQuery一起使用
function deleteEvent(id)
{
// The URL to which we will make the AJAX call
var url = "MyCalendar.aspx";
// Setup the data to send to the server
var sendData =
{
"action":"deleteEvent",
"id":id
};
// Make the AJAX call
var xhr = $.post(url, sendData, function(result)
{
// The response is up to the method you implement in the server side, be it json or text.
// For simplicity's sake let's assume you return a '0' for OK or '1' for ERROR
if(result == '0')
{
// Remove the event from the calendar since we know it all went well server-side
cal.fullCalendar("removeEvents", id);
cal.fullCalendar("rerenderEvents");
}
else
{
// There was an error server-side, put a message or something...
alert("Could not remove event. Try again later.");
}
});
xhr.error = function()
{
// There was an error trying to complete the request
alert("Could not complete request.");
}
}
在服务器端,我假设您将使用相同的页面来处理AJAX请求,在这种情况下,您将在PageLoad事件中执行类似的操作:
protected void Page_Load(object sender, EventArgs e)
{
// Check if we received a POST value with name 'action'
string action = Request["action"];
if(action != null)
{
// It's an AJAX Call
if(action == "deleteEvent")
{
// At this point we should expect a POST value with name 'id'
int id = int.Parse(Request["id"]);
// Execute action
DoActionDeleteEvent(id);
// Do nothing else since it's an AJAX call
return;
}
}
}
private void DoActionDeleteEvent(int id)
{
Response.ContentType = "text/plain";
try
{
// ToDo: Delete the event from the database
// All went well, write 0 in the response.
Response.Write("0");
}
catch
{
// There was an error, write 1 in the response
Response.Write("1");
}
// End the response
Response.End();
}
这样,所有更改都应反映在数据库和日历中。
至于版本,你会做一些非常相似的事情,但不会返回0或1,而是返回一个带有新编辑事件的JSON对象。