【问题标题】:FullCalendar - Adding Multiple Events or Add Event SourceFullCalendar - 添加多个事件或添加事件源
【发布时间】:2013-06-14 07:03:53
【问题描述】:

我查看了 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 jquery fullcalendar


【解决方案1】:

如果有人遇到与我相同的问题 - 你有一个 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

Servlet

    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 的更多信息,请查看上面的 cmets。

【讨论】:

    【解决方案2】:

    Melvin,您在 Stack 中有大量示例,请尝试搜索添加事件源。

    根据我的完整日历经验,您可以通过 JSON、格式良好的 XML 和数组添加事件,我认为就是这样。您可以使用 ajax 调用来做 3 种格式的检索。

    在你的服务器端,你应该创建一个方法来返回一个已经构建好的 XML/JSON/array 的字符串,这样你就可以传递给你的 ajax 调用。

    【讨论】:

    • 感谢您的回复,所以您的意思是我不能简单地使用我拥有的 java 列表(即约会),而必须通过 JSON 添加事件?
    • 就是这样,您不能只是简单地传递 Java 列表,您必须将该列表转换为可接受的完整日历格式( JSON、XML、Array )。如果你在你的 servlet 中弄乱了 Java,返回已经完成的字符串,或者你可以有一个文件 myfile.json,我看到这个问题中已经有一些例子,但是如果你需要另一个让我知道 ;)...跨度>
    • 嗨,Henrique,非常感谢。我已将列表转换为 JSON,更新了问题以防其他人也想知道。所以我知道我必须有一个 JSON 提要页面才能让 FullCalendar 工作?在打扰你们之前,我会再研究一下:)
    • 网站 (arshaw.com/fullcalendar/docs/event_data/events_json_feed) 声明我需要从 JSON 提要中获取事件。提要是否可能在同一页面上,而不是单独的页面上?
    • 如果一个单独的页面是必须的,任何人都知道如何使用我的 java 数组中的信息创建一个 php?
    【解决方案3】:

    看看https://github.com/mzararagoza/rails-fullcalendar-icecube 这是在 Rails 中完成的,但我认为您正在寻找的是

    dayClick: function(date, allDay, jsEvent, view) {
              document.location.href=new_event_link + "?start_date=" + date;
    },
    

    完整的jquery

    $('#calendar').fullCalendar({
            dayClick: function(date, allDay, jsEvent, view) {
              document.location.href=new_event_link + "?start_date=" + date;
            },
              header: {
                  left: 'prev,today,next',
                  center: 'title',
                  right: 'month,agendaWeek,agendaDay'
              },
              selectable: true,
              selectHelper: true,
              editable: false,
              ignoreTimezone: false,
              select: this.select,
              eventClick: this.eventClick,
              eventDrop: this.eventDropOrResize,
              eventSources: [
                {
                    url: '/event_instances.json',
                    data: {
                        custom_param1: 'something',
                        custom_param2: 'somethingelse'
                    },
                    error: function() {
                        alert('there was an error while fetching events!');
                    }
                }
              ],
    
              eventResize: this.eventDropOrResize
          });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多