Custom Blade components to add sortable/drag-and-drop HTML elements in your apps.

Overview

Laravel Blade Sortable

Latest Version on Packagist Total Downloads

Laravel Blade Sortable

Demo

Repo

demo

Installation

You can install the package via composer:

composer require asantibanez/laravel-blade-sortable

After the package is installed, make sure to add laravel-blade-sortable::scripts components next to your other scripts.

<x-laravel-blade-sortable::scripts/>
<script src="/js/app.js"></script>

Requirements

Package requires SortableJs and AlpineJs to be installed in your application in order to enable sorting. Reach out to their respective documentation in order to set them up.

NOTE: SortableJs must be available at the window object level in Javascript. To do this, import the library using

window.Sortable = require('sortablejs').default

or use any other similar approach

Usage

The package provides 2 custom Blade components to enable sorting of DOM elements:

  • laravel-blade-sortable::sortable
  • laravel-blade-sortable::sortable-item

Sortable

laravel-blade-sortable::sortable is used as the wrapper element for your sortable/drag-and-drop items. It must be used to enclose the children it will enable sortable.

<x-laravel-blade-sortable::sortable>
    {{-- Items here --}}
</x-laravel-blade-sortable::sortable>

By default, the component renders a "div" as the wrapper node. You can customize this behavior by passing an as property to render the type of node you need.

<x-laravel-blade-sortable::sortable
    as="ul" {{-- Will render an unordered list wrapper node --}}
>
    {{-- Items here --}}
</x-laravel-blade-sortable::sortable>

NOTE: Any other attribute you pass along (class, id, alt, etc) will be added to the element

If you would like to use custom Blade component as a wrapper node, you can also do this by passing a component property.

<x-laravel-blade-sortable::sortable
    component="custom-blade-component" {{-- Will render "x-custom-blade-component" --}}
>
    {{-- Items here --}}
</x-laravel-blade-sortable::sortable>

Sortable Item

laravel-blade-sortable::sortable-item is used as the wrapper element for each item you want to enable sorting.

<x-laravel-blade-sortable::sortable>
    <x-laravel-blade-sortable::sortable-item sort-key="jason">
        Jason
    </x-laravel-blade-sortable::sortable-item>
    <x-laravel-blade-sortable::sortable-item sort-key="andres">
        Andres
    </x-laravel-blade-sortable::sortable-item>
    <x-laravel-blade-sortable::sortable-item sort-key="matt">
        Matt
    </x-laravel-blade-sortable::sortable-item>
    <x-laravel-blade-sortable::sortable-item sort-key="james">
        James
    </x-laravel-blade-sortable::sortable-item>
</x-laravel-blade-sortable::sortable>

NOTE: Similar to laravel-blade-sortable::sortable, you can pass a as or component property to render the type of node or custom component you desire.

NOTE: Extra attributes like class, id, alt, etc can be passed along to and will be added to the item node.

As you may have noticed, every laravel-blade-sortable::sortable-item requires a sort-key property. This property will be used to keep track of the ordering of the elements. Should be unique too.

And that's it. You have now a sortable list rendered by Laravel Blade without any custom Javascript. 🔥

basic

That example looks awful though 😅 . Because you can pass in any custom component or styling directly, you can customize the wrapper and item nodes according to your needs. Here's another example using TailwindCSS ❤️ and custom components

custom-component

Looks dope, right? 👌

Advanced Usage

As Form Input

The sort order of elements can be used alongside other input fields on form submissions. To enable this behavior, just pass a name prop to a laravel-blade-sortable::sortable component. The name should be the name of the input in your form.

<form>
    <x-laravel-blade-sortable::sortable
        name="sort_order"
    >
        {{-- Items here --}}
    </x-laravel-blade-sortable::sortable>
</form>

By adding a name props, the component internally adds hidden inputs for each one of the items' sort-key.

as-form-input

Pretty neat! 👌

With Livewire

Into Livewire? It's awesome. We know.

You can use this package within your Livewire views and use the sorting information in the component.

To get "sort change" updates in your Livewire component, just add the attribute wire:onSortOrderChange to a x-laravel-blade-sortable::sortable component. Adding this attribute will hook the Livewire component when a sorting event happens and will call the specified method/callback.

<x-laravel-blade-sortable::sortable
    name="dropzone"
    wire:onSortOrderChange="handleSortOrderChange"
>
    {{-- Items here --}}
</x-laravel-blade-sortable::sortable>

In the example above, every time your items are sorted, the handleSortOrderChange method will be called passing as argument an array with your items' sort-key in the current order.

livewire

Extra info is passed along too, so you can check extra data when processing the sort order

