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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

DatePicker in GridField SS 3


Go to End


10 Posts   3924 Views

Avatar
quanto

Community Member, 91 Posts

6 September 2012 at 10:39pm

Edited: 07/09/2012 1:41am

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

Avatar
Optic Blaze

Community Member, 190 Posts

11 September 2012 at 11:22am

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

Avatar
quanto

Community Member, 91 Posts

12 September 2012 at 12:33am

Edited: 12/09/2012 12:34am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

18 September 2012 at 4:33am

Edited: 18/09/2012 4:35am

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

Avatar
Andy01

Community Member, 3 Posts

12 October 2012 at 1:23am

Edited: 12/10/2012 1:24am

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

Avatar
Optic Blaze

Community Member, 190 Posts

12 October 2012 at 1:38am

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

Avatar
Andy01

Community Member, 3 Posts

12 October 2012 at 4:30am

Edited: 12/10/2012 4:40am

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

Avatar
Optic Blaze

Community Member, 190 Posts

12 October 2012 at 4:36am

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

Go to Top