├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── resources └── assets │ └── jquery-ui.min.js ├── routes └── web.php └── src ├── Controllers └── GridSortableController.php ├── GridSortable.php ├── GridSortableServiceProvider.php ├── SaveOrderBtn.php └── SortableDisplay.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | phpunit.phar 3 | /vendor 4 | composer.phar 5 | composer.lock 6 | *.project 7 | .idea/ -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | laravel-admin grid-sortable 2 | ====== 3 | 4 | This extension can help you sort by dragging the rows of the data list, the front end is based on [jQueryUI sortable](https://jqueryui.com/sortable/), and the back end is based on [eloquent-sortable](https://github.com/spatie/eloquent-sortable) 5 | 6 | 这个插件可以帮助你通过拖动数据列表的行来进行排序,前端基于[jQueryUI sortable](https://jqueryui.com/sortable/), 后端基于[eloquent-sortable](https://github.com/spatie/eloquent-sortable) 7 | 8 | ![Kapture 2019-06-25 at 10 14 51](https://user-images.githubusercontent.com/1479100/60064224-50b97080-9732-11e9-8023-431fc6fe81a5.gif) 9 | 10 | ## Installation 11 | 12 | ```shell 13 | composer require laravel-admin-ext/grid-sortable -vvv 14 | ``` 15 | 16 | Publish asserts 17 | 18 | ```shell 19 | php artisan vendor:publish --provider="Encore\Admin\GridSortable\GridSortableServiceProvider" 20 | ``` 21 | 22 | ## Usage 23 | 24 | Define your model 25 | 26 | ```php 27 | 'order_column', 39 | 'sort_when_creating' => true, 40 | ]; 41 | } 42 | ``` 43 | 44 | Use in grid 45 | 46 | ```php 47 | $grid = new Grid(new MyModel); 48 | 49 | $grid->sortable(); 50 | ``` 51 | 52 | This will add a column to the grid. After dragging one row, a `Save order` button will appear at the top of the grid. Click to save order. 53 | 54 | ## Translation 55 | 56 | The default text for the button is `Save order`. If you use an other language, such as Simplified Chinese, you can add a translation to the `resources/lang/zh-CN.json` file. 57 | 58 | ```json 59 | { 60 | "Save order": "保存排序" 61 | } 62 | ``` 63 | 64 | ## Donate 65 | 66 | > Help keeping the project development going, by donating a little. Thanks in advance. 67 | 68 | [![PayPal Me](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/zousong) 69 | 70 | ![-1](https://cloud.githubusercontent.com/assets/1479100/23287423/45c68202-fa78-11e6-8125-3e365101a313.jpg) 71 | 72 | License 73 | ------------ 74 | Licensed under [The MIT License (MIT)](LICENSE). 75 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /routes/web.php: -------------------------------------------------------------------------------- 1 | name('laravel-admin-grid-sortable'); 4 | -------------------------------------------------------------------------------- /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/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/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 | -------------------------------------------------------------------------------- /src/SaveOrderBtn.php: -------------------------------------------------------------------------------- 1 | getGrid()->model()->getOriginalModel()); 15 | 16 | $class = str_replace('\\', '\\\\', $class); 17 | 18 | $script = <<