public function handleOnSortOrderChanged($sortOrder, $previousSortOrder, $name, $from, $to)
{
    // $sortOrder = new keys order
    // $previousSortOrder = keys previous order
    // $name = drop target name
    // $from = name of drop target from where the dragged/sorted item came from
    // $to = name of drop target to where the dragged/sorted item was placed
}

Customization

To support some advanced features of SortableJs, it is possible to pass the following props to a laravel-blade-sortable::sortable component:

  • animation: milliseconds it takes to run the sorting animation. 150 is the default value.
  • ghost-class: class added to the dragged object during sort. Default is null. Must be 1 class only.
  • drag-handle: class name that will be used as the handle for dragging. Only the DOM element that has that class can enable sorting.
<x-laravel-blade-sortable::sortable
    animation="1000"
    ghost-class="opacity-25"
    drag-handle="drag-handle"
>
    {{-- Items here --}}
</x-laravel-blade-sortable::sortable>

customization

Multiple Drop Zones

Wanting to have different drop zones to drag/drop/sort elements? We have you covered. 😎

Just add a group string prop to a laravel-blade-sortable::sortable component. Add the same prop to another laravel-blade-sortable::sortable component on the same page and BOOM! Done!

<x-laravel-blade-sortable::sortable
    group="people"
>
    {{-- Items here --}}
</x-laravel-blade-sortable::sortable>

<x-laravel-blade-sortable::sortable
    group="people"
>
    {{-- Items here --}}
</x-laravel-blade-sortable::sortable>

drag-drop

Enable/Disable sorting and/or drop

Use :allow-sort=true|false and :allow-drop=true|false to x-laravel-blade-sortable::sortable components to enable/disable sorting and/or drop of elements.

Both defaults to true.

<x-laravel-blade-sortable::sortable
    group="people"
    :allow-sort="false"
    :allow-drop="false"
>
    {{-- Items here --}}
</x-laravel-blade-sortable::sortable>

