Skip to content

Commit 92ec9e6

Browse files
committed
Add fieldAsDataTable
1 parent 09c8f46 commit 92ec9e6

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Ajax\semantic\widgets\base;
33

4+
use Ajax\semantic\widgets\datatable\DataTable;
45
use Ajax\service\JString;
56
use Ajax\semantic\html\elements\HtmlImage;
67
use Ajax\semantic\html\base\constants\Size;
@@ -376,4 +377,26 @@ public function fieldAsButton($index, $cssStyle = NULL, $attributes = NULL) {
376377
return $button;
377378
}, $index, $attributes, "button");
378379
}
380+
381+
public function fieldAsDataTable($index, $model, $instances = null, $fields = [], $attributes = NULL) {
382+
return $this->_fieldAs(function ($id, $name, $value, $caption) use ($model, $instances, $fields, $index) {
383+
$dt = new DataTable($id, $model, $instances);
384+
$dt->setNamePrefix($index);
385+
$dt->setFields($fields);
386+
$dt->setEdition(true);
387+
$dt->addDeleteButton(false, [], function ($bt) use ($index) {
388+
$bt->addClass('mini circular')
389+
->wrap('<input value="" class="_status" type="hidden" name="' . $index . '._status[]">');
390+
});
391+
if ($caption != null) {
392+
$dt->setFormCaption($caption);
393+
}
394+
$dt->onPreCompile(function () use (&$dt) {
395+
$dt->getHtmlComponent()
396+
->colRightFromRight(0);
397+
});
398+
$dt->wrap('<input type="hidden" name="' . $index . '">');
399+
return $dt;
400+
}, $index, $attributes, "dataTable");
401+
}
379402
}

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,22 @@ protected function showableProperty(\ReflectionProperty $rProperty) {
250250
}
251251

252252
public function setInstance($instance) {
253-
if (\is_string($instance)) {
253+
if (\is_string($instance) && \class_exists($instance)) {
254254
$instance = new $instance();
255255
}
256256
$this->instance = $instance;
257257
$this->properties = [];
258-
$this->reflect = new \ReflectionClass($instance);
259-
if (JArray::count($this->visibleProperties) === 0) {
260-
$this->properties = $this->getDefaultProperties();
261-
} else {
262-
foreach ($this->visibleProperties as $property) {
263-
$this->setInstanceProperty($property);
258+
try{
259+
$this->reflect = new \ReflectionClass($instance);
260+
if (JArray::count($this->visibleProperties) === 0) {
261+
$this->properties = $this->getDefaultProperties();
262+
} else {
263+
foreach ($this->visibleProperties as $property) {
264+
$this->setInstanceProperty($property);
265+
}
264266
}
267+
}catch (\Throwable $th){
268+
265269
}
266270
return $this;
267271
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class DataTable extends Widget {
6060
protected $_colWidths;
6161

6262
protected $_paginationToolbar;
63+
64+
protected $_caption;
65+
66+
protected $_namePrefix;
6367

6468
public function __construct($identifier, $model, $modelInstance = NULL) {
6569
parent::__construct($identifier, $model, $modelInstance);
@@ -93,8 +97,9 @@ public function run(JsUtils $js) {
9397
$this->_generateBehavior("display", $this->_displayBehavior, $js);
9498
}
9599
parent::run($js);
96-
if (isset($this->_pagination))
100+
if (isset($this->_pagination)) {
97101
$this->_associatePaginationBehavior($js, $offset);
102+
}
98103
$this->_associateSearchFieldBehavior($js, $offset);
99104
$this->_runned = true;
100105
}
@@ -158,6 +163,9 @@ public function compile(JsUtils $js = NULL, &$view = NULL) {
158163
"table",
159164
PositionInTable::AFTERTABLE
160165
]);
166+
if($this->_caption!=null){
167+
$this->wrap("<div class='field'><label>{$this->_caption}</label>","</div>");
168+
}
161169
$this->_compileForm();
162170
$this->_applyStyleAttributes($table);
163171
$this->_generated = true;
@@ -357,6 +365,9 @@ protected function _getFieldName($index) {
357365
$fieldName = parent::_getFieldName($index);
358366
if (\is_object($fieldName))
359367
$fieldName = "field-" . $index;
368+
if($this->_namePrefix!=null){
369+
$fieldName=$this->_namePrefix.'.'.$fieldName;
370+
}
360371
return $fieldName . "[]";
361372
}
362373

@@ -749,4 +760,22 @@ public function setInverted($recursive = true) {
749760
public function setFocusable(bool $focusable) {
750761
$this->content["table"]->setFocusable($focusable);
751762
}
763+
764+
public function setFormCaption($caption){
765+
$this->_caption=$caption;
766+
}
767+
768+
/**
769+
* @return mixed
770+
*/
771+
public function getNamePrefix() {
772+
return $this->_namePrefix;
773+
}
774+
775+
/**
776+
* @param mixed $namePrefix
777+
*/
778+
public function setNamePrefix($namePrefix): void {
779+
$this->_namePrefix = $namePrefix;
780+
}
752781
}

Ajax/semantic/widgets/datatable/DataTableFieldAsTrait.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ private function getCallable($thisCallback,$parameters,$callback=null){
6565
* @param string $caption
6666
* @return HtmlButton
6767
*/
68-
private function getFieldButton($caption,$visibleHover=true){
69-
$bt= new HtmlButton($this->cleanIdentifier($caption),$caption);
68+
private function getFieldButton($caption,$visibleHover=true,$icon=null){
69+
$id=$this->_instanceViewer->getIdentifier();
70+
$bt= new HtmlButton($this->cleanIdentifier($icon??$caption).'-'.$id,$caption);
7071
if($visibleHover)
7172
$this->_visibleOver($bt);
7273
return $bt;
@@ -162,7 +163,7 @@ public function insertDefaultButtonIn($index,$icon,$class=null,$visibleHover=tru
162163
}
163164

164165
private function getDefaultButton($icon,$class=null,$visibleHover=true){
165-
$bt=$this->getFieldButton("",$visibleHover);
166+
$bt=$this->getFieldButton("",$visibleHover,$icon);
166167
$bt->asIcon($icon);
167168
if(isset($class))
168169
$bt->addClass($class);

0 commit comments

Comments
 (0)