What is the TableListField class, and how do you use it in SilverStripe?

What is the TableListField class, and how do you use it in SilverStripe?

The TableListField class in SilverStripe is a component of the CMS that provides a user-friendly way to display and manage a list of related data records in a tabular format. It is commonly used for managing relationships, such as many-to-many associations or one-to-many relationships between DataObjects.

Key Features of TableListField

  • Data Display: Shows related records in a structured table format.
  • Editable: Allows for inline editing of certain fields.
  • Sorting and Filtering: Supports sorting and filtering capabilities for ease of data management.
  • Customization: Offers various options for customizing the display, including which columns to show and how to format them.

How to Use TableListField in SilverStripe

Step 1: Create Your DataObjects

First, you need to define the DataObjects that you want to manage. For example, consider a simple Author and Book relationship:

Example

<?php
use SilverStripe\ORM\DataObject;

class Author extends DataObject
{
    private static $db = [
        'Name' => 'Varchar(255)',
    ];

    private static $many_many = [
        'Books' => Book::class,
    ];
}

class Book extends DataObject
{
    private static $db = [
        'Title' => 'Varchar(255)',
    ];

    private static $many_many = [
        'Authors' => Author::class,
    ];
}
?>

Step 2: Create a CMS Page or Controller

In your CMS page or controller, you can create a TableListField to manage the relationship.

Example

<?php
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use Page;

class AuthorPage extends Page
{
    private static $has_many = [
        'Authors' => Author::class,
    ];

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

        // Create a GridField to manage the many-to-many relationship
        $gridField = GridField::create(
            'Books',
            'Books',
            $this->Books(), // The related data (Books)
            GridFieldConfig_RelationEditor::create() // Configuration for editing
        );

        // Add the GridField to the fields
        $fields->addFieldToTab('Root.Books', $gridField);

        return $fields;
    }
}
?>

Step 3: Configure the TableListField

You can customize the TableListField further by specifying which fields to show, applying filters, or enabling sorting:

Example

<?php
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;

$gridField = GridField::create(
    'Books',
    'Books',
    $this->Books(),
    GridFieldConfig_RelationEditor::create()->addComponents(
        new GridFieldSortableHeader(), // Enables sorting
        new GridFieldFilterHeader() // Enables filtering
    )
);
?>

Related Questions & Topics