disable-sort-drop

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
  • Submit droped items (in order)

    Submit droped items (in order)

    Hello,

    How to store/submit droped items (in order)?

    DRAG FROM:

    	<x-laravel-blade-sortable::sortable group="orders">
    	   @foreach($orders as $order)
    	      <x-laravel-blade-sortable::sortable-item as="div" sort-key="{{ $order->id }}" class="bg-theme-gray-500 my-1 px-5 py-5 text-white">{{ $order->id }}</x-laravel-blade-sortable::sortable-item>
    	   @endforeach
    	</x-laravel-blade-sortable::sortable>
    

    DROPED IN:

    <form method="POST" action="{{ route('selected_orders.update', $workday->id) }}" class="py-6 px-10">
    	@csrf
    	@method('PATCH')
    	<x-laravel-blade-sortable::sortable group="orders" name="sort_order">
              <!--- DROP ZONE --> 
    	</x-laravel-blade-sortable::sortable>
    
    	<button type="submit">Submit</button>
    </form>
    
    opened by WL1981 3
  • Deleting sortable-item

    Deleting sortable-item

    Hello,

    I have multiple sortable lists:

    group 1

    • team 1
    • team 2
    • team 3

    group 2 (no teams)

    group 3 (no teams)

    sample code:

    @foreach ($groups as $group)
                        
                        <x-laravel-blade-sortable::sortable
                            group="teams"
                            name="{{ $group->id }}"
                            wire:onSortOrderChange="groupChanged"
                        >
                            @foreach ($group->teams as $team)
                                <x-laravel-blade-sortable::sortable-item
                                    sort-key="{{ $team->id }}"
                                >
                                    <div>
                                        {{ $team->name }}
                                    </div>
                                </x-laravel-blade-sortable::sortable-item>
                            @endforeach
                        </x-laravel-blade-sortable::sortable>
                    @endforeach
    

    I have a function that can remove a group. ($groups is updated)

    If I remove the group2, then each groups after that breaks... for example when group 2 is removed and i try to move a team from group 1 to group 3 groupChanges will receive the following params:

    0: ["..."]
    1: ["..."]
    2: "group 2" <- this is already deleted, should be group 1
    3: "group 1"
    4: "group 3"
    

    Any ideas whats wrong?

    opened by Hesesses 2
  • Sort between groups, not inside

    Sort between groups, not inside

    Hi Andres , thanks for everything, I was wondering if its possible to limit the sorting in a group it self, for example you can drag from A to B, but not change the positions in A itself, appreciate any help, thanks

    Screen Shot 2021-04-02 at 2 01 00 PM

    Basically:

    1. Allow from A to B
    2. Don't allow from B to A
    3. Don't allow sorting inside A
    4. Allow sorting inside B
    opened by nam-co 2
  • Alpine V3 error create

    Alpine V3 error create

    Hello, I have this error when using it i am using laravel 9 alpinejs 3.10 and the latest development version of sortable.

    with the alpine 2 version it works perfect, does anyone have a solution for this?

    image

    opened by Jhamnerx 0
  • $dom.getAttribute is not a function

    $dom.getAttribute is not a function

    Hi, trying to use this with a livewire component.

                    @foreach ($p1->chunk(4) as $chunk)
                    <div class="row" style="background-color: #8cdba9; margin-bottom: 7px; padding: 10px;">
                        <x-laravel-blade-sortable::sortable>
                            @foreach ($chunk as $item)
                              <x-laravel-blade-sortable::sortable-item as='label' class="checkbox-inline col-3" sort-key="{{$item->order_position}}"  drag-handle="drag-handle">
                                {{-- <label class="checkbox-inline col-3"> --}}
                                    <input type="checkbox" value="1" id="item{{$item->id}}" @if (in_array($item->id, $currentConfigs)) checked @endif wire:click="updateConfig({{ $item->id }})"> {!! $item->value !!}
                                {{-- </label> --}}
                            </x-laravel-blade-sortable::sortable-item>
                            @endforeach
                        </x-laravel-blade-sortable::sortable>
                        </div>
                    @endforeach
    

    When I do this and try to click my element, the console prints this error: image

    I have my scripts in the file listed in this order: @livewire /js/app.js and lastly the x-laravel-blade-sortable::scripts/

    I have installed alpine and sortable through NPM and run npm dev to compile the scripts (within Laravel) after including/requiring the libraries in the app/bootstrap.js files.

    What is causing this error?

    opened by maximus1127 0
  • FEATURE: Added ability to enable/disable sort and/or drop

    FEATURE: Added ability to enable/disable sort and/or drop

    Summary

    This PR adds the ability to enable/disable sorting and/or drop in sortable containers. Two new arguments have been added to x-laravel-blade-sortable::sortable component

    • :allow-sort: true|false
    • :allow-drop: true|false

    Issues

    Resolves #5

    Type of Change

    • [x] :rocket: New Feature

    Screenshot/Video

    2021-04-04 17 36 19

    opened by asantibanez 0
  • No data is passed

    No data is passed

    https://github.com/asantibanez/laravel-blade-sortable/issues/9#issuecomment-903175095

    i use this from the example, but there is no data passed... all variables are empty except $name, $from, $to

    public function handleOnSortOrderChanged($sortOrder, $previousSortOrder, $name, $from, $to)
    {
        // $sortOrder = new keys order
        // $previousSortOrder = keys previous order
        // $name = drop target name
        // $from = name of drop target from where the dragged/sorted item came from
        // $to = name of drop target to where the dragged/sorted item was placed
    }
    
    opened by Dontorpedo 0
  • Unable to call component method. Public method [__v_raw] not found on component:

    Unable to call component method. Public method [__v_raw] not found on component:

    I am getting error when I drag and drop a sortable item.

    When I remove wire:onSortOrderChange="handleOnSortOrderChanged", it works. Method was properly added to livewire component.

    Anyone knows how to fix it? I am using laravel 9.11 version. PHP 8.0.1

    opened by dcemal 1
  • Laravel 9 support

    Laravel 9 support

    Hello,

    I'm getting this when trying to update/install on laravel 9 project:

    composer require asantibanez/laravel-blade-sortable
    Using version ^1.3 for asantibanez/laravel-blade-sortable
    ./composer.json has been updated
    Running composer update asantibanez/laravel-blade-sortable
    Loading composer repositories with package information
    Updating dependencies
    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - Root composer.json requires asantibanez/laravel-blade-sortable ^1.3 -> satisfiable by asantibanez/laravel-blade-sortable[v1.3.0].
        - asantibanez/laravel-blade-sortable v1.3.0 requires illuminate/support ^7.0|^8.0 -> found illuminate/support[v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
    
    You can also try re-running composer require with an explicit version constraint, e.g. "composer require asantibanez/laravel-blade-sortable:*" to figure out if any version is installable, or "composer require asantibanez/laravel-blade-sortable:^2.1" if you know which you need.
    
    Installation failed, reverting ./composer.json and ./composer.lock to their original content.
    opened by Hesesses 4
  • allow-drag=true|false

    allow-drag=true|false

    Hi,

    Would be nice to have allow-drag=true|false

    This would disable dragging from the container.

    For example I have lists A, B and C

    I want to move items from A and B to C I dont want to move items from C to anywhere

    opened by Hesesses 1
  • Multiple drop zones

    Multiple drop zones

    Hello,

    The problem i'm having is to NOT have linked drop zones. I would like to have just 2 listings A and B. You should not be able to drag item from A to B or from B to A

    I have tried this with:

    • do not use group param
    • use different group params for A and B
    • use group param for A, empty for B
    • use group param for B, empty for A

    Any ideas?

    Edit:

    I can return false from wire:onSortOrderChange function to disable the dropping, but would be nice to disable this from UI also

    opened by Hesesses 2
  • Get updated sort order for $from and $to groups?

    Get updated sort order for $from and $to groups?

    Hey there, great package! I'm wondering if I'm missing something -- is there a way to get the updated sort list for both the from and to groups when updating? I just see the new order and previous order for the target group I think

    opened by dambridge 1
Releases(v1.3.0)
Owner
Andrés Santibáñez
Andrés Santibáñez
🎨 Free custom elements for the WordPress plugin Oxygen Builder.

?? Custom Elements for Oxygen Builder Free custom elements for the WordPress plugin Oxygen Builder. If you find the elements useful, click on the star

Simon Vidman 62 Dec 29, 2022
Cagilo - a set of simple components for use in your views Laravel Blade.

Cagilo - a set of simple components for use in your views Laravel Blade. Official Documentation Documentation for Cagilo can be found on its we

Cagilo 151 Dec 6, 2022
Useful blade components and functionality for most Laravel projects.

laravel-base Note: Package is still in early stages of development, so functionality is subject to change. LaravelBase is a package I've created to pr

Randall Wilk 3 Jan 16, 2022
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

TinaH 622 Jan 2, 2023
Sortable behaviour for Eloquent models

Sortable behaviour for Eloquent models This package provides a trait that adds sortable behaviour to an Eloquent model. The value of the order column

Spatie 1.2k Dec 22, 2022
Laravel-admin grid-sortable

laravel-admin grid-sortable This extension can help you sort by dragging the rows of the data list, the front end is based on jQueryUI sortable, and t

Extensions for laravel-admin 37 Oct 14, 2022
Column sorting with Laravel 8 & 9 - Eloquent sortable

Column sorting for Laravel - Sortable - Sort by Larasort : Column sorting for Laravel - Sort easily Introduction - Larasort package This package allow

Stephen Damian - Laravel Developer 11 Sep 20, 2022
This package allows you to render livewire components like a blade component, giving it attributes, slots etc

X-livewire This package allows you to render livewire components like a blade component, giving it attributes, slots etc. Assuming you wanted to creat

null 7 Nov 15, 2022
A collection of pre-made simple Laravel Blade form components.

Laravel Form Components Library A collection of pre-made simple Laravel Blade form components. Installation & setup You can install the package via co

null 3 Oct 5, 2022
Provide all attributes (including irregular patterns) to Laravel Blade class components.

blade-wants-attributes blade-wants-attributes offers you the ability to use Blade/HTML-defined attributes within the constructors of Laravel Blade cla

Stephan Casas 4 Sep 15, 2022
Blade Snip allows you to use parts of a blade template multiple times. Basically partials, but inline.

Blade Snip Blade Snip allows you to use parts of a blade template multiple times. Basically partials, but inline: <div class="products"> @snip('pr

Jack Sleight 18 Dec 4, 2022
Add Server-Timing header information from within your Laravel apps.

Laravel Server Timings Add Server-Timing header information from within your Laravel apps. Installation You can install the package via composer: comp

Beyond Code 498 Dec 15, 2022
Source code behind the Laracasts Larabit: Creating and Using Custom Blade Directives

This is the source code behind the Laracasts Larabit: Creating and Using Custom Blade Directives, and features all of the files and code available in that video.

Andrew Schmelyun 1 Nov 12, 2021
A simple API documentation package for Laravel using OpenAPI and Stoplight Elements

Laravel Stoplight Elements Easily publish your API documentation using your OpenAPI document in your Laravel Application. Installation You can install

Steve McDougall 24 Nov 17, 2022
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

Ollie Codes 19 Jul 5, 2021
This package extends Laravel's FormBuilder to include some (soon all) HTML5 elements

HTML5 Forms for Laravel This package extends Laravel's FormBuilder to include some (soon all) HTML5 elements. How to Install Install the braunson/lara

Braunson Yager 89 Jun 17, 2022
Composer package which adds support for HTML5 elements using Laravels Form interface (e.g. Form::date())

Laravel HTML 5 Inputs Composer package which adds support for HTML5 elements by extending Laravel's Form interface (e.g. Form::date()) Adds support fo

Small Dog Studios 11 Oct 13, 2020
cybercog 996 Dec 28, 2022
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.

Introduction A simple drop-in solution for providing UUID support for the IDs of your Eloquent models. Both v1 and v4 IDs are supported out of the box

GoldSpec Digital 501 Jan 4, 2023