-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathToggleColumn.php
137 lines (117 loc) · 4.34 KB
/
ToggleColumn.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
<?php
namespace dixonstarter\togglecolumn;
use Yii;
use Closure;
use yii\helpers\Html;
use yii\web\View;
use yii\helpers\Url;
use yii\grid\DataColumn;
use yii\helpers\ArrayHelper;
/**
* Toggle update column in gridveiw
* @author Sathit Seethaphon <[email protected]>
*/
class ToggleColumn extends DataColumn
{
public $urlCreator;
public $controller;
public $action = 'toggle-update';
public $content;
//public $contentOptions = ['class'=>'text-center'];
/**
* [$linkTemplateOn link template for on value]
* @example
* default <a class="toggle-column" data-pjax="0" href="{url}">{label}</a>
* icon & label <a class="toggle-column btn btn-primary btn-xs btn-block" data-pjax="0" href="{url}"><i class="glyphicon glyphicon-ok"></i> {label}</a>'
* icon <a class="toggle-column btn btn-primary btn-xs btn-block" data-pjax="0" href="{url}"><i class="glyphicon glyphicon-ok"></i></a>
* @var string
*/
public $linkTemplateOn = '<a class="toggle-column btn btn-primary btn-xs btn-block" data-pjax="0" href="{url}"><i class="glyphicon glyphicon-ok"></i> {label}</a>';
/**
* [$linkTemplateOn link template for off value]
* @example
* default <a class="toggle-column" data-pjax="0" href="{url}">{label}</a>
* icon & label <a class="toggle-column btn btn-info btn-xs btn-block" data-pjax="0" href="{url}"><i class="glyphicon glyphicon-off"></i> {label}</a>
* icon <a class="toggle-column btn btn-info btn-xs btn-block" data-pjax="0" href="{url}"><i class="glyphicon glyphicon-time"></i> </a>
* @var string
*/
public $linkTemplateOff = '<a class="toggle-column btn btn-default btn-xs btn-block" data-pjax="0" href="{url}"><i class="glyphicon glyphicon-remove"></i> {label}</a>';
/**
* Array toggle items
* @var array
*/
public $items = [
'on' => ['label'=>'On', 'value'=>1],
'off' => ['label'=>'Off', 'value'=>0],
];
public function init()
{
parent::init();
$this->setFilter();
$this->registerJs();
}
public function setFilter()
{
$model = $this->grid->filterModel;
if(method_exists($model,'getToggleItems'))
{
$this->filter = $this->filter == false ? $model->getItemFilter() : $this->filter;
}
}
public function getIsOn($value,$model){
return $value == $model->onValue;
}
private function getLabel($key,$model)
{
return $this->getIsOn($key,$model) ? $model->onLabel : $model->offLabel;
}
private function setToggleItems($model){
if(method_exists($model,'getToggleItems')){
$this->items = $model->getToggleItems();
}
}
public function createUrl($action, $model, $key, $index)
{
if (is_callable($this->urlCreator)) {
return call_user_func($this->urlCreator, $action, $model, $key, $index);
} else {
$params = is_array($key) ? $key : ['id' => (string) $key];
$params[0] = $this->controller ? $this->controller . '/' . $action : $action;
return Url::toRoute($params);
}
}
protected function renderTemplate($model, $key, $index)
{
$url = $this->createUrl($this->action,$model, $key, $index);
$value = parent::renderDataCellContent($model, $key, $index);
return strtr($this->getIsOn($value,$model) ? $this->linkTemplateOn : $this->linkTemplateOff, [
'{url}' => Html::encode($url),
'{label}' => $this->getLabel($value,$model),
]);
}
protected function renderDataCellContent($model, $key, $index)
{
$this->setToggleItems($model);
$url = $this->createUrl($this->action,$model, $key, $index);
if ($this->content !== null) {
return call_user_func($this->content, $url, $model, $key, $index, $this);
} else {
return $this->renderTemplate($model, $key, $index);
}
}
public function registerJs()
{
$js = <<<JS
$("a.toggle-column").on("click", function(e) {
e.preventDefault();
var pjaxId = $(e.target).closest(".grid-view").parent().attr("id");
$(this).button('loading');
$.post($(this).attr("href"), function(data) {
$.pjax.reload({container:"#" + pjaxId});
});
return false;
});
JS;
$this->grid->view->registerJs($js, View::POS_READY, 'dixonstarter-toggle-column');
}
}