Export data from the scheduler

Base prepartions

To activate such functionality you need to include dhtmlxscheduler_serialize.js

 
<script src='codebase/ext/dhtmlxscheduler_serialize.js'></script>

Export to XML

You may use the toXML method to serialize scheduler data to xml string:

 
var xml = scheduler.toXML(); //xm string

Export to JSON

It is also possible to get scheduler events in JSON format. Use toJSON in this case:

 
var json_string = scheduler.toJSON(); //json string

Warning - method returns json string, not object. If you need an object - use getEvents method

Export to iCal

Serialization to iCal string requires toICal method. The next command and both methods, that are described above, can be used at any moment of time.

 
var ical_string = scheduler.toICal();

Also, there is an external script for iCal import-export operations

Attributes in serialization

The following technique is not applicable for the iCal format.

By default export will include only standard attributes(properties)

  1. id
  1. text
  1. start_date - format of seriaization is defined by scheduler.config.xml_date
  1. end_date

If you need to include some custom attributes, you can redefine data_attributes method, in simplest case it will look as

 
scheduler.data_attributes = function(){
 
	return [
 
		["id"],["text"],["date_start"],["date_end"],
 
		["custom_attribute"]
 
	];
 
};

Basically, the method defines the list of attirubute names.

But you can define the formatting function, which describes how attribute data needs being processed before serialization.

It can be useful for dates those need converting before to be placed in XML

 
scheduler.data_attributes = function(){
 
return [
 
	["id"],
 
	["text"],
 
	["start_date",scheduler.templates.xml_format],
 
	["end_date",scheduler.templates.xml_format]];
 
}

Serializing recurring events

Below technique is not applicable for the iCal format.

If you are using “recurring” extension, you need to define extra attributes, which are used by recurring events

 
scheduler.data_attributes = function(){
 
    var empty = function(a){ return a||""; }
 
    return [["id"],
 
			["text"],
 
			["start_date",scheduler.templates.xml_format],
 
			["end_date",scheduler.templates.xml_format],
 
			["rec_type",empty],
 
			["event_length",empty],
 
			["event_pid",empty]];
 
}

DB-less data saving

Serialization allows to implement the simple routine of data saving, which doesn't require DB - data will be stored in XML file.

(1) Firstly, you need to include serialization extension

 
<script src='codebase/ext/dhtmlxscheduler_serialize.js'></script>

(2) The next step - place hidden form for data saving

 
<form id="xml_form" action="xml_writer.php" method="post" target="hidden_frame" accept-charset="utf-8">
 
	<input type="hidden" name="data" value="" id="data">
 
</form>

(3) Then you need to place “Save” button on the page

 
<input type="button" name="save" value="save" onclick="save()" style="right:300px; width:80px; position:absolute; top:1px;">
 
function save(){
 
	var form = document.getElementById("xml_form");
 
	form.elements.data.value = scheduler.toXML();
 
	form.submit();
 
}

(4) on server side, you just need to write the data in an existent file. xml_writer.php could be as follows:

 
<?php
 
	file_put_contents("./data.xml",$_POST["data"]);
 
?>

the empty data.xml is:

 
<data></data>

That is all, now scheduler can be loaded from data.xml, pressing “save” button will serialize the current state of the scheduler to xml and save back to the file.

So on next loading the scheduler will load events those have been saved previously.

Related samples

In the scheduler package you can find ready samples:

  • dhtmlxScheduler\samples\04_export\01_serialize_xml.html
  • dhtmlxScheduler\samples\04_export\02_serialize_json.html
  • dhtmlxScheduler\samples\04_export\03_serialize_ical.html
  • dhtmlxScheduler\samples\04_export\05_serialize_recurring.html

Troubleshooting

If, during data saving, you encounter unwanted data escaping (\” in the saved data) - be sure that “magic_quotes” is disabled in php configuration.