-
Notifications
You must be signed in to change notification settings - Fork 17
/
SortableGridView.php
76 lines (65 loc) · 1.83 KB
/
SortableGridView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* MIT licence
* Version 1.2
* Sjaak Priester, Amsterdam 28-08-2014 ... 15-02-2021.
* https://sjaakpriester.nl
*
* Sortable GridView for Yii 2.0
*
* GridView which is made sortable by means of HTML Drag and Drop.
* After each order operation, order data are posted to $orderUrl in the following format:
* - $_POST["key"] - the primary key of the sorted ActiveRecord,
* - $_POST["pos"] - the new position, zero-indexed.
*
*/
namespace sjaakp\sortable;
use yii\grid\GridView;
use yii\helpers\Html;
use yii\helpers\Url;
/**
* Class SortableGridView
* @package sjaakp\sortable
*/
class SortableGridView extends GridView {
/**
* @var array|string
* The url which is called after an order operation.
* The format is that of yii\helpers\Url::toRoute.
* The url will be called with the POST method and the following data:
* - key the primary key of the ordered ActiveRecord,
* - pos the new, zero-indexed position.
*
* Example: ['movie/order-actor', 'id' => 5]
*/
public $orderUrl;
/**
* @var array
* for compatibility only
*/
public $sortOptions = [];
/**
* @var boolean|string
* for compatibility only
*/
public $sortAxis = 'y';
/**
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
Html::addCssClass($this->tableOptions, 'd-sortable');
$this->rowOptions = function($model, $key, $index, $grid) {
$id = "{$grid->id}_$key";
return [
'id' => $id,
'draggable' => 'true'
];
};
$url = Url::toRoute($this->orderUrl);
$view = $this->getView();
SortableAsset::register($view);
$view->registerJs("sortable('$url', 'table.d-sortable tbody', 'tr');");
}
}