├── .gitignore ├── routes └── web.php ├── src ├── GridSortableServiceProvider.php ├── GridSortable.php ├── Controllers │ └── GridSortableController.php ├── SaveOrderBtn.php └── SortableDisplay.php ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | phpunit.phar 3 | /vendor 4 | composer.phar 5 | composer.lock 6 | *.project 7 | .idea/ -------------------------------------------------------------------------------- /routes/web.php: -------------------------------------------------------------------------------- 1 | name('laravel-admin-grid-sortable'); 4 | -------------------------------------------------------------------------------- /src/GridSortableServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole() && $assets = $extension->assets()) { 20 | $this->publishes( 21 | [$assets => public_path('vendor/laravel-admin-ext/grid-sortable')], 22 | 'laravel-admin-grid-sortable' 23 | ); 24 | } 25 | 26 | GridSortable::routes(__DIR__.'/../routes/web.php'); 27 | 28 | Admin::booting(function () { 29 | Admin::headerJs('vendor/laravel-admin-ext/grid-sortable/jquery-ui.min.js'); 30 | }); 31 | 32 | $extension->install(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-admin-ext/grid-sortable", 3 | "description": "Sort the grid data by drag and drop rows", 4 | "type": "library", 5 | "keywords": ["laravel-admin", "extension", "grid", "sortable"], 6 | "homepage": "https://github.com/laravel-admin-ext/grid-sortable", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "z-song", 11 | "email": "zsong@126.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=7.0.0", 16 | "encore/laravel-admin": ">=1.7.2", 17 | "spatie/eloquent-sortable": "*" 18 | }, 19 | "require-dev": { 20 | "phpunit/phpunit": "~6.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Encore\\Admin\\GridSortable\\": "src/" 25 | } 26 | }, 27 | "extra": { 28 | "laravel": { 29 | "providers": [ 30 | "Encore\\Admin\\GridSortable\\GridSortableServiceProvider" 31 | ] 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jens Segers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /src/GridSortable.php: -------------------------------------------------------------------------------- 1 | column); 21 | 22 | Grid::macro('sortable', function () use ($column) { 23 | 24 | $this->tools(function (Grid\Tools $tools) { 25 | $tools->append(new SaveOrderBtn()); 26 | }); 27 | 28 | $sortName = $this->model()->getSortName(); 29 | 30 | if (!request()->has($sortName) 31 | && $this->model()->eloquent() instanceof Sortable 32 | ) { 33 | $this->model()->ordered(); 34 | } 35 | 36 | $this->column($column, ' ') 37 | ->displayUsing(SortableDisplay::class); 38 | }); 39 | } 40 | } -------------------------------------------------------------------------------- /src/Controllers/GridSortableController.php: -------------------------------------------------------------------------------- 1 | get('_sort'); 14 | 15 | $sorts = collect($sorts) 16 | ->pluck('key') 17 | ->combine( 18 | collect($sorts)->pluck('sort')->sort() 19 | ); 20 | 21 | $status = true; 22 | $message = trans('admin.save_succeeded'); 23 | $modelClass = $request->get('_model'); 24 | 25 | try { 26 | /** @var \Illuminate\Database\Eloquent\Collection $models */ 27 | $models = $modelClass::find($sorts->keys()); 28 | 29 | foreach ($models as $model) { 30 | 31 | $column = data_get($model->sortable, 'order_column_name', 'order_column'); 32 | 33 | $model->{$column} = $sorts->get($model->getKey()); 34 | $model->save(); 35 | } 36 | } catch (Exception $exception) { 37 | $status = false; 38 | $message = $exception->getMessage(); 39 | } 40 | 41 | return response()->json(compact('status', 'message')); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/SaveOrderBtn.php: -------------------------------------------------------------------------------- 1 | getGrid()->model()->getOriginalModel()); 15 | 16 | $class = str_replace('\\', '\\\\', $class); 17 | 18 | $script = <<