-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSorterColumn.php
170 lines (141 loc) · 3 KB
/
SorterColumn.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace webvimark\components;
use yii\grid\DataColumn;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
/**
* Class StatusColumn
*
* For rendering attributes with dropDown filters and colored value
*
* @package app\webvimark\helpers
*/
class SorterColumn extends DataColumn
{
/**
* @var string
*/
public $attribute = 'sorter';
/**
* Default is $this->grid->id . "-pjax"
*
* @var string
*/
public $pjaxId;
/**
* Button name when it's ready to sort. Default - "Sort"
*
* @var string
*/
public $sortButtonName = 'Sort';
/**
* After sort button will be disabled for X seconds and renamed according to this param.
* Default - "Sorting..."
*
* @var string
*/
public $sortingButtonName = 'Sorting...';
/**
* @var string
*/
public $buttonClass = 'btn btn-sm btn-primary';
/**
* @var string
*/
public $inputClass;
/**
* "filter" or "footer"
*
* @var string
*/
public $buttonPlace = 'filter';
/**
* Url where requests will be send
* Default - Url::to(['grid-sort'])
*
* @var string
*/
public $sortUrl;
/**
* Init
*/
public function init()
{
parent::init();
$this->setDefaultOptions();
$this->setCellStyleOptions();
$this->initOptions();
$this->grid->view->registerJs($this->sortButtonJs());
}
/**
* @throws \yii\base\InvalidConfigException
*/
protected function initOptions()
{
$this->{$this->buttonPlace} = Html::tag(
'span',
$this->sortButtonName,
['class'=>'grid-sort-button '.$this->buttonClass]
);
$this->format = 'raw';
$this->value = function($model, $key, $index, $widget)
{
return Html::textInput(
$this->attribute . "[{$model->id}]",
$model->{$this->attribute},
[
'style'=>'width:40px; text-align:center',
'class'=>'grid-sort-input '.$this->inputClass,
]
);
};
}
/**
* Set minimal width and align text in cell
*/
protected function setCellStyleOptions()
{
$this->contentOptions = ArrayHelper::merge(
['style'=>'text-align:center; width:10px; white-space:nowrap;'],
$this->contentOptions
);
$this->filterOptions = ArrayHelper::merge(
['style'=>'text-align:center; width:10px; white-space:nowrap;'],
$this->filterOptions
);
}
/**
* Set default options
*/
protected function setDefaultOptions()
{
if ( ! $this->sortUrl )
$this->sortUrl = Url::to(['grid-sort']);
if ( ! $this->pjaxId )
$this->pjaxId = $this->grid->id . '-pjax';
}
/**
* @return string
*/
protected function sortButtonJs()
{
$gridId = '#'.$this->grid->id;
$url = $this->sortUrl;
$sortingText = $this->sortingButtonName;
$pjaxId = '#'.$this->pjaxId;
$js = <<<JS
$(document).off('click', ".grid-sort-button").on('click', ".grid-sort-button", function () {
var _t = $(this);
_t.addClass('disabled').html('$sortingText');
$.post('$url', $('$gridId .grid-sort-input').serialize() )
.done(function(){
setTimeout(function(){
$.pjax.reload({container: '$pjaxId'});
}, 500);
});
});
JS;
return $js;
}
}