Jump to:

22992 Posts in 11842 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Date Picker in ModelAdmin search fields?

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

Page: 1 2 3
Go to End
Author Topic: 3412 Views
  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Date Picker in ModelAdmin search fields? Link to this post

    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);

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Date Picker in ModelAdmin search fields? Link to this post

    In your model, override scaffoldSearchFields() like so:

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

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Date Picker in ModelAdmin search fields? Link to this post

    In your model, override scaffoldSearchFields() like so:

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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Date Picker in ModelAdmin search fields? Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Date Picker in ModelAdmin search fields? Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Date Picker in ModelAdmin search fields? Link to this post

    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).

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Date Picker in ModelAdmin search fields? Link to this post

    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
  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Date Picker in ModelAdmin search fields? Link to this post

    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.

    3412 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.