Best way to set a dynamic button on a table builder

edster - 3 weeks ago

I want to have a button that says deactivate or activate on a table builder depending on the value of one of columns in that row.

how would you handle this? I can only get the config to set for all rows, not have a button show only for that row.

There is also a standard edit button for all rows as well.

Thanks!

Answer

- 3 weeks ago

Create a handler under the table directory called XxxxxxTableButtons.php. Then use setButtons on your builder like so:

Then i have this code: <?php namespace Pixney\CampaignsModule\Campaign\Table;

use Pixney\CampaignsModule\Campaign\Contract\CampaignInterface;
use Pixney\CampaignsModule\Campaign\Table\CampaignTableBuilder;

class CampaignTableButtons
{

    /**
     * @param CampaignFormBuilder $builder
     */
    public function handle(CampaignTableBuilder $builder)
    {
        $builder->setButtons(
            [
                'send' => [
                    'enabled' => function (CampaignInterface $entry)
                    {
                        // Send button should be available if the entry is locked
                        // and not sent.
                        return ($entry->locked && !$entry->sent);
                    },
                ],
                'edit' => [
                    'enabled' => function (CampaignInterface $entry)
                    {
                        return ($entry->locked == false);
                    },
                ],
                'preview' => [
                    'enabled' => function (CampaignInterface $entry)
                    {
                        return true;
                    },
                ],
            ]
        );
    }
}

william - 3 weeks ago

Create a handler under the table directory called XxxxxxTableButtons.php. Then use setButtons on your builder like so:

Then i have this code: <?php namespace Pixney\CampaignsModule\Campaign\Table;

use Pixney\CampaignsModule\Campaign\Contract\CampaignInterface;
use Pixney\CampaignsModule\Campaign\Table\CampaignTableBuilder;

class CampaignTableButtons
{

    /**
     * @param CampaignFormBuilder $builder
     */
    public function handle(CampaignTableBuilder $builder)
    {
        $builder->setButtons(
            [
                'send' => [
                    'enabled' => function (CampaignInterface $entry)
                    {
                        // Send button should be available if the entry is locked
                        // and not sent.
                        return ($entry->locked && !$entry->sent);
                    },
                ],
                'edit' => [
                    'enabled' => function (CampaignInterface $entry)
                    {
                        return ($entry->locked == false);
                    },
                ],
                'preview' => [
                    'enabled' => function (CampaignInterface $entry)
                    {
                        return true;
                    },
                ],
            ]
        );
    }
}

edster - 3 weeks ago

Thanks William! Didn't know about the enabled flag!