Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Date Picker in ModelAdmin search fields?


Go to End
Reply


17 Posts   3935 Views

Avatar
swaiba

Forum Moderator, 1805 Posts

7 October 2010 at 10:21pm

Hi,

If I set $summary_fields with a field that is enumerated then the search fields contain a drop down of values. If I set with a field that is of type 'Date' it stays as a text field. Is there a way to change it to a date picker?

Barry

p.s. I have worked out how to do this within the record editor within the dataobject getCMSFields...

$df = new CalendarDateField(<FieldName>, <Label>);
$fields->replaceField(<FieldName>, $df);

Avatar
dalesaurus

Community Member, 283 Posts

9 October 2010 at 6:36am

In your model, override scaffoldSearchFields() like so:

public function scaffoldSearchFields() {
   $fields = parent::scaffoldSearchFields();
   // Do your same replace code here on $fields
   return $fields;
}

Avatar
dalesaurus

Community Member, 283 Posts

9 October 2010 at 6:36am

In your model, override scaffoldSearchFields() like so:

public function scaffoldSearchFields() {
   $fields = parent::scaffoldSearchFields();
   // Do your same replace code here on $fields
   return $fields;
}

Avatar
swaiba

Forum Moderator, 1805 Posts

9 October 2010 at 9:10am

Many thanks, That is going to improve my model admin's!

Avatar
swaiba

Forum Moderator, 1805 Posts

11 October 2010 at 10:22pm

Edited: 11/10/2010 11:15pm

Hello again...

As advised the calendar pop-up now appears, but... it is clipped to the area of the 'left' and is kinda unusable... I don't suppose anyone can recommend a solution to this please?

(see attached screen)

code to reproduce (using 2.4.0 and legacy date time fields (attached)...

class TestAdmin extends ModelAdmin
{
   static $managed_models = array(
      'MyObject'
   );

   static $url_segment = 'treeadmin';
   static $menu_title = 'TestAdmin';
}

class MyObject extends DataObject
{
   static $db = array(
      'Name' => 'Text',
      'DateFrom' => 'Date',
   );

   public static $summary_fields = array (
      'Name' => 'Name',
      'DateFrom'=>'From',
   );

   public function scaffoldSearchFields()
   {
      $fields = parent::scaffoldSearchFields();

      $df = new CalendarDateField('DateFrom', 'From Date:');
      $fields->replaceField('DateFrom', $df);

      return $fields;
   }
}

Thanks in advance...

Barry

Avatar
swaiba

Forum Moderator, 1805 Posts

11 October 2010 at 11:40pm

Actually, do not worry I've got this working fine with the new version 2.4.2 without the clipping and using legacy datetimefileds... are this now stable for the UK locale? there were problems with certain dates back in 2.4.0 (which is why I'm using legacy date module).

Avatar
swaiba

Forum Moderator, 1805 Posts

12 October 2010 at 1:15am

right, so in the continuing series of messages on this I have now upgraded to 2.4.2 and am trying to get date and time drop downs working for en_GB local... this is what I currently have but it is not working... and by that I mean...

I've got the date picker working, BUT I had to change the site dateformat (and locale) to this... how do I edit the http://doc.silverstripe.org/datefield page to reflect this?
(as the dateformat suggested errors for me by not creating the day correctly (looks like day in Julian format).

The time picker though is not showing properly, it shows a bit of the scrollbar and nothing else! (see attached screen, note it shows scrolls for the form panel and then a bit on the right for the time field))

class TestAdmin extends ModelAdmin
{
   static $managed_models = array(
      'MyObject'
   );

   static $url_segment = 'testadmin';
   static $menu_title = 'TestAdmin';
}

class MyObject extends DataObject
{
   static $db = array(
      'Name' => 'Text',
      'DateFrom' => 'Date',
      'TimeFrom' => 'Time',
   );

   public static $summary_fields = array (
      'Name' => 'Name',
      'DateFrom'=>'DateFrom',
      'TimeFrom'=>'TimeFrom',
   );

   public function scaffoldSearchFields()
   {
      $fields = parent::scaffoldSearchFields();

      $df = new DateField('DateFrom','DateFrom');
      $df->setConfig('showcalendar', true);
      $df->setLocale('en_GB');
      $df->setConfig('dateformat', 'YYYY-mm-dd');
      $fields->replaceField('DateFrom', $df);

      $tf = new TimeField ('TimeFrom','TimeFrom');
      $tf->setConfig('timeformat', 'h:m a');
      $tf->setConfig('use_strtotime', true);
      $tf->setConfig('showdropdown',true);
      $fields->replaceField('TimeFrom', $tf);

      return $fields;
   }
}

Tested in firefox & safari

Attached Files
Avatar
dalesaurus

Community Member, 283 Posts

12 October 2010 at 3:13am

This is a CSS overflow issue, and should be reported as a bug at http://open.silverstripe.org. It will take some extra care as the layout used in the LeftAndMain requires more positioning.

For the time being you will probably have to override some of the positioning CSS to make the picker behave. Although if you are already using the legacy DateTimeFields, it will probably be easier to switch it to DMY fields ($dateField->setConfig('dmyfields', true);).

Also, I have dived deep in the Date/Time pickers in 2.4.2 recently. You must be sure you are setting your i18n::setLocale('en_GB'); in one of your _config.php files. This will make all date fields appropriately localized instead of having to do it manually on each field as outlined below.

Go to Top