|
14 | 14 | use Ajax\semantic\html\collections\HtmlMessage; |
15 | 15 | use Ajax\semantic\html\base\traits\BaseTrait; |
16 | 16 | use Ajax\service\JString; |
| 17 | +use Ajax\common\html\HtmlDoubleElement; |
17 | 18 |
|
18 | 19 | /** |
19 | 20 | * DataTable widget for displaying list of objects |
@@ -166,17 +167,55 @@ protected function _generateContent($table){ |
166 | 167 | } |
167 | 168 | InstanceViewer::setIndex(0); |
168 | 169 | $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 | + } |
172 | 186 | if($table->getRowCount()==0){ |
173 | 187 | $result=$table->addRow(); |
174 | 188 | $result->mergeRow(); |
175 | 189 | $result->setValues([$this->_emptyMessage]); |
176 | 190 | } |
177 | 191 | } |
| 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 | + } |
178 | 217 |
|
179 | | - protected function _generateRow($instance,$fields,&$table,$checkedClass=null){ |
| 218 | + protected function _generateRow($instance,$fields,&$table,$checkedClass=null,$uuids=null){ |
180 | 219 | $this->_instanceViewer->setInstance($instance); |
181 | 220 | InstanceViewer::$index++; |
182 | 221 | $values= $this->_instanceViewer->getValues(); |
@@ -205,6 +244,9 @@ protected function _generateRow($instance,$fields,&$table,$checkedClass=null){ |
205 | 244 | $result->setValues($values); |
206 | 245 | $result->addToProperty("class",$this->_rowClass); |
207 | 246 | $result->setPropertyValues("data-field", $fields); |
| 247 | + if(isset($uuids)){ |
| 248 | + $result->setProperty("data-child",implode(" ", $uuids)); |
| 249 | + } |
208 | 250 | return $result; |
209 | 251 | } |
210 | 252 |
|
@@ -525,6 +567,20 @@ public function getDisplayBehavior() { |
525 | 567 | public function setDisplayBehavior($_displayBehavior) { |
526 | 568 | $this->_displayBehavior = $_displayBehavior; |
527 | 569 | } |
| 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 | + |
528 | 584 |
|
529 | 585 |
|
530 | 586 | } |
0 commit comments