1
|
|
|
<?php |
2
|
|
|
namespace Ajax\semantic\widgets\datatable; |
3
|
|
|
|
4
|
|
|
use Ajax\JsUtils; |
5
|
|
|
use Ajax\common\Widget; |
6
|
|
|
use Ajax\common\html\HtmlDoubleElement; |
7
|
|
|
use Ajax\semantic\html\base\constants\Direction; |
8
|
|
|
use Ajax\semantic\html\base\traits\BaseTrait; |
9
|
|
|
use Ajax\semantic\html\collections\HtmlMessage; |
10
|
|
|
use Ajax\semantic\html\collections\table\HtmlTable; |
11
|
|
|
use Ajax\semantic\html\collections\table\traits\TableTrait; |
12
|
|
|
use Ajax\semantic\html\elements\HtmlInput; |
13
|
|
|
use Ajax\semantic\html\modules\checkbox\HtmlCheckbox; |
14
|
|
|
use Ajax\semantic\widgets\base\InstanceViewer; |
15
|
|
|
use Ajax\service\JArray; |
16
|
|
|
use Ajax\service\JString; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DataTable widget for displaying list of objects |
20
|
|
|
* |
21
|
|
|
* @version 1.0 |
22
|
|
|
* @author jc |
23
|
|
|
* @since 2.2 |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
class DataTable extends Widget { |
27
|
|
|
use TableTrait,DataTableFieldAsTrait,HasCheckboxesTrait,BaseTrait; |
28
|
|
|
|
29
|
|
|
protected $_searchField; |
30
|
|
|
|
31
|
|
|
protected $_urls; |
32
|
|
|
|
33
|
|
|
protected $_pagination; |
34
|
|
|
|
35
|
|
|
protected $_compileParts; |
36
|
|
|
|
37
|
|
|
protected $_deleteBehavior; |
38
|
|
|
|
39
|
|
|
protected $_editBehavior; |
40
|
|
|
|
41
|
|
|
protected $_displayBehavior; |
42
|
|
|
|
43
|
|
|
protected $_visibleHover = false; |
44
|
|
|
|
45
|
|
|
protected $_targetSelector; |
46
|
|
|
|
47
|
|
|
protected $_refreshSelector; |
48
|
|
|
|
49
|
|
|
protected $_emptyMessage; |
50
|
|
|
|
51
|
|
|
protected $_json; |
52
|
|
|
|
53
|
|
|
protected $_rowClass = "_element"; |
54
|
|
|
|
55
|
|
|
protected $_sortable; |
56
|
|
|
|
57
|
|
|
protected $_hiddenColumns; |
58
|
|
|
|
59
|
|
|
protected $_colWidths; |
60
|
|
|
|
61
|
|
|
public function __construct($identifier, $model, $modelInstance = NULL) { |
62
|
|
|
parent::__construct($identifier, $model, $modelInstance); |
63
|
|
|
$this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0, 0), false); |
64
|
|
|
$this->_urls = []; |
65
|
|
|
$this->_emptyMessage = new HtmlMessage("", "nothing to display"); |
66
|
|
|
$this->_emptyMessage->setIcon("info circle"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function run(JsUtils $js) { |
70
|
|
|
$offset = $js->scriptCount(); |
71
|
|
|
if ($this->_hasCheckboxes && isset($js)) { |
72
|
|
|
$this->_runCheckboxes($js); |
73
|
|
|
} |
74
|
|
|
if ($this->_visibleHover) { |
75
|
|
|
$js->execOn("mouseover", "#" . $this->identifier . " tr", "$(event.target).closest('tr').find('.visibleover').css('visibility', 'visible');", [ |
76
|
|
|
"preventDefault" => false, |
77
|
|
|
"stopPropagation" => true |
78
|
|
|
]); |
79
|
|
|
$js->execOn("mouseout", "#" . $this->identifier . " tr", "$(event.target).closest('tr').find('.visibleover').css('visibility', 'hidden');", [ |
80
|
|
|
"preventDefault" => false, |
81
|
|
|
"stopPropagation" => true |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
if (\is_array($this->_deleteBehavior)) |
85
|
|
|
$this->_generateBehavior("delete", $this->_deleteBehavior, $js); |
86
|
|
|
if (\is_array($this->_editBehavior)) |
87
|
|
|
$this->_generateBehavior("edit", $this->_editBehavior, $js); |
88
|
|
|
if (\is_array($this->_displayBehavior)) { |
89
|
|
|
$this->_generateBehavior("display", $this->_displayBehavior, $js); |
90
|
|
|
} |
91
|
|
|
parent::run($js); |
92
|
|
|
if (isset($this->_pagination)) |
93
|
|
|
$this->_associatePaginationBehavior($js, $offset); |
94
|
|
|
$this->_associateSearchFieldBehavior($js, $offset); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function _generateBehavior($op, $params, JsUtils $js) { |
98
|
|
|
if (isset($this->_urls[$op])) { |
99
|
|
|
$params = \array_merge($params, [ |
100
|
|
|
"attr" => "data-ajax" |
101
|
|
|
]); |
102
|
|
|
$js->ajaxOnClick("#" . $this->identifier . " ._" . $op, $this->_urls[$op], $this->getTargetSelector($op), $params); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
* @see TableTrait::getTable() |
110
|
|
|
*/ |
111
|
|
|
protected function getTable() { |
112
|
|
|
return $this->content["table"]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function refreshTR() { |
116
|
|
|
$this->getTable()->refreshTR(); |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function refreshTD($fieldName, $jquery, $view) { |
121
|
|
|
$index = $this->_getIndex($fieldName); |
122
|
|
|
$this->compile($jquery, $view); |
123
|
|
|
return $this->refreshTR() |
124
|
|
|
->getTable() |
125
|
|
|
->getCell(0, $index); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function compile(JsUtils $js = NULL, &$view = NULL) { |
129
|
|
|
if (! $this->_generated) { |
130
|
|
|
if (isset($this->_buttonsColumn)) { |
131
|
|
|
$this->_instanceViewer->sortColumnContent($this->_buttonsColumn, $this->_buttons); |
132
|
|
|
} |
133
|
|
|
$this->_instanceViewer->setInstance($this->_model); |
134
|
|
|
$captions = $this->_instanceViewer->getCaptions(); |
135
|
|
|
$table = $this->content["table"]; |
136
|
|
|
if ($this->_hasCheckboxes) { |
137
|
|
|
$this->_generateMainCheckbox($captions); |
138
|
|
|
} |
139
|
|
|
$table->setRowCount(0, \sizeof($captions)); |
140
|
|
|
$this->_generateHeader($table, $captions); |
141
|
|
|
|
142
|
|
|
if (isset($this->_compileParts)) |
143
|
|
|
$table->setCompileParts($this->_compileParts); |
144
|
|
|
|
145
|
|
|
$this->_generateContent($table); |
146
|
|
|
|
147
|
|
|
$this->compileExtraElements($table, $captions); |
148
|
|
|
$this->_compileSearchFieldBehavior($js); |
149
|
|
|
|
150
|
|
|
$this->content = JArray::sortAssociative($this->content, [ |
151
|
|
|
PositionInTable::BEFORETABLE, |
152
|
|
|
"table", |
153
|
|
|
PositionInTable::AFTERTABLE |
154
|
|
|
]); |
155
|
|
|
$this->_compileForm(); |
156
|
|
|
$this->_applyStyleAttributes($table); |
157
|
|
|
$this->_generated = true; |
158
|
|
|
} |
159
|
|
|
return parent::compile($js, $view); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function compileExtraElements($table, $captions) { |
163
|
|
|
if ($this->_hasCheckboxes && $table->hasPart("thead")) { |
164
|
|
|
$table->getHeader() |
165
|
|
|
->getCell(0, 0) |
166
|
|
|
->addClass("no-sort"); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (isset($this->_toolbar)) { |
170
|
|
|
$this->_setToolbarPosition($table, $captions); |
171
|
|
|
} |
172
|
|
|
if (isset($this->_pagination) && $this->_pagination->getVisible()) { |
173
|
|
|
$this->_generatePagination($table); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
protected function _applyStyleAttributes($table) { |
178
|
|
|
if (isset($this->_hiddenColumns)) |
179
|
|
|
$this->_hideColumns(); |
180
|
|
|
if (isset($this->_colWidths)) { |
181
|
|
|
foreach ($this->_colWidths as $colIndex => $width) { |
182
|
|
|
$table->setColWidth($colIndex, $width); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected function _hideColumns() { |
188
|
|
|
foreach ($this->_hiddenColumns as $colIndex) { |
189
|
|
|
$this->_self->hideColumn($colIndex); |
190
|
|
|
} |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
protected function _generateHeader(HtmlTable $table, $captions) { |
195
|
|
|
$table->setHeaderValues($captions); |
196
|
|
|
if (isset($this->_sortable)) { |
197
|
|
|
$table->setSortable($this->_sortable); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function _generateContent($table) { |
202
|
|
|
$objects = $this->_modelInstance; |
203
|
|
|
if (isset($this->_pagination)) { |
204
|
|
|
$objects = $this->_pagination->getObjects($this->_modelInstance); |
205
|
|
|
} |
206
|
|
|
InstanceViewer::setIndex(0); |
207
|
|
|
$fields = $this->_instanceViewer->getSimpleProperties(); |
208
|
|
|
$groupByFields = $this->_instanceViewer->getGroupByFields(); |
209
|
|
|
if (! is_array($groupByFields)) { |
210
|
|
|
$table->fromDatabaseObjects($objects, function ($instance) use ($table, $fields) { |
211
|
|
|
return $this->_generateRow($instance, $fields, $table); |
212
|
|
|
}); |
213
|
|
|
} else { |
214
|
|
|
$activeValues = array_combine($groupByFields, array_fill(0, sizeof($groupByFields), null)); |
215
|
|
|
$uuids = []; |
216
|
|
|
$table->fromDatabaseObjects($objects, function ($instance) use ($table, $fields, &$activeValues, $groupByFields, &$uuids) { |
217
|
|
|
$this->_instanceViewer->setInstance($instance); |
218
|
|
|
foreach ($groupByFields as $index => $gbField) { |
219
|
|
|
$this->_generateGroupByRow($index, $gbField, $table, $fields, $activeValues, $uuids); |
220
|
|
|
} |
221
|
|
|
return $this->_generateRow($instance, $fields, $table, null, $uuids); |
222
|
|
|
}); |
223
|
|
|
} |
224
|
|
|
if ($table->getRowCount() == 0) { |
225
|
|
|
$result = $table->addRow(); |
226
|
|
|
$result->mergeRow(); |
227
|
|
|
$result->setValues([ |
228
|
|
|
$this->_emptyMessage |
229
|
|
|
]); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
protected function _generateGroupByRow($index, $gbField, $table, $fields, &$activeValues, &$uuids) { |
234
|
|
|
$newValue = $this->_instanceViewer->getValue($gbField); |
235
|
|
|
if ($this->getElementContent($activeValues[$gbField]) !== $this->getElementContent($newValue)) { |
236
|
|
|
if ($index == 0) { |
237
|
|
|
$uuids = []; |
238
|
|
|
} |
239
|
|
|
$uuid = uniqid("grp"); |
240
|
|
|
$uuids[$gbField] = $uuid; |
241
|
|
|
$id = $this->_instanceViewer->getIdentifier(); |
242
|
|
|
$result = $table->addMergeRow(sizeof($fields) + 1, $newValue); |
243
|
|
|
$result->setIdentifier($this->identifier . "-tr-gb-" . $id); |
244
|
|
|
$result->setProperty("data-ajax", $id); |
245
|
|
|
$result->setProperty("data-group", $uuid); |
246
|
|
|
$result->addToProperty("class", $this->_rowClass); |
247
|
|
|
$activeValues[$gbField] = $newValue; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
private function getElementContent($elm) { |
252
|
|
|
if ($elm instanceof HtmlDoubleElement) { |
253
|
|
|
return $elm->getTextContent(); |
254
|
|
|
} |
255
|
|
|
return $elm; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function getFieldValue($index) { |
259
|
|
|
$index = $this->_getIndex($index); |
260
|
|
|
if (is_numeric($index)) { |
261
|
|
|
$values = $this->_instanceViewer->getValues(); |
262
|
|
|
if (isset($values[$index])) { |
263
|
|
|
return $values[$index]; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
return null; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
protected function _generateRow($instance, $fields, &$table, $checkedClass = null, $uuids = null) { |
270
|
|
|
$this->_instanceViewer->setInstance($instance); |
271
|
|
|
InstanceViewer::$index ++; |
272
|
|
|
$values = $this->_instanceViewer->getValues(); |
273
|
|
|
$id = $this->_instanceViewer->getIdentifier(); |
274
|
|
|
$dataAjax = $id; |
275
|
|
|
$id = $this->cleanIdentifier($id); |
276
|
|
|
if ($this->_hasCheckboxes) { |
277
|
|
|
$ck = new HtmlCheckbox("ck-" . $this->identifier . "-" . $id, ""); |
278
|
|
|
$checked = false; |
279
|
|
|
if (isset($this->_checkedCallback)) { |
280
|
|
|
$func = $this->_checkedCallback; |
281
|
|
|
$checked = $func($instance); |
282
|
|
|
} |
283
|
|
|
$ck->setChecked($checked); |
284
|
|
|
// $ck->setOnChange("event.stopPropagation();"); |
285
|
|
|
$field = $ck->getField(); |
286
|
|
|
$field->setProperty("value", $dataAjax); |
287
|
|
|
$field->setProperty("name", "selection[]"); |
288
|
|
|
if (isset($checkedClass)) |
289
|
|
|
$field->setClass($checkedClass); |
290
|
|
|
\array_unshift($values, $ck); |
291
|
|
|
} |
292
|
|
|
$result = $table->newRow(); |
293
|
|
|
$result->setIdentifier($this->identifier . "-tr-" . $id); |
294
|
|
|
$result->setProperty("data-ajax", $dataAjax); |
295
|
|
|
$result->setValues($values); |
296
|
|
|
$result->addToProperty("class", $this->_rowClass); |
297
|
|
|
$result->setPropertyValues("data-field", $fields); |
298
|
|
|
if (isset($uuids)) { |
299
|
|
|
$result->setProperty("data-child", implode(" ", $uuids)); |
300
|
|
|
} |
301
|
|
|
return $result; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
protected function _generatePagination($table) { |
305
|
|
|
if (isset($this->_toolbar)) { |
306
|
|
|
if ($this->_toolbarPosition == PositionInTable::FOOTER) |
307
|
|
|
$this->_toolbar->setFloated("left"); |
308
|
|
|
} |
309
|
|
|
$footer = $table->getFooter(); |
310
|
|
|
$footer->mergeCol(); |
311
|
|
|
$footer->addValues($this->_pagination->generateMenu($this->identifier)); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
protected function _associatePaginationBehavior(JsUtils $js = NULL, $offset = null) { |
315
|
|
|
if (isset($this->_urls["refresh"])) { |
316
|
|
|
$menu = $this->_pagination->getMenu(); |
317
|
|
|
if (isset($menu) && isset($js)) { |
318
|
|
|
$js->postOnClick("#" . $menu->getIdentifier() . " .item", $this->_urls["refresh"], "{'p':$(this).attr('data-page'),'_model':'" . JString::doubleBackSlashes($this->_model) . "'}", $this->getRefreshSelector(), [ |
319
|
|
|
"preventDefault" => false, |
320
|
|
|
"jqueryDone" => "replaceWith", |
321
|
|
|
"hasLoader" => false, |
322
|
|
|
"jsCallback" => '$("#' . $this->identifier . '").trigger("pageChange");$("#' . $this->identifier . '").trigger("activeRowChange");' |
323
|
|
|
]); |
324
|
|
|
$page = $_POST["p"] ?? null; |
325
|
|
|
if (isset($page)) { |
326
|
|
|
$js->execAtLast('$("#' . $this->getIdentifier() . ' .pagination").children("a.item").removeClass("active");$("#' . $this->getIdentifier() . ' .pagination").children("a.item[data-page=' . $page . ']:not(.no-active)").addClass("active");'); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
protected function _compileSearchFieldBehavior(JsUtils $js = NULL) { |
333
|
|
|
if (isset($this->_searchField) && isset($js) && isset($this->_urls["refresh"])) { |
334
|
|
|
$this->_searchField->postOn("change", $this->_urls["refresh"], "{'s':$(self).val(),'_model':'" . JString::doubleBackSlashes($this->_model) . "'}", "#" . $this->identifier . " tbody", [ |
335
|
|
|
"preventDefault" => false, |
336
|
|
|
"jqueryDone" => "replaceWith", |
337
|
|
|
"hasLoader" => "internal", |
338
|
|
|
"jsCallback" => '$("#' . $this->identifier . '").trigger("searchTerminate",[$(self).val()]);' |
339
|
|
|
]); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
protected function _associateSearchFieldBehavior(JsUtils $js = NULL, $offset = null) {} |
344
|
|
|
|
345
|
|
|
protected function _getFieldName($index) { |
346
|
|
|
$fieldName = parent::_getFieldName($index); |
347
|
|
|
if (\is_object($fieldName)) |
348
|
|
|
$fieldName = "field-" . $index; |
349
|
|
|
return $fieldName . "[]"; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
protected function _getFieldCaption($index) { |
353
|
|
|
return null; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
protected function _setToolbarPosition($table, $captions = NULL) { |
357
|
|
|
switch ($this->_toolbarPosition) { |
358
|
|
|
case PositionInTable::BEFORETABLE: |
359
|
|
|
case PositionInTable::AFTERTABLE: |
360
|
|
|
if (isset($this->_compileParts) === false) { |
361
|
|
|
$this->content[$this->_toolbarPosition] = $this->_toolbar; |
362
|
|
|
} |
363
|
|
|
break; |
364
|
|
|
case PositionInTable::HEADER: |
365
|
|
|
case PositionInTable::FOOTER: |
366
|
|
|
case PositionInTable::BODY: |
367
|
|
|
$this->addToolbarRow($this->_toolbarPosition, $table, $captions); |
368
|
|
|
break; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Associates a $callback function after the compilation of the field at $index position |
374
|
|
|
* The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
375
|
|
|
* |
376
|
|
|
* @param int $index |
377
|
|
|
* postion of the compiled field |
378
|
|
|
* @param callable $callback |
379
|
|
|
* function called after the field compilation |
380
|
|
|
* @return DataTable |
381
|
|
|
*/ |
382
|
|
|
public function afterCompile($index, $callback) { |
383
|
|
|
$this->_instanceViewer->afterCompile($index, $callback); |
384
|
|
|
return $this; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
private function addToolbarRow($part, $table, $captions) { |
388
|
|
|
$hasPart = $table->hasPart($part); |
389
|
|
|
if ($hasPart) { |
390
|
|
|
$row = $table->getPart($part)->addRow(\sizeof($captions)); |
391
|
|
|
} else { |
392
|
|
|
$row = $table->getPart($part)->getRow(0); |
393
|
|
|
} |
394
|
|
|
$row->mergeCol(); |
395
|
|
|
$row->setValues([ |
396
|
|
|
$this->_toolbar |
397
|
|
|
]); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* |
402
|
|
|
* {@inheritdoc} |
403
|
|
|
* @see Widget::getHtmlComponent() |
404
|
|
|
* @return HtmlTable |
405
|
|
|
*/ |
406
|
|
|
public function getHtmlComponent() { |
407
|
|
|
return $this->content["table"]; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
public function getUrls() { |
411
|
|
|
return $this->_urls; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Sets the associative array of urls for refreshing, updating or deleting |
416
|
|
|
* think of defining the update zone with the setTargetSelector method |
417
|
|
|
* |
418
|
|
|
* @param string|array $urls |
419
|
|
|
* associative array with keys refresh: for refreshing with search field or pagination, edit : for updating a row, delete: for deleting a row |
420
|
|
|
* @return DataTable |
421
|
|
|
*/ |
422
|
|
|
public function setUrls($urls) { |
423
|
|
|
if (\is_array($urls)) { |
424
|
|
|
$this->_urls["refresh"] = JArray::getValue($urls, "refresh", 0); |
425
|
|
|
$this->_urls["edit"] = JArray::getValue($urls, "edit", 1); |
426
|
|
|
$this->_urls["delete"] = JArray::getValue($urls, "delete", 2); |
427
|
|
|
$this->_urls["display"] = JArray::getValue($urls, "display", 3); |
428
|
|
|
} else { |
429
|
|
|
$this->_urls = [ |
430
|
|
|
"refresh" => $urls, |
431
|
|
|
"edit" => $urls, |
432
|
|
|
"delete" => $urls, |
433
|
|
|
"display" => $urls |
434
|
|
|
]; |
435
|
|
|
} |
436
|
|
|
return $this; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
441
|
|
|
* |
442
|
|
|
* @param number $page |
443
|
|
|
* the active page number |
444
|
|
|
* @param number $total_rowcount |
445
|
|
|
* the total number of items |
446
|
|
|
* @param number $items_per_page |
447
|
|
|
* The number of items per page |
448
|
|
|
* @param number $pages_visibles |
449
|
|
|
* The number of visible pages in the Pagination component |
450
|
|
|
* @return DataTable |
451
|
|
|
*/ |
452
|
|
|
public function paginate($page, $total_rowcount, $items_per_page = 10, $pages_visibles = null) { |
453
|
|
|
$this->_pagination = new Pagination($items_per_page, $pages_visibles, $page, $total_rowcount); |
454
|
|
|
return $this; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Auto Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
459
|
|
|
* |
460
|
|
|
* @param number $page |
461
|
|
|
* the active page number |
462
|
|
|
* @param number $items_per_page |
463
|
|
|
* The number of items per page |
464
|
|
|
* @param number $pages_visibles |
465
|
|
|
* The number of visible pages in the Pagination component |
466
|
|
|
* @return DataTable |
467
|
|
|
*/ |
468
|
|
|
public function autoPaginate($page = 1, $items_per_page = 10, $pages_visibles = 4) { |
469
|
|
|
$this->_pagination = new Pagination($items_per_page, $pages_visibles, $page); |
470
|
|
|
return $this; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* |
475
|
|
|
* @param array $compileParts |
476
|
|
|
* @return DataTable |
477
|
|
|
*/ |
478
|
|
|
public function refresh($compileParts = [ |
479
|
|
|
"tbody" |
480
|
|
|
]) { |
481
|
|
|
$this->_compileParts = $compileParts; |
482
|
|
|
return $this; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Adds a search input in toolbar |
487
|
|
|
* |
488
|
|
|
* @param string $position |
489
|
|
|
* @return \Ajax\common\html\HtmlDoubleElement |
490
|
|
|
*/ |
491
|
|
|
public function addSearchInToolbar($position = Direction::RIGHT) { |
492
|
|
|
return $this->addInToolbar($this->getSearchField()) |
493
|
|
|
->setPosition($position); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
public function getSearchField() { |
497
|
|
|
if (isset($this->_searchField) === false) { |
498
|
|
|
$this->_searchField = new HtmlInput("search-" . $this->identifier, "search", "", "Search..."); |
499
|
|
|
$this->_searchField->addIcon("search", Direction::RIGHT); |
500
|
|
|
} |
501
|
|
|
return $this->_searchField; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* The callback function called after the insertion of each row when fromDatabaseObjects is called |
506
|
|
|
* callback function takes the parameters $row : the row inserted and $object: the instance of model used |
507
|
|
|
* |
508
|
|
|
* @param callable $callback |
509
|
|
|
* @return DataTable |
510
|
|
|
*/ |
511
|
|
|
public function onNewRow($callback) { |
512
|
|
|
$this->content["table"]->onNewRow($callback); |
513
|
|
|
return $this; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Returns a form corresponding to the Datatable |
518
|
|
|
* |
519
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlForm |
520
|
|
|
*/ |
521
|
|
|
public function asForm() { |
522
|
|
|
return $this->getForm(); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
protected function getTargetSelector($op) { |
526
|
|
|
$result = $this->_targetSelector; |
527
|
|
|
if (! isset($result[$op])) |
528
|
|
|
$result = "#" . $this->identifier; |
529
|
|
|
return $result[$op]; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Sets the response element selector for Edit and Delete request with ajax |
534
|
|
|
* |
535
|
|
|
* @param string|array $_targetSelector |
536
|
|
|
* string or associative array ["edit"=>"edit_selector","delete"=>"delete_selector"] |
537
|
|
|
* @return DataTable |
538
|
|
|
*/ |
539
|
|
|
public function setTargetSelector($_targetSelector) { |
540
|
|
|
if (! \is_array($_targetSelector)) { |
541
|
|
|
$_targetSelector = [ |
542
|
|
|
"edit" => $_targetSelector, |
543
|
|
|
"delete" => $_targetSelector |
544
|
|
|
]; |
545
|
|
|
} |
546
|
|
|
$this->_targetSelector = $_targetSelector; |
547
|
|
|
return $this; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
public function getRefreshSelector() { |
551
|
|
|
if (isset($this->_refreshSelector)) |
552
|
|
|
return $this->_refreshSelector; |
553
|
|
|
return "#" . $this->identifier . " tbody"; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* |
558
|
|
|
* @param string $_refreshSelector |
559
|
|
|
* @return DataTable |
560
|
|
|
*/ |
561
|
|
|
public function setRefreshSelector($_refreshSelector) { |
562
|
|
|
$this->_refreshSelector = $_refreshSelector; |
563
|
|
|
return $this; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* |
568
|
|
|
* {@inheritdoc} |
569
|
|
|
* @see \Ajax\common\Widget::show() |
570
|
|
|
*/ |
571
|
|
|
public function show($modelInstance) { |
572
|
|
|
if (\is_array($modelInstance)) { |
573
|
|
|
if (isset($modelInstance[0]) && \is_array(array_values($modelInstance)[0])) |
574
|
|
|
$modelInstance = \json_decode(\json_encode($modelInstance), FALSE); |
575
|
|
|
} |
576
|
|
|
$this->_modelInstance = $modelInstance; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
public function getRowClass() { |
580
|
|
|
return $this->_rowClass; |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* Sets the default row class (tr class) |
585
|
|
|
* |
586
|
|
|
* @param string $_rowClass |
587
|
|
|
* @return DataTable |
588
|
|
|
*/ |
589
|
|
|
public function setRowClass($_rowClass) { |
590
|
|
|
$this->_rowClass = $_rowClass; |
591
|
|
|
return $this; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Sets the message displayed when there is no record |
596
|
|
|
* |
597
|
|
|
* @param mixed $_emptyMessage |
598
|
|
|
* @return DataTable |
599
|
|
|
*/ |
600
|
|
|
public function setEmptyMessage($_emptyMessage) { |
601
|
|
|
$this->_emptyMessage = $_emptyMessage; |
602
|
|
|
return $this; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
public function setSortable($colIndex = NULL) { |
606
|
|
|
$this->_sortable = $colIndex; |
607
|
|
|
return $this; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
public function setActiveRowSelector($class = "active", $event = "click", $multiple = false) { |
611
|
|
|
$this->_self->setActiveRowSelector($class, $event, $multiple); |
612
|
|
|
return $this; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
public function hideColumn($colIndex) { |
616
|
|
|
if (! \is_array($this->_hiddenColumns)) |
617
|
|
|
$this->_hiddenColumns = []; |
618
|
|
|
$this->_hiddenColumns[] = $colIndex; |
619
|
|
|
return $this; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
public function setColWidth($colIndex, $width) { |
623
|
|
|
$this->_colWidths[$colIndex] = $width; |
624
|
|
|
return $this; |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
public function setColWidths($_colWidths) { |
628
|
|
|
$this->_colWidths = $_colWidths; |
629
|
|
|
return $this; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
public function setColAlignment($colIndex, $alignment) { |
633
|
|
|
$this->content["table"]->setColAlignment($colIndex, $alignment); |
634
|
|
|
return $this; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
public function trigger($event, $params = "[]") { |
638
|
|
|
return $this->getHtmlComponent()->trigger($event, $params); |
|
|
|
|
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
public function onActiveRowChange($jsCode) { |
642
|
|
|
$this->getHtmlComponent()->onActiveRowChange($jsCode); |
643
|
|
|
return $this; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* |
648
|
|
|
* @return mixed |
649
|
|
|
*/ |
650
|
|
|
public function getDeleteBehavior() { |
651
|
|
|
return $this->_deleteBehavior; |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* |
656
|
|
|
* @return mixed |
657
|
|
|
*/ |
658
|
|
|
public function getEditBehavior() { |
659
|
|
|
return $this->_editBehavior; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* |
664
|
|
|
* @return mixed |
665
|
|
|
*/ |
666
|
|
|
public function getDisplayBehavior() { |
667
|
|
|
return $this->_displayBehavior; |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* |
672
|
|
|
* @param mixed $_displayBehavior |
673
|
|
|
*/ |
674
|
|
|
public function setDisplayBehavior($_displayBehavior) { |
675
|
|
|
$this->_displayBehavior = $_displayBehavior; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* |
680
|
|
|
* @return mixed |
681
|
|
|
*/ |
682
|
|
|
public function getGroupByFields() { |
683
|
|
|
return $this->_instanceViewer->getGroupByFields(); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* |
688
|
|
|
* @param mixed $_groupByFields |
689
|
|
|
*/ |
690
|
|
|
public function setGroupByFields($_groupByFields) { |
691
|
|
|
$this->_instanceViewer->setGroupByFields($_groupByFields); |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* |
696
|
|
|
* @param boolean $_visibleHover |
697
|
|
|
*/ |
698
|
|
|
public function setVisibleHover($_visibleHover) { |
699
|
|
|
$this->_visibleHover = $_visibleHover; |
700
|
|
|
} |
701
|
|
|
} |
702
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.