gpt4 book ai didi

java - FullCalendar - 添加多个事件或添加事件源

转载 作者:行者123 更新时间:2023-11-30 07:16:15 25 4
gpt4 key购买 nike

我已经浏览了 stackoverflow 中将事件添加到 FullCalendar 中的帖子,但是我真的很新,如果没有示例,我真的很难理解。简而言之,这里有人可以为我简化它,以便将对象数组添加到 FullCalendar 中吗?

我想添加我创建的约会(约会日期、字符串名称、字符串电话号码)。所以它们在列表中被检索:

 PersistenceManager pm = PMF.get().getPersistenceManager();
String query = "select from " + Appointment.class.getName();
query += " where merchant == '" + session.getAttribute("merchant") + "'";
List<Appointment> appointment = (List<Appointment>) pm.newQuery(query).execute();

我怎样才能用我获得的列表填充 FullCalendar 插件?非常感谢!

最佳答案

如果有人遇到与我相同的问题 - 您有一个 java 对象列表并希望它填充 FullCalendar,这是解决方案:

JSP 页面

$(document).ready(function() {

var calendar = $('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
selectable: true,
selectHelper: true,

select: function(start, end, allDay) {
var title = prompt('Event Title:');
if (title) {
calendar.fullCalendar('renderEvent',
{
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
}
calendar.fullCalendar('unselect');
},
editable: true,

eventSources: [
{
url: '/calendarDetails',
type: 'GET',
data: {
start: 'start',
end: 'end',
id: 'id',
title: 'title',
allDay: 'allDay'
},
error: function () {
alert('there was an error while fetching events!');
}
}
]
});
});

请不要拿走 URL,它是 servlet URL

小服务程序

    public class CalendarServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {

String something = req.getSession().getAttribute("merchant").toString(); //get info from your page (e.g. name) to search in query for database

//Get the entire list of appointments available for specific merchant from database

//Convert appointment to FullCalendar (A class I created to facilitate the JSON)
List<FullCalendar> fullCalendar = new ArrayList<FullCalendar>();
for (Appointment a : appointment) {
String startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getDate());
startDate = startDate.replace(" ", "T");

//Calculate End Time
Calendar c = Calendar.getInstance();
c.setTime(a.getDate());
c.add(Calendar.MINUTE, 60);
String endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
endDate = endDate.replace(" ", "T");

FullCalendar fc = new FullCalendar(startDate, endDate, a.getId(), a.getName() + " @ " + a.getPhone(), false);
fullCalendar.add(fc);
}

//Convert FullCalendar from Java to JSON
Gson gson = new Gson();
String jsonAppointment = gson.toJson(fullCalendar);

//Printout the JSON
resp.setContentType("application/json");
resp.setCharacterEncoding("UTF-8");
try {
resp.getWriter().write(jsonAppointment);
} catch (IOException e) {
e.printStackTrace();
}
}
}

如果您需要有关 JSON 或 GS​​ON 的更多信息,请查看上面的评论。

关于java - FullCalendar - 添加多个事件或添加事件源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17162554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com