Jump to:

22993 Posts in 11843 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » DatePicker in GridField SS 3

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
Go to End
Author Topic: 1504 Views
  • quanto
    Avatar
    Community Member
    79 Posts

    DatePicker in GridField SS 3 Link to this post

    Hi All,

    I'm trying to get a DatePicker in my GridField. The Datefield DataObject is of the type Date. It seams also to be that getCMSFields_forPopup doesn't work at all?

    Agenda.php:

    class Agenda extends Page {
    public static $has_many = array(
    'Optredens' => 'Optreden'
    );

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

    $gridFieldConfig = GridFieldConfig::create()->addcomponents(
    new GridFieldToolbarHeader(),
    new GridFieldAddNewButton('toolbar-header-right'),
    new GridFieldSortableHeader(),
    new GridFieldDataColumns(),
    new GridFieldPaginator(10),
    new GridFieldEditButton(),
    new GridFieldDeleteAction(),
    new GridFieldDetailForm()
    );

    $gridField = new GridField("Optredens", "Optredens:", $this->Optredens(), $gridFieldConfig);

    $gridField->getConfig()->getComponentByType('GridFieldDataColumns')->setFieldCasting(
    array(
    'Datum' => 'Date'
    )
    );
    $fields->addFieldToTab("Root.Optredens", $gridField);
    return $fields;
    }
    }
    ...

    Optreden.php

    class Optreden extends DataObject{
    static $db = array(
    'Locatie' => 'Varchar(100)',
    'Plaats' => 'Varchar(100)',
    'Datum' => 'Date',
    'Tijd' => 'Varchar(15)',
    'Omschrijving' => 'HTMLText'
    );

    static $has_one = array(
    'Agenda'=>'Agenda'
    );

    public static $summary_fields = array(
    'Datum'=>'Datum',
    'Locatie'=>'Locatie',
    'Plaats'=>'Plaats'
    );

    function getCMSFields_forPopup() {
    $fields = new FieldList();
    $fields->push(TextField('Locatie','Locatie (feest naam)'));
    $fields->push(TextField('Plaats','Plaats (dorp / stad)'));
    $fields->push(DateField('Datum', 'Datum'));
    $fields->push(TextField('Tijd', 'Tijd'));
    $fields->push(TextareaField('Omschrijving', 'Omschrijving'));
    return $fields;

    }
    }

  • Optic Blaze
    Avatar
    Community Member
    153 Posts

    Re: DatePicker in GridField SS 3 Link to this post

    I had a similar problem that i managed to fix.

    I however did not use the 'function getCMSFields_forPopup()' syntax but used the following:

       function getCMSFields() {
       // Date field is created that displays calendar   
       $dateField = new DateField('Datum');
       $dateField->setConfig('showcalendar', true);

       $fields = new FieldSet(                  
       $dateField
       );
       return $fields;
       }

    When i used the above code in my app the grid field worked very well.

    You can also set the default fields that show up in the grid field with the following code.
    If we wanted to show 'Locatie','Datum' and 'Plaats' as default fields in grid field we do the following in the
    Optreden.php file

    // Sets the sumary fields that will be displayed in teh grid field
    static $summary_fields = array(
    'ID',
    'Locatie',
    'Datum',
    'Plaats',
    );

    Hope it helps

  • quanto
    Avatar
    Community Member
    79 Posts

    Re: DatePicker in GridField SS 3 Link to this post

    Hi Optic Blaze,

    Thanks for your answer. I think your solution works well for v2.4.x, but not for SS 3.x (FieldSet is deprecated).

    In 3.x DateField wil be called automatically when your dbfield is set Date (or DateTime).

    The error was in /framwork/javascript/DateField.js, line 12:

    if(!config.showcalendar) return;

    I commented that line out and the DatePicker works fine by now

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: DatePicker in GridField SS 3 Link to this post

    Hi quanto,

    Although your solution works for you, it shouldn't be necessary. Afaik the DatePicker doesn't necessarily show in the DateField by default in 3.0 - you should still explicitly set it, just like in Optic Blazes' example, the same way you set min/max values and determine the dateformat:

    function getCMSFields() {
       $fields = new FieldList(
          ...
          $dateField = new DateField('Datum', 'Datum'),
          ...
       );
       $dateField->setConfig('showcalendar', true);

       // optional:
       $dateField->setConfig('dateformat','dd-MM-yyyy');
       $dateField->setConfig('min','2012-11-01');
       $dateField->setConfig('max','2012-11-15');

       return $fields;
    }

    If you don't it's still a datefield, with validation, but without the datepicker

    Cheers, Martine

  • Andy01
    Avatar
    Community Member
    3 Posts

    Re: DatePicker in GridField SS 3 Link to this post

    Hello,

    I had the same problem in my current installation 3.0.2.
    With the code you supplied I am now able to enter a date with the date picker

    Unfortunally if I have entered a dataset I could not see my cms page again, cause of an error?!

    Is ist possible that the grid has a problem with displaying date values in the table ?

    Where could I find informations about the error - log ?

    Regards

    Andy

  • Optic Blaze
    Avatar
    Community Member
    153 Posts

    Re: DatePicker in GridField SS 3 Link to this post

    Hi there.

    To check for errors you need to do the following:

    1) Make sure that you are in dev mode. In your _config file make sure you have the following line:

    //Set development mode //
    Director::set_environment_type("dev");

    2) I assume you are using wamp or mamp.
    a) click on the wamp icon in your system tray
    b) click on php and then php settings
    c) select 'display errors'

    This should then show us where the problem is. It could just be a formatting error

  • Andy01
    Avatar
    Community Member
    3 Posts

    Re: DatePicker in GridField SS 3 Link to this post

    Hi Optic Blaze,

    now I see a popup with the following message:

    Error at line 661 of D:\programme\xampp\htdocs\SilverStripe\framework\core\Object.php

    Looking at the Object.php -->
    throw new Exception("Object->__call(): the method '$method' does not exist on '$this->class'", 2175);

    snip ..

    Trace

    Object->__call(Termin,Array)
    DataObject.php:2575
    Termin->Termin()
    DataObject.php:2575
    DataObject->relField(Termin.Title)
    GridField.php:447
    GridField->getDataFieldValue(Termin,Termin.Title)
    GridFieldDataColumns.php:133
    GridFieldDataColumns->getColumnContent(GridField,Termin,Termin.Title)
    GridField.php:417
    GridField->getColumnContent(Termin,Termin.Title)
    GridField.php:314
    GridField->FieldHolder()


    ..snip

    with my Termin defined as

    <?php
    class Termin extends DataObject {
    static $db = array(
    'FirstDate' => 'Date',
    'DateRange' => 'Varchar',
    'Description' => 'HTMLVarchar'
    );
    static $has_one = array(
    'JahrsTermine' => 'JahrsTerminePage'
    );

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

    $dateField = new DateField('FirstDate');
    $dateField->setConfig('showcalendar', true);
    $dateField->setConfig('dateformat','dd.MM.yyyy');
    $fields->push( $dateField);
    $fields->push( new TextField('DateRange','Termin(e)'));
    $fields->push( new HTMLEditorField('Description','Beschreibung'));
          

    return $fields;
    }
    }

    Regards Andy

  • Optic Blaze
    Avatar
    Community Member
    153 Posts

    Re: DatePicker in GridField SS 3 Link to this post

    You need to post your code that you used the datepicker . It sounds like you referenced something that does not exist in the code.

    1504 Views
Page: 1 2
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.