-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSortableGridView.php
101 lines (84 loc) · 2.67 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace richardfan\sortable;
use Closure;
use yii\base\InvalidConfigException;
use yii\grid\GridView;
use yii\helpers\Json;
use yii\grid\GridViewAsset;
use yii\helpers\Html;
class SortableGridView extends GridView {
/**
* (required) The URL of related SortableAction
*
* @see \richardfan1126\sortable\SortableAction
* @var string
*/
public $sortUrl;
/**
* (optional) The text shown in the model while the server is reordering model
* You can use HTML tag in this attribute.
*
* @var string
*/
public $sortingPromptText = 'Loading...';
/**
* (optional) The text shown in alert box when sorting failed.
*
* @var string
*/
public $failText = 'Fail to sort';
/**
* (optional) Element which need to drag to start sortable.
* Empty if need drag by whole row.
*
* @var string
*/
public $moveItem = '';
public function init(){
parent::init();
if(!isset($this->sortUrl)){
throw new InvalidConfigException("You must specify the sortUrl");
}
GridViewAsset::register($this->view);
SortableGridViewAsset::register($this->view);
$this->tableOptions['class'] .= ' sortable-grid-view';
}
/**
* {@inheritDoc}
* @see \yii\grid\GridView::renderTableRow()
*/
public function renderTableRow($model, $key, $index)
{
$cells = [];
/* @var $column Column */
foreach ($this->columns as $column) {
$cells[] = $column->renderDataCell($model, $key, $index);
}
if ($this->rowOptions instanceof Closure) {
$options = call_user_func($this->rowOptions, $model, $key, $index, $this);
} else {
$options = $this->rowOptions;
}
// $options['id'] = "items[]_{$model->primaryKey}";
$options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
return Html::tag('tr', implode('', $cells), $options);
}
public function run(){
foreach($this->columns as $column){
if(property_exists($column, 'enableSorting'))
$column->enableSorting = false;
}
parent::run();
$options = [
'id' => $this->id,
'action' => $this->sortUrl,
'sortingPromptText' => $this->sortingPromptText,
'sortingFailText' => $this->failText,
'moveItem' => $this->moveItem,
'csrfTokenName' => \Yii::$app->request->csrfParam,
'csrfToken' => \Yii::$app->request->csrfToken,
];
$options = Json::encode($options);
$this->view->registerJs("jQuery.SortableGridView($options);");
}
}