Skip to content

Commit 8f54ccd

Browse files
committed
_getValue refactoring + hiddenColumns in DataTable
1 parent e1ce60b commit 8f54ccd

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ public function fieldAsRating($index,$max=5, $icon=""){
114114
return $this;
115115
}
116116

117-
public function fieldAsLabel($index,$icon=NULL){
118-
$this->setValueFunction($index,function($caption) use($icon){
119-
$lbl=$this->_getLabelField($caption,$icon);
120-
return $lbl;
121-
});
122-
return $this;
117+
public function fieldAsLabel($index,$icon=NULL,$attributes=NULL){
118+
return $this->_fieldAs(function($id,$name,$value) use($icon){
119+
$lbl=new HtmlLabel($id,$value);
120+
if(isset($icon))
121+
$lbl->addIcon($icon);
122+
return $lbl;
123+
}, $index,$attributes,"label");
123124
}
124125

125126
public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,33 +90,37 @@ protected function _getDefaultValue($name,$value,$index){
9090

9191
protected function _getPropertyValue(\ReflectionProperty $property,$index){
9292
$property->setAccessible(true);
93-
$value=$property->getValue($this->instance);
94-
if(isset($this->values[$index])){
95-
$value= $this->values[$index]($value,$this->instance,$index);
96-
}else{
97-
$value=$this->_getDefaultValue($property->getName(),$value, $index);
98-
}
99-
return $value;
93+
return $property->getValue($this->instance);
10094
}
10195

10296
protected function _getValue($property,$index){
10397
$value=null;
98+
$propertyName=$property;
10499
if($property instanceof \ReflectionProperty){
105100
$value=$this->_getPropertyValue($property, $index);
106-
}else{
107-
if(\is_callable($property))
108-
$value=$property($this->instance);
109-
elseif(\is_array($property)){
110-
$values=\array_map(function($v) use ($index){return $this->_getValue($v, $index);}, $property);
111-
$value=\implode("", $values);
112-
}else{
113-
if(isset($this->values[$index])){
114-
$value= $this->values[$index]($property,$this->instance,$index);
115-
}elseif(isset($this->instance->{$property})){
116-
$value=$this->instance->{$property};
117-
}
101+
$propertyName=$property->getName();
102+
}elseif(\is_callable($property))
103+
$value=$property($this->instance);
104+
elseif(\is_array($property)){
105+
$values=\array_map(function($v) use ($index){return $this->_getValue($v, $index);}, $property);
106+
$value=\implode("", $values);
107+
}elseif(\is_string($property)){
108+
$value=$property;
109+
if(isset($this->instance->{$property})){
110+
$value=$this->instance->{$property};
111+
}elseif(\method_exists($this->instance, $getter=JReflection::getterName($property))){
112+
$value=JReflection::callMethod($this->instance, $getter, []);
118113
}
119114
}
115+
return $this->_postGetValue($index, $propertyName, $value);
116+
}
117+
118+
protected function _postGetValue($index,$propertyName,$value){
119+
if(isset($this->values[$index])){
120+
$value= $this->values[$index]($value,$this->instance,$index);
121+
}else{
122+
$value=$this->_getDefaultValue($propertyName,$value, $index);
123+
}
120124
if(isset($this->afterCompile[$index])){
121125
if(\is_callable($this->afterCompile[$index])){
122126
$this->afterCompile[$index]($value,$this->instance,$index);

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class DataTable extends Widget {
3737
protected $_json;
3838
protected $_rowClass="";
3939
protected $_sortable;
40+
protected $_hiddenColumns;
4041

4142

4243
public function __construct($identifier,$model,$modelInstance=NULL) {
@@ -117,12 +118,21 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
117118

118119
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
119120
$this->_compileForm();
120-
121+
if(isset($this->_hiddenColumns))
122+
$this->_hideColumns();
121123
$this->_generated=true;
122124
}
123125
return parent::compile($js,$view);
124126
}
125127

128+
protected function _hideColumns(){
129+
$table=$this->getTable();
130+
foreach ($this->_hiddenColumns as $colIndex){
131+
$table->hideColumn($colIndex);
132+
}
133+
return $this;
134+
}
135+
126136
protected function _generateHeader(HtmlTable $table,$captions){
127137
$table->setHeaderValues($captions);
128138
if(isset($this->_sortable)){
@@ -407,7 +417,9 @@ public function setActiveRowSelector($class="active",$event="click",$multiple=fa
407417
}
408418

409419
public function hideColumn($colIndex){
410-
$this->getTable()->hideColumn($colIndex);
420+
if(!\is_array($this->_hiddenColumns))
421+
$this->_hiddenColumns=[];
422+
$this->_hiddenColumns[]=$colIndex;
411423
return $this;
412424
}
413425

Ajax/service/JReflection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ public static function jsonObject($classname){
3232
public static function callMethod($object,$callback,array $values){
3333
return \call_user_func_array([$object,$callback],$values);
3434
}
35+
36+
public static function getterName($propertyName,$prefix="get"){
37+
return $prefix.\ucfirst($propertyName);
38+
}
3539
}

0 commit comments

Comments
 (0)