Skip to content

Commit 5f2e51b

Browse files
committed
DataTable identifier bug fixed
1 parent e0aff5f commit 5f2e51b

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

Ajax/semantic/html/collections/HtmlTable.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Ajax\JsUtils;
99

1010
use Ajax\service\JArray;
11+
use Ajax\semantic\html\content\table\HtmlTR;
1112

1213
/**
1314
* Semantic HTML Table component
@@ -18,13 +19,15 @@ class HtmlTable extends HtmlSemDoubleElement {
1819
private $_colCount;
1920
private $_compileParts;
2021
private $_footer;
22+
private $_afterCompileEvents;
2123

2224
public function __construct($identifier, $rowCount, $colCount) {
2325
parent::__construct($identifier, "table", "ui table");
2426
$this->content=array ();
2527
$this->setRowCount($rowCount, $colCount);
2628
$this->_variations=[ Variation::CELLED,Variation::PADDED,Variation::COMPACT ];
2729
$this->_compileParts=["thead","tbody","tfoot"];
30+
$this->_afterCompileEvents=[];
2831
}
2932

3033
/**
@@ -108,12 +111,12 @@ public function getRow($rowIndex) {
108111
/**
109112
* Adds a new row and sets $values to his cols
110113
* @param array $values
111-
* @return \Ajax\semantic\html\collections\HtmlTable
114+
* @return HtmlTR
112115
*/
113116
public function addRow($values=array()) {
114117
$row=$this->getBody()->addRow($this->_colCount);
115118
$row->setValues(\array_values($values));
116-
return $this;
119+
return $row;
117120
}
118121

119122
/**
@@ -278,10 +281,15 @@ public function compile(JsUtils $js=NULL, &$view=NULL) {
278281
public function fromDatabaseObject($object, $function) {
279282
$result=$function($object);
280283
if (\is_array($result)) {
281-
return $this->addRow($function($object));
284+
$result= $this->addRow($function($object));
282285
} else {
283-
return $this->getBody()->_addRow($result);
286+
$result= $this->getBody()->_addRow($result);
287+
}
288+
if(isset($this->_afterCompileEvents["onNewRow"])){
289+
if(\is_callable($this->_afterCompileEvents["onNewRow"]))
290+
$this->_afterCompileEvents["onNewRow"]($result,$object);
284291
}
292+
return $result;
285293
}
286294

287295
/**
@@ -305,4 +313,14 @@ public function run(JsUtils $js){
305313
return $result;
306314
}
307315

316+
/**
317+
* The callback function called after the insertion of each row when fromDatabaseObjects is called
318+
* callback function takes the parameters $row : the row inserted and $object: the instance of model used
319+
* @param callable $callback
320+
* @return \Ajax\semantic\html\collections\HtmlTable
321+
*/
322+
public function onNewRow($callback) {
323+
$this->_afterCompileEvents["onNewRow"]=$callback;
324+
return $this;
325+
}
308326
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Ajax\semantic\html\base\constants\Direction;
1414
use Ajax\service\JArray;
1515
use Ajax\semantic\widgets\base\FieldAsTrait;
16+
use Ajax\semantic\html\base\HtmlSemDoubleElement;
1617

1718
/**
1819
* DataTable widget for displaying list of objects
@@ -94,6 +95,7 @@ private function _generateContent($table){
9495
InstanceViewer::setIndex(0);
9596
$table->fromDatabaseObjects($objects, function($instance){
9697
$this->_instanceViewer->setInstance($instance);
98+
InstanceViewer::$index++;
9799
$result= $this->_instanceViewer->getValues();
98100
if($this->_hasCheckboxes){
99101
$ck=new HtmlCheckbox("ck-".$this->identifier,"");
@@ -227,21 +229,25 @@ public function refresh($compileParts=["tbody"]){
227229
* @return callable
228230
*/
229231
private function getFieldButtonCallable($caption,$callback=null){
230-
return $this->getCallable($this->getFieldButton($caption),$callback);
232+
return $this->getCallable("getFieldButton",[$caption],$callback);
231233
}
232234

233235
/**
234236
* @param mixed $object
235237
* @param callable $callback
236238
* @return callable
237239
*/
238-
private function getCallable($object,$callback=null){
239-
$result=function($instance) use($object,$callback){
240+
private function getCallable($thisCallback,$parameters,$callback=null){
241+
$result=function($instance) use($thisCallback,$parameters,$callback){
242+
$object=call_user_func_array(array($this,$thisCallback), $parameters);
240243
if(isset($callback)){
241244
if(\is_callable($callback)){
242245
$callback($object,$instance);
243246
}
244247
}
248+
if($object instanceof HtmlSemDoubleElement){
249+
$object->setProperty("data-ajax",$this->_instanceViewer->getIdentifier());
250+
}
245251
return $object;
246252
};
247253
return $result;
@@ -252,9 +258,7 @@ private function getCallable($object,$callback=null){
252258
* @return HtmlButton
253259
*/
254260
private function getFieldButton($caption){
255-
$bt=new HtmlButton("",$caption);
256-
$bt->setProperty("data-ajax",$this->_instanceViewer->getIdentifier());
257-
return $bt;
261+
return new HtmlButton("",$caption);
258262
}
259263

260264
/**
@@ -264,7 +268,7 @@ private function getFieldButton($caption){
264268
* @return \Ajax\semantic\widgets\datatable\DataTable
265269
*/
266270
public function addFieldButton($caption,$callback=null){
267-
$this->addField($this->getFieldButtonCallable($caption,$callback));
271+
$this->addField($this->getCallable("getFieldButton",[$caption],$callback));
268272
return $this;
269273
}
270274

@@ -293,14 +297,12 @@ public function insertInFieldButton($index,$caption,$callback=null){
293297
}
294298

295299
private function addDefaultButton($icon,$class=null,$callback=null){
296-
$bt=$this->getDefaultButton($icon,$class);
297-
$this->addField($this->getCallable($bt,$callback));
300+
$this->addField($this->getCallable("getDefaultButton",[$icon,$class],$callback));
298301
return $this;
299302
}
300303

301304
private function insertDefaultButtonIn($index,$icon,$class=null,$callback=null){
302-
$bt=$this->getDefaultButton($icon,$class);
303-
$this->insertInField($index,$this->getCallable($bt,$callback));
305+
$this->insertInField($index,$this->getCallable("getDefaultButton",[$icon,$class],$callback));
304306
return $this;
305307
}
306308

@@ -399,4 +401,15 @@ public function setSortable($colIndex=NULL) {
399401
protected function _getFieldIdentifier($prefix){
400402
return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
401403
}
404+
405+
/**
406+
* The callback function called after the insertion of each row when fromDatabaseObjects is called
407+
* callback function takes the parameters $row : the row inserted and $object: the instance of model used
408+
* @param callable $callback
409+
* @return DataTable
410+
*/
411+
public function onNewRow($callback) {
412+
$this->content["table"]->onNewRow($callback);
413+
return $this;
414+
}
402415
}

Ajax/semantic/widgets/datatable/InstanceViewer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class InstanceViewer {
1010
private $visibleProperties;
1111
private $values;
1212
private $afterCompile;
13-
private static $index=0;
13+
public static $index=0;
1414

1515
public function __construct($instance=NULL,$captions=NULL){
1616
$this->values=[];
@@ -60,7 +60,6 @@ public function getIdentifier(){
6060
$value=self::$index;
6161
if(isset($this->values["identifier"]))
6262
$value=$this->values["identifier"](self::$index,$this->instance);
63-
self::$index++;
6463
return $value;
6564
}
6665

0 commit comments

Comments
 (0)