最近使用dhtmlx的scheduler来做一个插件.

觉得这个东东确实是非常好用的.

这里我主要说明一下如何使用json来作为scheduler的数据源.默认情况下是使用xml来做数据源的,之前我也是用xml但是发现数据的实时性还是会存在问题.而且在生成xml中也容易出问题.

1.先来看看基本配置吧.

   
   
   
   
  1. "text/javascript" charset="utf-8"
  2.     $(function(){ 
  3.         scheduler.config.prevent_cache = false
  4.         scheduler.config.lightbox.sections=[     
  5.             {name:"description", height:70, map_to:"text", type:"textarea" , focus:true}, 
  6.             {name:"time", height:72, type:"time", map_to:"auto"
  7.              
  8.         ] 
  9.         scheduler.config.first_hour=7; 
  10.         scheduler.config.time_step=30; 
  11.         scheduler.config.api_date="%Y-%m-%d %H:%i"
  12.         scheduler.config.xml_date="%Y-%m-%d %H:%i"
  13.         scheduler.config.hour_date="%H:%i"
  14.         scheduler.config.default_date="%Y年%m月%d日"
  15.         scheduler.config.month_date="%Y年 %m月"
  16.         scheduler.config.day_date="%D %m月%d日"
  17.         scheduler.config.start_on_monday=true
  18.         scheduler.config.details_on_create=true
  19.         scheduler.config.details_on_dblclick=true;  
  20.         scheduler.init('scheduler_here',new Date(format('Y') ?>,format('m') - 1 ?>,format('d') ?>),"week"); 
  21.         var convert = scheduler.date.date_to_str("%Y-%m-%d %H:%i:%s"); 
  22.         var dateConvert = scheduler.date.date_to_str('%Y-%m-%d'); 
  23.         var equipment_number = 
  24.         //载入文件 
  25.         scheduler.load("/lms/lmsReg/generateEquipmentRegInfoJson?equipment_number="+equipment_number,"json");   
  26.         //以下代码为绑定事件:条目变化(拖拉事件、调整时间) 
  27.         scheduler.attachEvent("onEventChanged"function(event_id,event_object){ 
  28.         }); 
  29.         //以下代码为绑定事件:增加 
  30.         scheduler.attachEvent("onEventAdded"function(event_id,event_object){ 
  31.             var startdate = convert(event_object.start_date); 
  32.             var enddate = convert(event_object.end_date); 
  33.             var title = event_object.text; 
  34.             var date = dateConvert(event_object.end_date); 
  35.             var equipment_number = 
  36.             $.post('ajaxReturnNextRegStamp',function(regstamp){ 
  37.                 $.post('regEquipmentCreate',{equipment_number: equipment_number,starttime: startdate,endtime: enddate,date: date,title: title,regstamp:regstamp},function(data){ 
  38.                     if(data == 'success') { 
  39.                         $.messager.show({ 
  40.                             title: '消息'
  41.                             msg: "设备预约成功!"
  42.                             showType: 'fade'
  43.                             timeout: 2000 
  44.                         }); 
  45.                         setTimeout(window.location.reload,3000); 
  46.                         scheduler.addEvent(startdate,enddate,title,event_id); 
  47.                     } 
  48.                     if(data == 'error'){ 
  49.                         $.messager.alert('错误','时间冲突','error'); 
  50.                         scheduler.deleteEvent(event_id); 
  51.                     } 
  52.                     if(data != "success" && data != "error"){ 
  53.                         $.messager.alert('错误','抱歉!您的预约没有成功!
    错误原因: '
    +data+'','error'); 
  54.                         scheduler.deleteEvent(event_id); 
  55.                     } 
  56.                 }); 
  57.             }); 
  58.         }); 
  59.         scheduler.attachEvent('onEventDeleted',function(event_id,event_object){ 
  60.             var equipment_number = 
  61.             $.post('delReg',{regstamp: event_id});  
  62.         }); 
  63.         
  64.     }) 
  65.  

2.载入json

   
   
   
   
  1. //载入文件  
  2.         scheduler.load("/lms/lmsReg/generateEquipmentRegInfoJson?equipment_number="+equipment_number,"json");   

注意最后面要定义为json格式.

3.json的格式:

   
   
   
   
  1. {"id":"1","start_date":"2012-09-28 08:30:00","end_date":"2012-09-28 13:30:00","text":"\u7533\u8bf7\u4eba: \u8d85\u7ea7\u7ba1\u7406\u5458 
  2. \u7533\u8bf7\u5185\u5bb9: \u65b0\u9884\u7ea6","details":"admin"}