Skip to content

Commit 008e10b

Browse files
committed
Add datatable groupByFields
1 parent 331007f commit 008e10b

File tree

6 files changed

+132
-11
lines changed

6 files changed

+132
-11
lines changed

Ajax/common/html/HtmlDoubleElement.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,11 @@ public function asLink($href=NULL,$target=NULL) {
9696
$this->setProperty("target", $target);
9797
return $this->setTagName("a");
9898
}
99+
100+
public function getTextContent(){
101+
if(is_array($this->content)){
102+
return strip_tags(implode("", $this->content));
103+
}
104+
return strip_tags($this->content);
105+
}
99106
}

Ajax/semantic/html/collections/table/HtmlTable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,5 +508,9 @@ public function onActiveRowChange($jsCode){
508508
$this->on("activeRowChange",$jsCode);
509509
return $this;
510510
}
511+
512+
public function addMergeRow($colCount,$value=null){
513+
return $this->getBody()->addMergeRow($colCount,$value);
514+
}
511515

512516
}

Ajax/semantic/html/content/table/HtmlTableContent.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ public function _addRow($row) {
8686
return $this->addItem($row);
8787
}
8888

89+
public function addMergeRow($colCount,$value=null){
90+
$row=$this->addRow($colCount);
91+
$row->mergeCol();
92+
if(isset($value)){
93+
$row->setValues([$value]);
94+
}
95+
return $row;
96+
}
97+
8998
/**
9099
* @return HtmlTR
91100
*/
@@ -219,8 +228,10 @@ private function colAlignFromRight($colIndex, $function) {
219228
for($i=0; $i < $count; $i++) {
220229
$maxRow=$this->content[$i]->count();
221230
$index=$maxRow-$colIndex-1;
222-
if (($cell=$this->getCell($i, $index))!== NULL)
223-
$cell->$function();
231+
if (($cell=$this->getCell($i, $index))!== NULL){
232+
if($cell->getColspan()==1)
233+
$cell->$function();
234+
}
224235
}
225236
return $this;
226237
}

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class InstanceViewer {
1515
protected $captions;
1616
protected $captionCallback;
1717
protected $defaultValueFunction;
18+
protected $groupByFields;
1819

1920

2021
public static $index=0;
@@ -55,8 +56,18 @@ public function getValues(){
5556
$values=[];
5657
$index=0;
5758
$count=$this->count();
58-
while($index<$count){
59-
$values[]=$this->getValue($index++);
59+
$hasGroupby=is_array($this->groupByFields);
60+
if(!$hasGroupby){
61+
while($index<$count){
62+
$values[]=$this->getValue($index++);
63+
}
64+
}else{
65+
while($index<$count){
66+
if(array_search($index, $this->groupByFields)===false){
67+
$values[]=$this->getValue($index);
68+
}
69+
$index++;
70+
}
6071
}
6172
return $values;
6273
}
@@ -300,7 +311,8 @@ public function getCaption($index){
300311
}
301312

302313
public function getCaptions(){
303-
$count=$this->count();
314+
$hasGroupby=is_array($this->groupByFields);
315+
$count=$this->count()-$this->getGroupByFieldsCount();
304316
if(isset($this->captions)){
305317
$captions= \array_values($this->captions);
306318
$captionsSize=\sizeof($captions);
@@ -310,8 +322,17 @@ public function getCaptions(){
310322
}else{
311323
$captions=[];
312324
$index=0;
313-
while($index<$count){
314-
$captions[]=$this->getCaption($index++);
325+
if(!$hasGroupby){
326+
while($index<$count){
327+
$captions[]=$this->getCaption($index++);
328+
}
329+
}else{
330+
while($index<$count){
331+
if($hasGroupby && array_search($index, $this->groupByFields)===false){
332+
$captions[]=$this->getCaption($index);
333+
}
334+
$index++;
335+
}
315336
}
316337
}
317338
if(isset($this->captionCallback) && \is_callable($this->captionCallback)){
@@ -382,4 +403,25 @@ public function getSimpleProperties() {
382403
public function getDefaultValueFunction() {
383404
return $this->defaultValueFunction;
384405
}
406+
/**
407+
* @return mixed
408+
*/
409+
public function getGroupByFields() {
410+
return $this->groupByFields;
411+
}
412+
413+
/**
414+
* @param mixed $groupByFields
415+
*/
416+
public function setGroupByFields($groupByFields) {
417+
$this->groupByFields = $groupByFields;
418+
}
419+
420+
public function getGroupByFieldsCount(){
421+
if(is_array($this->groupByFields)){
422+
return sizeof($this->groupByFields);
423+
}
424+
return 0;
425+
}
426+
385427
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Ajax\semantic\html\collections\HtmlMessage;
1515
use Ajax\semantic\html\base\traits\BaseTrait;
1616
use Ajax\service\JString;
17+
use Ajax\common\html\HtmlDoubleElement;
1718

1819
/**
1920
* DataTable widget for displaying list of objects
@@ -166,17 +167,55 @@ protected function _generateContent($table){
166167
}
167168
InstanceViewer::setIndex(0);
168169
$fields=$this->_instanceViewer->getSimpleProperties();
169-
$table->fromDatabaseObjects($objects, function($instance) use($table,$fields){
170-
return $this->_generateRow($instance, $fields,$table);
171-
});
170+
if(!is_array($this->_instanceViewer->getGroupByFields())){
171+
$table->fromDatabaseObjects($objects, function($instance) use($table,$fields){
172+
return $this->_generateRow($instance, $fields,$table);
173+
});
174+
}else{
175+
$groupByFields=$this->_instanceViewer->getGroupByFields();
176+
$activeValues=array_combine($groupByFields, array_fill(0, sizeof($groupByFields), null));
177+
$uuids=[];
178+
$table->fromDatabaseObjects($objects, function($instance) use($table,$fields,&$activeValues,$groupByFields,&$uuids){
179+
$this->_instanceViewer->setInstance($instance);
180+
foreach ($groupByFields as $index=>$gbField){
181+
$this->_generateGroupByRow($index, $gbField, $table,$fields,$activeValues, $uuids);
182+
}
183+
return $this->_generateRow($instance, $fields,$table,null,$uuids);
184+
});
185+
}
172186
if($table->getRowCount()==0){
173187
$result=$table->addRow();
174188
$result->mergeRow();
175189
$result->setValues([$this->_emptyMessage]);
176190
}
177191
}
192+
193+
protected function _generateGroupByRow($index,$gbField,$table,$fields,&$activeValues,&$uuids){
194+
$newValue=$this->_instanceViewer->getValue($gbField);
195+
if($this->getElementContent($activeValues[$gbField])!==$this->getElementContent($newValue)){
196+
if($index==0){
197+
$uuids=[];
198+
}
199+
$uuid=uniqid("grp");
200+
$uuids[$gbField]=$uuid;
201+
$id=$this->_instanceViewer->getIdentifier();
202+
$result=$table->addMergeRow(sizeof($fields)+1,$newValue);
203+
$result->setIdentifier($this->identifier."-tr-gb-".$id);
204+
$result->setProperty("data-ajax",$id);
205+
$result->setProperty("data-group",$uuid);
206+
$result->addToProperty("class",$this->_rowClass);
207+
$activeValues[$gbField]=$newValue;
208+
}
209+
}
210+
211+
private function getElementContent($elm){
212+
if($elm instanceof HtmlDoubleElement){
213+
return $elm->getTextContent();
214+
}
215+
return $elm;
216+
}
178217

179-
protected function _generateRow($instance,$fields,&$table,$checkedClass=null){
218+
protected function _generateRow($instance,$fields,&$table,$checkedClass=null,$uuids=null){
180219
$this->_instanceViewer->setInstance($instance);
181220
InstanceViewer::$index++;
182221
$values= $this->_instanceViewer->getValues();
@@ -205,6 +244,9 @@ protected function _generateRow($instance,$fields,&$table,$checkedClass=null){
205244
$result->setValues($values);
206245
$result->addToProperty("class",$this->_rowClass);
207246
$result->setPropertyValues("data-field", $fields);
247+
if(isset($uuids)){
248+
$result->setProperty("data-child",implode(" ", $uuids));
249+
}
208250
return $result;
209251
}
210252

@@ -525,6 +567,20 @@ public function getDisplayBehavior() {
525567
public function setDisplayBehavior($_displayBehavior) {
526568
$this->_displayBehavior = $_displayBehavior;
527569
}
570+
/**
571+
* @return mixed
572+
*/
573+
public function getGroupByFields() {
574+
return $this->_instanceViewer->getGroupByFields();
575+
}
576+
577+
/**
578+
* @param mixed $_groupByFields
579+
*/
580+
public function setGroupByFields($_groupByFields) {
581+
$this->_instanceViewer->setGroupByFields($_groupByFields);
582+
}
583+
528584

529585

530586
}

Ajax/semantic/widgets/datatable/JsonDataTable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function _associatePaginationBehavior(JsUtils $js=NULL,$offset=null){
5959
$callback.=$js->trigger("#".$id." [name='selection[]']","change",false)."$('#".$id." tbody .ui.checkbox').checkbox();".$js->execOn("change", "#".$id." [name='selection[]']", $this->_getCheckedChange($js));
6060
$callback.=$this->_generatePaginationScript($id);
6161
if(isset($this->_urls["refresh"])){
62+
if(isset($menu))
6263
$js->jsonArrayOn("click", "#".$menu->getIdentifier()." a","#".$this->_identifier." tr.".$this->_modelClass, $this->_urls["refresh"],"post",["params"=>"{'p':$(this).attr('data-page'),'_model':'".JString::doubleBackSlashes($this->_model)."'}","jsCallback"=>$callback]);
6364
}
6465
}

0 commit comments

Comments
 (0)