-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathDataTableColumn.php
263 lines (228 loc) · 7.82 KB
/
DataTableColumn.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* @copyright Copyright (c) 2018 Herve Guenot
* @license MIT
* @author Herve Guenot <[email protected]>
*/
namespace nullref\datatable;
use yii\base\Arrayable;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Inflector;
use yii\web\JsExpression;
/**
* Class DataTableColumn
*
* @package nullref\datatable
*
* Features
*
* @property string $type possible values (num, num-fmt, html-num, html-num-fmt, html, string)
* @property bool $orderable Using this parameter, you can remove the end user's ability to order upon a column.
* @property bool $searchable Using this parameter, you can define if DataTables should include this column in the filterable data in the table
* @property bool $visible show and hide columns dynamically through use of this option
* @property string $width This parameter can be used to define the width of a column, and may take any CSS value (3em, 20px etc).
* @property string $cellType Change the cell type created for the column - either TD cells or TH cells
* @property string $contentPadding Add padding to the text content used when calculating the optimal width for a table.
* @property string $orderDataType
*
* Check the full list of supported properties
*
* @see: https://datatables.net/reference/option/columns
*/
class DataTableColumn extends Widget implements Arrayable
{
/**
* @var string the attribute name associated with this column.
*/
public $data;
/**
* @var string label to be displayed in the header.
* If it is not set [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
*/
public $title;
/**
* @var array the HTML attributes for the filter input fields. This property is used in combination with
* the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
* render the HTML attributes for the generated filter input fields.
*
* Empty `id` in the default value ensures that id would not be obtained from the model attribute thus
* providing better performance.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $filterInputOptions = ['class' => 'form-control', 'id' => null];
/** @var JsExpression Javascript (function or expression) used to display the filter */
public $renderFilter;
/** @var JsExpression Javascript (function) used to display the value. */
public $render;
/** @var string CSS class for column cell */
public $sClass = '';
public $className = '';
/**
* Add extra fields to dataset
* These fields could be used at render function
*
* @var array
*/
public $extraColumns = [];
/**
* @var array|null|false Indicating if a filter will be displayed or not.
*
* - If this property is not set, a text field will be generated as the filter input with attributes defined
* with [[filterInputOptions]].
* - If this property is an array, a dropdown list will be generated that uses this property value as
* the list options.
* - If you don't want a filter for this data column, set this value to be false.
*/
protected $filter;
private $_options = [];
/**
* Check if all required properties is set
*/
public function init()
{
parent::init();
if ($this->data === null && $this->render === null) {
throw new InvalidConfigException("Either 'data' or 'render' properties must be specified.");
}
if ($this->title === null && !is_null($this->attribute)) {
$this->title = Inflector::camel2words($this->attribute);
}
if ($this->render === null) {
$this->render = $this->getJsRender();
}
if ($this->renderFilter === null) {
$this->renderFilter = $this->getJsFilter();
}
}
/**
* @return JsExpression
*/
public function getJsRender()
{
if (is_array($this->filter)) {
$select = "switch (data) {";
foreach ($this->filter as $key => $value) {
$key = Html::encode($key);
$value = Html::encode($value);
$select .= "\n\tcase '{$key}': return '{$value}';";
}
$select .= "\n\tdefault: return data;";
$select .= "\n}";
return new JsExpression("function render(data, type, row, meta) { {$select} }");
} else {
return new JsExpression("function render(data, type, row, meta){ return data; }");
}
}
/**
* @return JsExpression
*/
public function getJsFilter()
{
$jsTitle = Html::encode($this->label);
$jsClass = Html::encode($this->filterInputOptions['class']);
$jsId = $this->filterInputOptions['id'] ? Html::encode($this->filterInputOptions['id']) : $this->getId();
if (is_array($this->filter)) {
$select = "jQuery('<select type=\"text\" placeholder=\"Search {$jsTitle}\"></select>')\n" .
"\t.addClass('{$jsClass}')\n" .
"\t.width('100%')\n" .
"\t.attr('id', '{$jsId}')\n" .
"\t.append(jQuery('<option value=\"\"></option>'))";
foreach ($this->filter as $key => $value) {
$key = Html::encode($key);
$value = Html::encode($value);
$select .= "\n\t.append(jQuery('<option></option>', {\n\t\t"
. "'value': serverSide ? '{$key}' : '{$value}',\n\t\t"
. "'text': '{$value}'\n\t"
. "}))";
}
return new JsExpression("function(table) { var serverSide = table.page.info().serverSide; return {$select}; }");
} else if ($this->filter !== false) {
return new JsExpression(
"function() {" .
"return jQuery('<input type=\"text\" placeholder=\"Search {$jsTitle}\" />')\n" .
"\t.addClass('{$jsClass}')\n" .
"\t.width('100%')\n" .
"\t.attr('id', '{$jsId}');\n" .
"}"
);
} else {
return new JsExpression('jQuery()');
}
}
public function setAttribute($attribute)
{
$this->data = $attribute;
}
public function getAttribute()
{
return $this->data;
}
public function setLabel($label)
{
$this->title = $label;
}
public function getLabel()
{
return $this->title;
}
/**
* @return array|false|null
*/
public function getFilter()
{
return $this->filter;
}
/**
* @param array|false|null $filter
*/
public function setFilter($filter)
{
$this->filter = $filter;
}
/**
* @return array
*/
public function getExtraColumns()
{
return $this->extraColumns;
}
public function __get($name)
{
return $this->canGetProperty($name, true)
? parent::__get($name)
: (isset($this->_options[$name]) ? $this->_options[$name] : null);
}
public function __set($name, $value)
{
if ($this->canSetProperty($name, true))
return parent::__set($name, $value);
else
return $this->_options[$name] = $value;
}
/**
* @inheritDoc
*/
public function fields()
{
return \Yii::getObjectVars($this);
}
/**
* @inheritDoc
*/
public function extraFields()
{
return $this->_options;
}
/**
* @inheritDoc
*/
public function toArray(array $fields = [], array $expand = [], $recursive = true)
{
return $recursive
? array_merge_recursive($this->fields(), $this->extraFields())
: array_merge($this->fields(), $this->extraFields());
}
}