Skip to content

Commit 24447d1

Browse files
committed
first commit
0 parents  commit 24447d1

10 files changed

+367
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
phpunit.phar
3+
/vendor
4+
composer.phar
5+
composer.lock
6+
*.project
7+
.idea/

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.

README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
laravel-admin grid-sortable
2+
======
3+
4+
这个插件可以帮助你通过拖动数据列表的行来进行排序,前端基于[jQueryUI sortable](https://jqueryui.com/sortable/), 后端基于[eloquent-sortable](https://github.com/spatie/eloquent-sortable)
5+
6+
## Installation
7+
8+
```shell
9+
composer require laravel-admin-ext/grid-sortable -vvv
10+
```
11+
12+
## Usage
13+
14+
定义模型
15+
16+
```php
17+
<?php
18+
19+
use Illuminate\Database\Eloquent\Model;
20+
use Spatie\EloquentSortable\Sortable;
21+
use Spatie\EloquentSortable\SortableTrait;
22+
23+
class MyModel extends Model implements Sortable
24+
{
25+
use SortableTrait;
26+
27+
public $sortable = [
28+
'order_column_name' => 'order_column',
29+
'sort_when_creating' => true,
30+
];
31+
}
32+
```
33+
34+
在Grid中使用
35+
36+
```php
37+
$grid = new Grid(new MyModel);
38+
39+
$grid->sortable();
40+
```
41+
42+
这样会给表格增加一列排序列,拖动之后在表格顶部会出现一个Save order的按钮,点击保存排序
43+
44+
## Translation
45+
46+
排序保存按钮默认的文字是`Save order`,如果使用其他语言,比如简体中文,那么可以在`resources/lang/zh-CN.json`文件中增加一项翻译
47+
48+
```json
49+
{
50+
"Save order": "保存排序"
51+
}
52+
```
53+
54+
其他语言也是按照上面的方式操作。
55+
56+
## Donate
57+
58+
> Help keeping the project development going, by donating a little. Thanks in advance.
59+
60+
[![PayPal Me](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/zousong)
61+
62+
![-1](https://cloud.githubusercontent.com/assets/1479100/23287423/45c68202-fa78-11e6-8125-3e365101a313.jpg)
63+
64+
License
65+
------------
66+
Licensed under [The MIT License (MIT)](LICENSE).

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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": "[email protected]"
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+
}

resources/assets/jquery-ui.min.js

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/web.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
Route::post('_grid-sortable_', function (\Illuminate\Http\Request $request) {
4+
5+
$sorts = $request->get('_sort');
6+
7+
$sorts = collect($sorts)
8+
->pluck('key')
9+
->combine(
10+
collect($sorts)->pluck('sort')->sort()
11+
);
12+
13+
$status = true;
14+
$message = trans('admin.save_succeeded');
15+
$modelClass = $request->get('_model');
16+
17+
try {
18+
/** @var \Illuminate\Database\Eloquent\Collection $models */
19+
$models = $modelClass::find($sorts->keys());
20+
21+
foreach ($models as $model) {
22+
23+
$column = data_get($model->sortable, 'order_column_name', 'order_column');
24+
25+
$model->{$column} = $sorts->get($model->getKey());
26+
$model->save();
27+
}
28+
29+
} catch (Exception $exception) {
30+
$status = false;
31+
$message = $exception->getMessage();
32+
}
33+
34+
return response()->json(compact('status', 'message'));
35+
36+
})->name('laravel-admin-grid-sortable');

src/GridSortable.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Encore\Admin\GridSortable;
4+
5+
use Encore\Admin\Extension;
6+
use Encore\Admin\Grid;
7+
use Encore\Admin\Grid\Tools\ColumnSelector;
8+
use Spatie\EloquentSortable\Sortable;
9+
10+
class GridSortable extends Extension
11+
{
12+
public $name = 'grid-sortable';
13+
14+
public $assets = __DIR__.'/../resources/assets';
15+
16+
protected $column = '__sortable__';
17+
18+
public function install()
19+
{
20+
ColumnSelector::ignore($column = $this->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

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Encore\Admin\GridSortable;
4+
5+
use Encore\Admin\Admin;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class GridSortableServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function boot(GridSortable $extension)
14+
{
15+
if (! GridSortable::boot()) {
16+
return ;
17+
}
18+
19+
if ($this->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+
$this->app->booted(function () {
27+
GridSortable::routes(__DIR__.'/../routes/web.php');
28+
});
29+
30+
Admin::booting(function () {
31+
Admin::headerJs('vendor/laravel-admin-ext/grid-sortable/jquery-ui.min.js');
32+
});
33+
34+
$extension->install();
35+
}
36+
}

src/SaveOrderBtn.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Encore\Admin\GridSortable;
4+
5+
use Encore\Admin\Admin;
6+
use Encore\Admin\Grid\Tools\AbstractTool;
7+
8+
class SaveOrderBtn extends AbstractTool
9+
{
10+
protected function script()
11+
{
12+
$route = route('laravel-admin-grid-sortable');
13+
14+
$class = get_class($this->getGrid()->model()->getOriginalModel());
15+
16+
$class = str_replace('\\', '\\\\', $class);
17+
18+
$script = <<<SCRIPT
19+
(function () {
20+
21+
$('.grid-save-order-btn').click(function () {
22+
23+
$.post('{$route}', {
24+
_token: $.admin.token,
25+
_model: '{$class}',
26+
_sort: $(this).data('sort'),
27+
},
28+
function(data){
29+
30+
if (data.status) {
31+
$.admin.toastr.success(data.message, '', {positionClass:"toast-top-center"});
32+
$.admin.reload();
33+
} else {
34+
$.admin.toastr.error(data.message, '', {positionClass:"toast-top-center"});
35+
}
36+
});
37+
});
38+
39+
})();
40+
SCRIPT;
41+
Admin::script($script);
42+
}
43+
44+
public function render()
45+
{
46+
$this->script();
47+
48+
$text = __('Save order');
49+
50+
return <<<HTML
51+
<button type="button" class="btn btn-sm btn-info grid-save-order-btn" style="margin-left: 10px;display: none;">
52+
<i class="fa fa-save"></i><span class="hidden-xs">&nbsp;&nbsp;{$text}</span>
53+
</button>
54+
HTML;
55+
}
56+
}

src/SortableDisplay.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Encore\Admin\GridSortable;
4+
5+
use Encore\Admin\Admin;
6+
use Encore\Admin\Grid\Displayers\AbstractDisplayer;
7+
8+
class SortableDisplay extends AbstractDisplayer
9+
{
10+
protected function script()
11+
{
12+
$id = $this->getGrid()->tableID;
13+
14+
$script = <<<SCRIPT
15+
16+
(function () {
17+
$("#{$id} tbody").sortable({
18+
placeholder: "sort-highlight",
19+
handle: ".grid-sortable-handle",
20+
forcePlaceholderSize: true,
21+
zIndex: 999999
22+
}).on("sortupdate", function(event, ui) {
23+
24+
var sorts = [];
25+
$(this).find('.grid-sortable-handle').each(function () {
26+
sorts.push($(this).data());
27+
});
28+
29+
var \$btn = $('#{$id}').closest('.box').find('.grid-save-order-btn');
30+
\$btn.data('sort', sorts).show();
31+
});
32+
})();
33+
SCRIPT;
34+
35+
Admin::script($script);
36+
}
37+
38+
protected function getRowSort()
39+
{
40+
$column = data_get($this->row->sortable, 'order_column_name', 'order_column');
41+
42+
return $this->row->{$column};
43+
}
44+
45+
public function display()
46+
{
47+
$this->script();
48+
49+
$key = $this->getKey();
50+
$sort = $this->getRowSort();
51+
52+
return <<<HTML
53+
<a class="grid-sortable-handle" style="cursor: move;" data-key="{$key}" data-sort="{$sort}">
54+
<i class="fa fa-ellipsis-v"></i>
55+
<i class="fa fa-ellipsis-v"></i>
56+
</a>
57+
HTML;
58+
}
59+
}

0 commit comments

Comments
 (0)