Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

Backpack laravel ediitable_select options #1230

Open blessen131 opened 1 hour ago

blessen131 commented 1 hour ago

In my table status column values are 'Completed', 'In Progress', 'Scheduled', 'Canceled', but in my AdminCrudController user is only allowed to update the status 'Cancel' and 'Completed'

How to add drop down options as 'Cancel' and 'Completed' with the current status by default ABC | In Progress( with drop down options as 'Cancel' and 'Completed' ) ABD | Canceled ( No drop down if the status is canceled) ABCD | Completed( No drop down if the status is Completed)

` $this->crud->addColumn( [ 'name' => 'status', 'label' => 'Status', 'type' => 'editable_select', 'options' => [ 'Completed'=>'Completed', 'In Progress'=>'In Progress', 'Scheduled'=>'Scheduled', 'Canceled'=>'Canceled'

            ],
        ]
    ); `
backpack-ai-bot[bot] commented 1 hour ago

To achieve the functionality you're describing with the editable_select column in Backpack, you can use a closure for the options attribute to dynamically determine which options should be available based on the current status. Here's how you can modify your column definition:

$this->crud->addColumn([
    'name'    => 'status',
    'label'   => 'Status',
    'type'    => 'editable_select',
    'options' => function($entry) {
        if ($entry->status == 'In Progress') {
            return [
                'Completed' => 'Completed',
                'Cancel' => 'Cancel'
            ];
        } elseif ($entry->status == 'Canceled' || $entry->status == 'Completed') {
            return [$entry->status => $entry->status];
        } else {
            return [
                'Completed' => 'Completed',
                'In Progress' => 'In Progress',
                'Scheduled' => 'Scheduled',
                'Canceled' => 'Canceled'
            ];
        }
    },
]);

This configuration will:

  1. Show 'Cancel' and 'Completed' options for entries with 'In Progress' status.
  2. Show no dropdown (only the current status) for entries with 'Canceled' or 'Completed' status.
  3. Show all options for other statuses (like 'Scheduled').

Note that this approach uses the options attribute as a closure, which is a feature available in the Editable Columns add-on for Backpack. This allows you to dynamically set the options based on the current entry's status.

Editable Columns documentation mentions that you can use a closure for the options attribute, which gives you access to the current entry:

'options' => (function ($entry) {
    // Your logic here
}),

Remember that you need to have the Editable Columns add-on installed and properly set up in your Backpack project for this to work.

Relevant sources:

Columns|enum Fields|enum CRUD Fluent API|Examples Part 1 Editable Columns|Usage Part 3 Github Issue (closed): [Bug] crud.addFunctionToDataTablesDrawEventQueue('addOrRemoveCrudCheckedItem') is not called Editable Columns|Usage Part 3 Github Issue (closed): [Bug] Issues on "backpack/calendar-operation" Github Issue (closed): [Bug] Issue related backpack/calendar-operation , one hour behind in database upon visual time slot selection Github Issue (closed): Laravel-Backpack Table type with select