phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
HtmlTable.php
Go to the documentation of this file.
1<?php
3
14
22
23 private $_colCount;
24
26
27 private $_footer;
28
30
32
33 private $_focusable = false;
34
39 public function getActiveRowSelector() {
41 }
42
43 protected $_innerScript;
44
45 public function __construct($identifier, $rowCount, $colCount) {
46 parent::__construct($identifier, "table", "ui table");
47 $this->content = array();
48 $this->setRowCount($rowCount, $colCount);
49 $this->_variations = [
53 ];
54 $this->_compileParts = [
55 "thead",
56 "tbody",
57 "tfoot"
58 ];
59 $this->_afterCompileEvents = [];
60 }
61
68 public function getPart($key) {
69 if (\array_key_exists($key, $this->content) === false) {
70 $this->content[$key] = new HtmlTableContent("", $key);
71 if ($key !== "tbody") {
72 $this->content[$key]->setRowCount(1, $this->_colCount);
73 }
74 }
75 return $this->content[$key];
76 }
77
78 protected function _getFirstPart() {
79 if (isset($this->content["thead"])) {
80 return $this->content["thead"];
81 }
82 return $this->content["tbody"];
83 }
84
90 public function getBody() {
91 return $this->getPart("tbody");
92 }
93
99 public function getRowCount() {
100 return $this->getPart("tbody")->count();
101 }
102
108 public function getHeader() {
109 return $this->getPart("thead");
110 }
111
117 public function getFooter() {
118 return $this->getPart("tfoot");
119 }
120
127 public function hasPart($key) {
128 return \array_key_exists($key, $this->content) === true;
129 }
130
137 public function setRowCount($rowCount, $colCount) {
138 $this->_colCount = $colCount;
139 return $this->getBody()->setRowCount($rowCount, $colCount);
140 }
141
149 public function getCell($row, $col) {
150 return $this->getBody()->getCell($row, $col);
151 }
152
159 public function getRow($rowIndex) {
160 return $this->getBody()->getRow($rowIndex);
161 }
162
169 public function addRow($values = array()) {
170 $row = $this->getBody()->addRow($this->_colCount);
171 $row->setValues(\array_values($values));
172 return $row;
173 }
174
180 public function newRow() {
181 return $this->getBody()->newRow($this->_colCount);
182 }
183
191 public function setValues($values = array()) {
192 $this->getBody()->setValues($values);
193 return $this;
194 }
195
202 public function setHeaderValues($values = array()) {
203 return $this->getHeader()->setValues($values);
204 }
205
212 public function setFooterValues($values = array()) {
213 return $this->getFooter()->setValues($values);
214 }
215
223 public function setColValues($colIndex, $values = array()) {
224 $this->getBody()->setColValues($colIndex, $values);
225 return $this;
226 }
227
235 public function setRowValues($rowIndex, $values = array()) {
236 $this->getBody()->setRowValues($rowIndex, $values);
237 return $this;
238 }
239
240 public function addColVariations($colIndex, $variations = array()) {
241 return $this->getBody()->addColVariations($colIndex, $variations);
242 }
243
250 public function colCenter($colIndex) {
251 return $this->colAlign($colIndex, "colCenter");
252 }
253
260 public function colRight($colIndex) {
261 return $this->colAlign($colIndex, "colRight");
262 }
263
270 public function colLeft($colIndex) {
271 return $this->colAlign($colIndex, "colLeft");
272 }
273
280 public function colCenterFromRight($colIndex) {
281 return $this->colAlign($colIndex, "colCenterFromRight");
282 }
283
290 public function colRightFromRight($colIndex) {
291 return $this->colAlign($colIndex, "colRightFromRight");
292 }
293
300 public function colLeftFromRight($colIndex) {
301 return $this->colAlign($colIndex, "colLeftFromRight");
302 }
303
304 public function setColAlignment($colIndex, $alignment) {
305 switch ($alignment) {
306 case TextAlignment::LEFT:
307 $function = "colLeft";
308 break;
309
310 case TextAlignment::RIGHT:
311 $function = "colRight";
312 break;
313
314 case TextAlignment::CENTER:
315 $function = "colCenter";
316 break;
317
318 default:
319 $function = "colLeft";
320 }
321 $this->colAlign($colIndex, $function);
322 return $this;
323 }
324
325 public function setColAlignmentFromRight($colIndex, $alignment) {
326 switch ($alignment) {
327 case TextAlignment::LEFT:
328 $function = "colLeftFromRight";
329 break;
330
331 case TextAlignment::RIGHT:
332 $function = "colRightFromRight";
333 break;
334
335 case TextAlignment::CENTER:
336 $function = "colCenterFromRight";
337 break;
338
339 default:
340 $function = "colLeftFromRight";
341 }
342 $this->colAlign($colIndex, $function);
343 return $this;
344 }
345
346 private function colAlign($colIndex, $function) {
347 if (\is_array($colIndex)) {
348 foreach ($colIndex as $cIndex) {
349 $this->colAlign($cIndex, $function);
350 }
351 } else {
352 if ($this->hasPart("thead")) {
353 $this->getHeader()->$function($colIndex);
354 }
355 $this->getBody()->$function($colIndex);
356 }
357 return $this;
358 }
359
369 public function conditionalCellFormat($callback, $format) {
370 $this->getBody()->conditionalCellFormat($callback, $format);
371 return $this;
372 }
373
383 public function conditionalRowFormat($callback, $format) {
384 $this->getBody()->conditionalRowFormat($callback, $format);
385 return $this;
386 }
387
394 public function applyCells($callback) {
395 $this->getBody()->applyCells($callback);
396 return $this;
397 }
398
405 public function applyRows($callback) {
406 $this->getBody()->applyRows($callback);
407 return $this;
408 }
409
416 public function compile(JsUtils $js = NULL, &$view = NULL) {
417 if (\sizeof($this->_compileParts) < 3) {
418 $this->_template = "%content%";
419 $this->refresh($js);
420 }
421 $this->content = JArray::sortAssociative($this->content, $this->_compileParts);
422 return parent::compile($js, $view);
423 }
424
425 protected function compile_once(JsUtils $js = NULL, &$view = NULL) {
426 parent::compile_once($js, $view);
427 if ($this->propertyContains("class", "sortable")) {
428 $this->addEvent("execute", "$('#" . $this->identifier . "').tablesort().data('tablesort').sort($('th.default-sort'));");
429 }
430 }
431
438 public function fromDatabaseObject($object, $function) {
439 $result = $function($object);
440 if (\is_array($result)) {
441 $result = $this->addRow($function($object));
442 } else {
443 $result = $this->getBody()->_addRow($result);
444 }
445 if (isset($this->_afterCompileEvents["onNewRow"])) {
446 if (\is_callable($this->_afterCompileEvents["onNewRow"]))
447 $this->_afterCompileEvents["onNewRow"]($result, $object);
448 }
449 return $result;
450 }
451
459 public function setCompileParts($parts = [
460 "tbody"
461 ]) {
462 $this->_compileParts = $parts;
463 return $this;
464 }
465
466 public function refreshTR() {
467 $this->setCompileParts();
468 $this->getPart("tbody")->refreshTR();
469 }
470
471 public function refresh($js) {
472 $this->_footer = $this->getFooter();
473 if (isset($js)) {
474 $js->exec('$("#' . $this->identifier . ' tfoot").replaceWith("' . \addslashes($this->_footer) . '");', true);
475 }
476 }
477
478 public function run(JsUtils $js) {
479 if (! $this->_runned) {
480 if (isset($this->_activeRowSelector)) {
481 $this->_activeRowSelector->run();
482 }
483 }
484 $result = parent::run($js);
485 if (isset($this->_footer))
486 $this->_footer->run($js);
487 $this->_runned = true;
488 return $result;
489 }
490
498 public function onNewRow($callback) {
499 $this->_afterCompileEvents["onNewRow"] = $callback;
500 return $this;
501 }
502
511 public function setActiveRowSelector($class = "active", $event = "click", $multiple = false) {
512 $this->_activeRowSelector = new ActiveRow($this, $class, $event, $multiple);
513 return $this;
514 }
515
516 public function getActiveRowClass() {
517 if (isset($this->_activeRowSelector)) {
518 return $this->_activeRowSelector->getClass();
519 }
520 return 'active';
521 }
522
523 public function hasActiveRowSelector() {
524 return isset($this->_activeRowSelector);
525 }
526
527 public function hideColumn($colIndex) {
528 if (isset($this->content["thead"])) {
529 $this->content["thead"]->hideColumn($colIndex);
530 }
531 $this->content["tbody"]->hideColumn($colIndex);
532 if (isset($this->content["tfoot"])) {
533 $this->content["tfoot"]->hideColumn($colIndex);
534 }
535 return $this;
536 }
537
538 public function setColWidth($colIndex, $width) {
539 $part = $this->_getFirstPart();
540 if ($part !== null && $part->count() > 0)
541 $part->getCell(0, $colIndex)->setWidth($width);
542 return $this;
543 }
544
545 public function setColWidths($widths) {
546 $part = $this->_getFirstPart();
547 if ($part !== null && $part->count() > 0) {
548 $count = $part->getColCount();
549 if (! \is_array($widths)) {
550 $widths = \array_fill(0, $count, $widths);
551 }
552 $max = \min(\sizeof($widths), $count);
553 for ($i = 0; $i < $max; $i ++) {
554 $part->getCell(0, $i)->setWidth($widths[$i]);
555 }
556 }
557 return $this;
558 }
559
560 public function mergeIdentiqualValues($colIndex, $function = "strip_tags") {
561 $body = $this->getBody();
562 $body->mergeIdentiqualValues($colIndex, $function);
563 return $this;
564 }
565
570 public function getInnerScript() {
571 return $this->_innerScript;
572 }
573
578 public function setInnerScript($_innerScript) {
579 $this->_innerScript = $_innerScript;
580 }
581
582 public function onActiveRowChange($jsCode) {
583 $this->on("activeRowChange", $jsCode);
584 return $this;
585 }
586
587 public function addMergeRow($colCount, $value = null) {
588 return $this->getBody()->addMergeRow($colCount, $value);
589 }
590
595 public function setFocusable(bool $focusable): void {
596 $this->getBody()->setFocusable($focusable);
597 }
598}
JQuery PHP library.
Definition JsUtils.php:23
BaseHtml for HTML components.
Definition BaseHtml.php:17
on($event, $jsCode, $stopPropagation=false, $preventDefault=false)
Base class for Semantic double elements.
A class for row selection in Table or DataTable.
Definition ActiveRow.php:10
setFooterValues($values=array())
Sets the footer values.
colLeftFromRight($colIndex)
Sets col alignment to left.
setHeaderValues($values=array())
Sets the header values.
applyCells($callback)
Applies a callback function on each cell.
mergeIdentiqualValues($colIndex, $function="strip_tags")
run(JsUtils $js)
{{SimpleExtComponent\Ajax\common\html\BaseHtmlrun()\Ajax\common\html\BaseHtmlrun()}HtmlDoubleElement:...
conditionalRowFormat($callback, $format)
Applies a format on each row when $callback returns true.
addRow($values=array())
Adds a new row and sets $values to his cols.
colLeft($colIndex)
Sets col alignment to left.
setActiveRowSelector($class="active", $event="click", $multiple=false)
Defines how a row is selectable.
addColVariations($colIndex, $variations=array())
conditionalCellFormat($callback, $format)
Applies a format on each cell when $callback returns true.
applyRows($callback)
Applies a callback function on each row.
hasPart($key)
Checks if the part corresponding to $key exists.
__construct($identifier, $rowCount, $colCount)
Definition HtmlTable.php:45
colCenterFromRight($colIndex)
Sets the col alignment to center.
setColValues($colIndex, $values=array())
Sets values to the col at index $colIndex.
getHeader()
Returns/create eventually the header of the table.
getRow($rowIndex)
Retuns the row at $rowIndex.
getCell($row, $col)
Returns the cell (HtmlTD) at position $row,$col.
compile(JsUtils $js=NULL, &$view=NULL)
{{}BaseHtml::compile()}
getPart($key)
Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot.
Definition HtmlTable.php:68
compile_once(JsUtils $js=NULL, &$view=NULL)
colRight($colIndex)
Sets the col alignment to right.
colRightFromRight($colIndex)
Sets the col alignment to right.
getRowCount()
Returns the number of rows (TR)
Definition HtmlTable.php:99
getBody()
Returns/create eventually the body of the table.
Definition HtmlTable.php:90
onNewRow($callback)
The callback function called after the insertion of each row when fromDatabaseObjects is called callb...
setRowValues($rowIndex, $values=array())
Sets values to the row at index $rowIndex.
setCompileParts($parts=["tbody"])
Sets the parts of the Table to compile.
colCenter($colIndex)
Sets the col alignment to center.
setValues($values=array())
Sets the tbody values.
getFooter()
Returns/create eventually the footer of the table.
addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false)
a table content (thead, tbody or tfoot)