1

How do we make a select2_multiple sortable? I have a users table and a badges table. Naturally, I want to have a user_badge pivot table which will keep track of all the badges a user has and also sort it based on the order that I define using the select2_multiple.

Currently, I have my setupCreateOperation() setup as follows:

protected function setupCreateOperation()
{
    $this->crud->addField([
        'name' => 'badges_multi',
        'label' => 'Badges',
        'type' => 'select2_multiple',
        'attribute' => 'internal_name',
        'entity' => 'badges_multi',
        'model' => 'App\Models\Badges',
        'pivot' => true,
        'sortable' => true,
        'sortable_options' => [
            'handle' => '.my-custom-handle',
        ],
    ]);
}

This returns the select2_multiple but the input is not sortable, i.e. I can drag and drop to rearrange. It only returns badges in alphabetical order

cross19xx
  • 3,170
  • 1
  • 25
  • 40

1 Answers1

2

The select_multiple field does not have the ordering functionality.

To order you can use the select_and_order field or the repeatable field

You can also create a custom version of the select_multiple field that has the order functionality if you want to implement it that way.

Doing php artisan backpack:field select_multiple_with_order --from=select_multiple would create you a new file in your resources folder that is equal to Backpack select_multiple, from there you can add the functionality you need, and re-use it in other cruds if you need it there too.

Wish you the best.

Pedro X
  • 849
  • 5
  • 13
  • Thanks for the feedback, Pedro. How do I use the `select_and_order` to return the data to the frontend? – cross19xx Dec 27 '22 at 12:25
  • Sorry, I didn't quite understand what you mean by frontend, create and update operation forms ? You can check the field source code to have a better understanding of how the field works under the hood and extract some functionality you need elsewere. – Pedro X Dec 27 '22 at 16:32
  • Thanks for this, @Pedro. I switched to using select_and_order – cross19xx Jan 02 '23 at 08:24