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

Add class to gridfield row


Go to End


4 Posts   2027 Views

Avatar
MarijnKampf

Community Member, 176 Posts

1 May 2015 at 11:26pm

I'd like to add an extra class to a gridfield row (the tr element) is there a (standard) way of doing this or do I have to write my own hack? I couldn't find anything in the API, but I could have missed something. Tried asking on IRC but didn't get a response.

Avatar
Pyromanik

Community Member, 419 Posts

5 May 2015 at 9:58pm

Unfortunately not thus far - http://api.silverstripe.org/3.1/source-class-GridField.html#473
Only columns it seems - http://api.silverstripe.org/3.1/class-GridFieldDataColumns.html

Perhaps you could affix a class to the first column or something?
Maybe through a custom component implementing http://api.silverstripe.org/3.1/class-GridField_HTMLProvider.html

Avatar
MarijnKampf

Community Member, 176 Posts

6 May 2015 at 12:01am

I hadn't noticed the refractored GridField class from April 17/20. Hack was relatively simple with new code:

RowExtraClassesGridField.php

<?php

class RowExtraClassesGridField extends GridField {
//  class RowExtraClassesGridField extends FrontEndGridField {
	protected function newRowClasses($total, $index, $record) {
		$classes = array('ss-gridfield-item');

		if($index == 0) {
			$classes[] = 'first';
		}

		if($index == $total - 1) {
			$classes[] = 'last';
		}

		$classes[] = ($index % 2) ? 'even' : 'odd';

		if ($record->hasMethod("GridFieldRowClasses")) $classes = array_merge($classes, $record->GridFieldRowClasses());

		return $classes;
	}
}

Rather than create GridField create RowExtraClassesGridField

		$gridField = new RowExtraClassesGridField(
			$this,
			"Message",
			$data,
			$config
		);

Add "GridFieldRowClasses" function to your DataObject:

	public function GridFieldRowClasses() {
		$classes = array();
		if ($this->UnreadMessages()) $classes[] = "unread-messages";
		return $classes;
	}

Unfortunately using a DataExtension didn't seem to work, which would have been a nicer method.

// RowExtraClassesGridField.php
class RowExtraClassesGridField extends DataExtension {
  ::
-->8--
//config.yml

GridField:
  extensions:
    - RowExtraClassesGridField

Avatar
ntd

Community Member, 9 Posts

20 July 2015 at 8:13pm

Your class can be further condensed:

class RowExtraClassesGridField extends GridField {

	protected function newRowClasses($total, $index, $record) {
		$classes = parent::newRowClasses($total, $item, $record);
		if($record->hasMethod('GridFieldRowClasses'))
			$classes = array_merge($classes, $record->GridFieldRowClasses());
		return $classes;
	}
}

Instead of DataExtension you can always leverage the injector to pick up your class, e.g. by adding this to your YAML file:

Injector:
    GridField:
        class: RowExtraClassesGridField

In this case, I suspect you should instantiate your grid fields with GridField::create() instead of new GridField() though.