Created
August 4, 2011 07:46
-
-
Save Zauberfisch/1124672 to your computer and use it in GitHub Desktop.
Silverstripe DateField jQueryUI Datepicker with advanced configs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add this to mysite/_config.php | |
Object::useCustomClass('DateField_View_JQuery', 'jQueryUIDateField_View'); | |
// mysite/code/jQueryUIDateField_View.php | |
class jQueryUIDateField_View extends DateField_View_JQuery { | |
function onBeforeRender() { | |
$Field = $this->getField(); | |
$format = self::convert_iso_to_jquery_format($Field->getConfig('dateformat')); | |
$conf = array( | |
'dateFormat' => $format | |
); | |
if($Field->getConfig('showcalendar')) | |
$conf['showcalendar'] = true; | |
if ($Field->getConfig('min')) | |
$conf['minDate'] = self::convert_iso_to_jquery_format($Field->getConfig('min')); | |
if ($Field->getConfig('max')) | |
$conf['maxDate'] = self::convert_iso_to_jquery_format($Field->getConfig('max')); | |
if (!empty($Field->jQueryConfig) && is_array($Field->jQueryConfig)) | |
foreach ($Field->jQueryConfig as $key=>$val) | |
$conf[$key] = $val; | |
if (!empty($Field->jQueryEvents) && is_array($Field->jQueryEvents)) { | |
$value_arr = array(); | |
$replace_keys = array(); | |
foreach($Field->jQueryEvents as $key => $value){ | |
// Store function string. | |
$value_arr[] = $value; | |
// Replace function string in $foo with a 'unique' special key. | |
$value = '%' . $key . '%'; | |
$conf[$key] = $value; | |
// Later on, we'll look for the value, and replace it. | |
$replace_keys[] = '"' . $value . '"'; | |
} | |
} | |
$conf = Convert::raw2json($conf); | |
if (!empty($Field->jQueryEvents) && is_array($Field->jQueryEvents)) { | |
// Replace the special keys with the original function. | |
$conf = str_replace($replace_keys, $value_arr, $conf); | |
} | |
$conf = str_replace('"', '\'', $conf); | |
$this->getField()->addExtraClass($conf); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Allows you to set all jQuery UI options and events.
Usage:
$DateField = new DateField('myDate');
$DateField->jQueryConfig = array(
'changeMonth' => true,
'changeYear' => true
);
$DateField->jQueryEvents = array(
'beforeShowDay' => "function(date) { /* do something with date */ }"
);