BaronGrivet
BaronGrivet

Reputation: 4424

How to Display Dynamic non-DataObject Data in a Gridfield in Silverstripe 4.x?

We have a Silverstripe 4.x project which uses the following Dataobjects to create dynamic tables:

DataColumn
 ColumnName

DataRow
 RowID

DataCell
 Value
 has_one DataColumn
 has_one DataRow

CMS Users can add/remove/edit columns - so we can't use a static Dataobject to represent this data.

However we would like to display this dynamic data in a Gridfield & export it as a CSV.

There are three possible approaches I can think of - but I'm not sure which one we should focus on (or whether there's a better way).

Approach #1: Create temporary Dataobject from dynamic data We could possibly create a temporary Dataobject in the database from the dynamic data. If we did this we could then display the temporary object in a Gridfield where it could be searched/ filtered/ exported etc. using the standard Gridfield functionality.

Approach #2: Transmute the dynamic data into a Gridfield Could it be possible to transmute the dynamic data into a DataList or ArrayList that could then be displayed in a Gridfield? This is similar to approach #1 but is done on the fly, rather than creating a temporary Dataobject in the database.

Approach #3: Recode Gridfield functionality for dynamic data format This is the fallback but would involve the re-inventing of many wheels - so this is our last option if the other two aren't possible.

Which approach should we take? Is there existing functionality that can do what we're after?

Any guidance is appreciated.

Upvotes: 0

Views: 259

Answers (1)

Zauberfisch
Zauberfisch

Reputation: 4015

In theory, GridField is not bound to DataObject or DataList. It's working with the SS_List interface and an will mostly also accept ViewableData or ArrayData Objects.

Though that's not true for all gridfield components. So you will need to implement a lot of extra methods that the gridfield components expect.

Most components also expect each record to have a field ID.
And for example GridFieldSortableHeader expects the List to implement Sortable.


It's a bit of extra work, but it is definitely possible.

I have done several projects where I displayed external data from an api in a GridField by turning the data into a ArrayList of ArrayData objects and passing that into the gridfield. Though I had to customize some components like GridFieldDetailForm.

I've also built a projects with dynamic columns, where a user can select which columns to show (the DataObject had ~50 columns and not every user needed to see all of them). So I built a custom component to add a selection field to the top of the gridfield and then a modified GridFieldDataColumns that only showed the selected fields.

Upvotes: 2

Related Questions