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.

All other Modules /

Discuss all other Modules here.

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

Dropdown on gridfieldextension


Go to End


2 Posts   1668 Views

Avatar
Dobby

Community Member, 15 Posts

8 August 2016 at 6:24am

Edited: 08/08/2016 6:42am

I created a gridfield with the modul gridfieldextension to edit the records of a dataobject inline.
Unfortunately the documentation of the modul doesn't help.

I have the dataobject games which has_many goals. In the gridfield for a goal I want to edit the goals for one game. Here is a has_one with players for the player who scored the goal. But I have no idea how to put a dropdownfield for the scorer into the gridfield. Maybe someone can help.

Code for the games

<?php

class Spiele extends DataObject {
	
	private static $db = array(
	"Datum" => "Date",
	"Zeit" => "Time",
	"ToreHeim" => "Int",
	"ToreGast" => "Int",
	"Zuschauer" => "Int",
	"Gespielt" => "Boolean",
	"Stats" => "Boolean",
	"Spieldauer" => "Time"
	);
	
	private static $has_one = array(
	"Heim" => "Teams",
	"Gast" => "Teams",
	"Saison" => "Saison",
	"SpielTyp" => "SpielTyp",
	"ErgebnisArt" => "ErgebnisArt"
	);
	private static $has_many = array(
	"Tore" => "Tore",
	"Strafen" => "Strafen",
	"Aufstellung" => "Aufstellung"
	);

	private static $singular_name = "Spiel";
	private static $plural_name = "Spiele";


	static $default_sort = '';
	
	
	
	private static $searchable_fields = array(
	"Saison.Name"
	);
	
	private static $summary_fields = array(
	"Datum",
	"Zeit",
	"SpielTyp.Name",
	"Heim.Name",
	"Gast.Name"
	);
			 
function getCMSFields() {
		

		$fields = parent::getCMSFields();
		$fields->addFieldToTab("Root.Main", new CheckboxField("Gespielt","Gespielt"));
		$fields->addFieldToTab("Root.Main", new DropdownField("SpielTypID","Art des Spiels",SpielTyp::get()->Map("ID", "Name","-")));
		$fields->addFieldToTab("Root.Main", new TextField("Zuschauer","Zuschauer"));
		if ($this->SpielTypID == 1) {
		$fields->addFieldToTab("Root.Main", new DropdownField("HeimID","Heimteam",Teams::get()->Map("ID", "Name","-")));
		$fields->addFieldToTab("Root.Main", new DropdownField("GastID","Gastteam",Teams::get()->Map("ID", "Name","-")));	
		} else {
		$fields->addFieldToTab("Root.Main", new DropdownField("HeimID","Heimteam",Teams::get()->filter(array("Saison.ID" => $this->SaisonID))->Map("ID", "Name","-")->toArray(),1));
		$fields->addFieldToTab("Root.Main", new DropdownField("GastID","Gastteam",Teams::get()->filter(array("Saison.ID" => $this->SaisonID))->Map("ID", "Name","-")->toArray(),1));}
	 	$fields->addFieldToTab("Root.Main", new TextField("ToreHeim","Tore Heim"));
		$fields->addFieldToTab("Root.Main", new TextField("ToreGast","Tore Gast"));
	 	$fields->addFieldToTab("Root.Main", new CheckboxField("Stats","Stats"));
	 	$fields->addFieldToTab("Root.EigeneTore", 
		//$grid = new GridField('Tore', 'Alle Tore', Tore::get()->filter(array("SpielID" => $this->ID))->exclude('TorID', 0))
		$grid = new GridField(
    'EigeneTore',
    'Eigene Tore',
    //Tore::get()->filter(array("SpielID" => $this->ID))->exclude('TorID', 0),
    $this->Tore()->filter(array("SpielID" => $this->ID))->exclude('TorID', 0),
    GridFieldConfig::create()
		->addComponent(new GridFieldButtonRow('before'))
	    ->addComponent(new GridFieldToolbarHeader())
        ->addComponent(new GridFieldTitleHeader())
        ->addComponent(new GridFieldEditableColumns())
        ->addComponent(new GridFieldDeleteAction())
        ->addComponent(new GridFieldAddNewInlineButton())
		));
	
	$grid->getConfig()->getComponentByType('GridFieldEditableColumns')->setDisplayFields(array(
	    'TorID'  => function($record, $column, $grid) {
	        return new DropdownField($column);
	    }
	));
				
		$fields->removeFieldFromTab("Root.Main",'Spieldauer');
		$fields->removeFieldFromTab('Root', 'Tore');
	 	return $fields;
	 }
}

Code for goals

<?php

class Tore extends DataObject {
	
	private static $db = array(
	"Zeit" => "Varchar(6)",
	"Drittel" => "Int",
	"Art" => "Varchar(50)",
	"TorGegner" => "Varchar(200)",
	"A1Gegner" => "Varchar(200)",
	"A2Gegner" => "Varchar(200)",
	);
	
	private static $has_one = array(
	"Tor" => "Spieler",
	"A1" => "Spieler",
	"A2" => "Spieler",
	"TW" => "Spieler",
	"Spiel" => "Spiele",
	"Gegner" => "Teams"
	);

	private static $singular_name = "Tor";
	private static $plural_name = "Tore";


	static $default_sort = '';
	
	
	
	private static $searchable_fields = array(
	);
	
	private static $summary_fields = array(
	'Drittel',
	'Zeit',
	'Tor.Name',
	'A1.Name',
	'A2.Name',
	'TorGegner',
	'A1Gegner',
	'A2Gegner',
	'Art',
	'SpielID'
	);
			 
function getCMSFields() {
		
echo $this->SaisonID;
		$fields = parent::getCMSFields();
		$fields->addFieldToTab("Root.Main", new DropdownField("TorID","Torschütze",Spieler::get()->Map("ID", "Name","-")->toArray(),1));
		$fields->addFieldToTab("Root.Main", new DropdownField("A1ID","Assist 1",Spieler::get()->Map("ID", "Name","-")->toArray(),1));
		$fields->addFieldToTab("Root.Main", new DropdownField("A2ID","Assist 2",Spieler::get()->Map("ID", "Name","-")->toArray(),1));
		
	 	
	 	return $fields;
	 }

    public function onBeforeWrite() {
        $Team = Spiele::get()->byID($this->SpielID);
        if ($Team->Heim.Teamname == "EHC München" ){
        $this->Gegner = $Team->GastID;}
		else {
        $this->Gegner = $Team->HeimID;}
		
        parent::onBeforeWrite();
    }
}

Avatar
Dobby

Community Member, 15 Posts

8 August 2016 at 10:33pm

It was easier than I thought

return new DropdownField("SpielerID", "Spieler", Spieler::get());