Kreyu/data-table-bundle

request: global configurable way of prepending actions

Closed this issue · 4 comments

Hi There,

Great work on the bundle, also the effort in extensive documentation is impressive. We are using the bundle and follow the improvements you are making.

However we do have a question/request: as of this moment it is possible to "sort" the actions column with the priority attribute, which is fine but kind of manual labor for a developer to always put this in de definition if it always needs to be on the beginning of the table.

Right now there is a constant in the DatatableInterface but maybe it could also be a parameter, so it can be easily changed from the config or through the compiler.

Nevertheless great work so far!

Regards

Hey, thank you for the kind words! Great suggestion 😄

Just for reference, currently, it is possible to override a default configuration of each type (data table, column, filter, etc.) with a type extension class. For example, if we want the ActionsColumnType to always be rendered with a specific priority option, we can create an column type extension:

<?php

declare(strict_types=1);

namespace App\DataTable\Column\Extension;

use Kreyu\Bundle\DataTableBundle\Column\Extension\AbstractColumnTypeExtension;
use Kreyu\Bundle\DataTableBundle\Column\Type\ActionsColumnType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DefaultPriorityActionsColumnTypeExtension extends AbstractColumnTypeExtension
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefault('priority', 1000);
    }

    public static function getExtendedTypes(): iterable
    {
        yield ActionsColumnType::class;
    }
}

This way, the ActionsColumnType (specified in the getExtendedTypes() method) will default to priority option set to 1000, unless it is explicitly given when creating a column.

@rvmourik, do you think that satisfies your requirement?

Oops, sorry, the solution I suggested above wasn't really possible, because the priority option was given explicitly by the DataTableBuilder itself:

class DataTableBuilder extends DataTableConfigBuilder implements DataTableBuilderInterface
{
    private function appendActionsColumn(): void
    {
        $this->addColumn(self::ACTIONS_COLUMN_NAME, ActionsColumnType::class, [
            'priority' => self::ACTIONS_COLUMN_PRIORITY, // this is now removed
            'actions' => $this->getRowActions(),
        ]);
    }
}

I moved the default priority (-1) to the ActionsColumnType itself (available in version 0.19.1).

Nice work! This way it's properly dynamic. Have a good one 💪