forked from phemellc/yii2-toggle-column
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToggleColumn.php
144 lines (125 loc) · 3.3 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
138
139
140
141
142
143
144
<?php
/**
* @link http://phe.me
* @copyright Copyright (c) 2014 Pheme
* @license MIT http://opensource.org/licenses/MIT
*/
namespace pheme\grid;
use yii\grid\DataColumn;
use yii\helpers\Html;
use yii\web\View;
use Yii;
/**
* @author Aris Karageorgos <aris@phe.me>
*/
class ToggleColumn extends DataColumn
{
/**
* Toggle action that will be used as the toggle action in your controller
* @var string
*/
public $action = 'toggle';
/**
* @var string pk field name
*/
public $primaryKey = 'primaryKey';
/**
* Whether to use ajax or not
* @var bool
*/
public $enableAjax = true;
/**
* @var string glyphicon for 'on' value
*/
public $iconOn = 'ok';
/**
* @var string glyphicon for 'off' value
*/
public $iconOff = 'remove';
/**
* @var string text to display on the 'on' link
*/
public $onText;
/**
* @var string text to display on the 'off' link
*/
public $offText;
/**
* @var string text to display next to the 'on' link
*/
public $displayValueText = false;
/**
* @var string text to display next to the 'on' link
*/
public $onValueText;
/**
* @var string text to display next to the 'off' link
*/
public $offValueText;
public function init()
{
if ($this->onText === null) {
$this->onText = Yii::t('app', 'On');
}
if ($this->offText === null) {
$this->offText = Yii::t('app', 'Off');
}
if ($this->onValueText === null) {
$this->onValueText = Yii::t('app', 'Active');
}
if ($this->offValueText === null) {
$this->offValueText = Yii::t('app', 'Inactive');
}
if ($this->enableAjax) {
$this->registerJs();
}
}
/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
$url = [$this->action, 'id' => $model->{$this->primaryKey}];
$attribute = $this->attribute;
$value = $model->$attribute;
if ($value === null || $value == true) {
$icon = $this->iconOn;
$title = $this->offText;
$valueText = $this->onValueText;
} else {
$icon = $this->iconOff;
$title = $this->onText;
$valueText = $this->offValueText;
}
return Html::a(
'<span class="glyphicon glyphicon-' . $icon . '"></span>',
$url,
[
'title' => $title,
'class' => 'toggle-column',
'data-method' => 'post',
'data-pjax' => '0',
]
) . ( $this->displayValueText ? " {$valueText}" : "" );
}
/**
* Registers the ajax JS
*/
public function registerJs()
{
if(Yii::$app->request->isAjax) {
return;
}
$js = <<<'JS'
$(document.body).on("click", "a.toggle-column", function(e) {
e.preventDefault();
$.post($(this).attr("href"), function(data) {
var pjaxId = $(e.target).closest("[data-pjax-container]").attr("id");
$.pjax.reload({container:"#" + pjaxId});
});
return false;
});
JS;
$this->grid->view->registerJs($js, View::POS_READY, 'pheme-toggle-column');
}
}