Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zauberfisch/1124672 to your computer and use it in GitHub Desktop.
Save Zauberfisch/1124672 to your computer and use it in GitHub Desktop.
Silverstripe DateField jQueryUI Datepicker with advanced configs
// 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);
}
}
@Zauberfisch
Copy link
Author

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 */ }"
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment