开发者可以使用FullCalendar创建强大的日历日程应用,FullCalendar提供了丰富的选项设置和方法调用,使得开发者可以轻松的创建各种功能的日历程序。
由于项目需求,需要通过日历的方式展现某些数据,在网上搜索了很多日历的插件,觉得比较好用的还是fullCalendar,下面是我写的一个简单Demo,如有不足,请多多指正!!!
一、引入必要的文件:
<link href="https://www.jb51.net/Scripts/Plugins/fullcalendar.css" /> <link href="https://www.jb51.net/Scripts/Plugins/jquery.ui.css" /> <link href="https://www.jb51.net/Scripts/Plugins/style.default.css" /> <script src="https://www.jb51.net/Scripts/Plugins/jquery-1.7.min.js"></script> <script src="https://www.jb51.net/Scripts/Plugins/jquery-ui-1.8.16.custom.min.js"></script> <script src="https://www.jb51.net/Scripts/Plugins/fullcalendar.min.js"></script> <script src="https://www.jb51.net/Scripts/Plugins/fullcalendar.js"></script>
二、在界面中添加div块:
<body> <div> </div> </body>
初始化calendar的js代码我把它放到了fullcalendar.js文件中接下来就让我们看一下这个文件里的脚本信息。
三、初始化fullcalender:
/* 初始化calendar */ 通过ID来初始化放到哪个标签里 jQuery('#calendar').fullCalendar({ header: { left: 'month,agendaWeek,agendaDay', center: 'title', right: 'today, prev, next' }, monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"], monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"], dayNames: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"], dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"], today: ["今天"], firstDay: 1, buttonText: { prev: '«', next: '»', prevYear: ' << ', nextYear: ' >> ', today: '今天', month: '月', week: '周', day: '日' }, viewDisplay: function (view) { //动态把数据查出,按照月份动态查询 var viewStart = $.fullCalendar.formatDate(view.start, "yyyy-MM-dd HH:mm:ss"); var viewEnd = $.fullCalendar.formatDate(view.end, "yyyy-MM-dd HH:mm:ss"); $("#calendar").fullCalendar('removeEvents'); 通过post 请求一般处理程序来返回需要展现的信息(start与end为需要传递的参数,在本文中没有用到可以忽略) $.post("../DateHandle/ViewData.ashx", { start: viewStart, end: viewEnd }, function (data) { var resultCollection = jQuery.parseJSON(data); $.each(resultCollection, function (index, term) { $("#calendar").fullCalendar('renderEvent', term, true); }); }); //把从后台取出的数据进行封装以后在页面上以fullCalendar的方式进行显示 }, dayClick: function (date, allDay, jsEvent, view) { }, loading: function (bool) { if (bool) $('#loading').show(); else $('#loading').hide(); }, //#region 数据绑定上去后添加相应信息在页面上(一开始加载数据时运行) eventAfterRender: function (event, element, view) { var fstart = $.fullCalendar.formatDate(event.start, "HH:mm"); var fend = $.fullCalendar.formatDate(event.end, "HH:mm"); var confbg = '<span></span>'; if (view.name == "month") {//按月份 var evtcontent = '<div><a>'; evtcontent = evtcontent + confbg; //evtcontent = evtcontent + '<span>' + fstart + " - " + fend + event.fullname + '</span>'; evtcontent = evtcontent + '<span>' + event.fullname + '</span>'; element.html(evtcontent); } else if (view.name == "agendaWeek") {//按周 var evtcontent = '<a>'; evtcontent = evtcontent + confbg; evtcontent = evtcontent + '<span>' + fstart + "-" + fend + '</span>'; evtcontent = evtcontent + '<span>' + event.fullname + '</span>'; element.html(evtcontent); } else if (view.name == "agendaDay") {//按日 var evtcontent = '<a>'; evtcontent = evtcontent + confbg; evtcontent = evtcontent + '<span>' + fstart + " - " + fend + '</span>'; element.html(evtcontent); } }, //#endregion //#region 鼠标放上去显示信息 eventMouseover: function (calEvent, jsEvent, view) { //var fstart = $.fullCalendar.formatDate(calEvent.start, "yyyy/MM/dd HH:mm"); //var fend = $.fullCalendar.formatDate(calEvent.end, "yyyy/MM/dd HH:mm"); //$(this).attr('title', fstart + " - " + fend + " " + calEvent.fullname); 鼠标悬浮到title的时候可以设置展现哪些信息 $(this).attr('title', calEvent.fullname); $(this).css('font-weight', 'normal'); // $(this).tooltip({ // effect: 'toggle', // cancelDefault: true // }); }, eventClick: function (event) { }, events: [], //#endregion editable: true, droppable: true, // this allows things to be dropped onto the calendar !!! drop: function (date, allDay) { // this function is called when something is dropped // retrieve the dropped element's stored Event Object var originalEventObject = jQuery(this).data('eventObject'); // we need to copy it, so that multiple events don't have a reference to the same object var copiedEventObject = jQuery.extend({}, originalEventObject); // assign it the date that was reported copiedEventObject.start = date; copiedEventObject.allDay = allDay; // render the event on the calendar // the last `true` argument determines if the event "sticks" () jQuery('#calendar').fullCalendar('renderEvent', copiedEventObject, true); // is the "remove after drop" checkbox checked? jQuery(this).remove(); } });
四:通过一般处理程序返回需要的json数据
首先新增任务类 Task: