Skip to content

Commit c16d386

Browse files
committed
new jsonDataTable
1 parent f99fed4 commit c16d386

File tree

12 files changed

+199
-42
lines changed

12 files changed

+199
-42
lines changed

Ajax/common/Widget.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ public function __construct($identifier,$model,$modelInstance=NULL) {
5858
$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
5959
$this->setModel($model);
6060
if(isset($modelInstance)){
61-
if(\is_array($modelInstance)){
62-
$modelInstance=\json_decode(\json_encode($modelInstance), FALSE);
63-
}
6461
$this->show($modelInstance);
6562
}
6663
$this->_generated=false;
@@ -96,6 +93,9 @@ protected function _getFieldCaption($index){
9693
abstract protected function _setToolbarPosition($table,$captions=NULL);
9794

9895
public function show($modelInstance){
96+
if(\is_array($modelInstance)){
97+
$modelInstance=\json_decode(\json_encode($modelInstance), FALSE);
98+
}
9999
$this->_modelInstance=$modelInstance;
100100
}
101101

Ajax/common/traits/JqueryAjaxTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,16 @@ public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $js
168168
$jsCallback=isset($jsCallback) ? $jsCallback : "";
169169
$retour=$this->_getAjaxUrl($url, $attr);
170170
if($context===null){
171-
$appendTo="\t\tnewElm.appendTo($('".$maskSelector."').parent());\n";
171+
$parent="$('".$maskSelector."').parent()";
172172
$newElm = "$('#'+newId)";
173173
}else{
174-
$appendTo="\t\tnewElm.appendTo(".$context.");\n";
174+
$parent=$context;
175175
$newElm = $context.".find('#'+newId)";
176176
}
177+
$appendTo="\t\tnewElm.appendTo(".$parent.");\n";
177178
$retour.="var self = $(this);\n$.{$method}(url,".$params.").done(function( data ) {\n";
178-
$retour.="\tdata=$.parseJSON(data);$.each(data, function(index, value) {\n"."\tvar created=false;var maskElm=$('".$maskSelector."').first();maskElm.hide();"."\tvar newId=(maskElm.attr('id') || 'mask')+'-'+index;"."\tvar newElm=".$newElm.";\n"."\tif(!newElm.length){\n"."\t\tnewElm=maskElm.clone();newElm.attr('id',newId);\n";
179+
$retour.=$parent.".find('._json').remove();";
180+
$retour.="\tdata=$.parseJSON(data);$.each(data, function(index, value) {\n"."\tvar created=false;var maskElm=$('".$maskSelector."').first();maskElm.hide();"."\tvar newId=(maskElm.attr('id') || 'mask')+'-'+index;"."\tvar newElm=".$newElm.";\n"."\tif(!newElm.length){\n"."\t\tnewElm=maskElm.clone();newElm.attr('id',newId);\n;newElm.addClass('_json');\n";
179181
$retour.= $appendTo;
180182
$retour.="\t}\n"."\tfor(var key in value){\n"."\t\t\tvar html = $('<div />').append($(newElm).clone()).html();\n"."\t\t\tif(html.indexOf('[['+key+']]')>-1){\n"."\t\t\t\tcontent=$(html.split('[['+key+']]').join(value[key]));\n"."\t\t\t\t$(newElm).replaceWith(content);newElm=content;\n"."\t\t\t}\n"."\t\tvar sel='[data-id=\"'+key+'\"]';if($(sel,newElm).length){\n"."\t\t\tvar selElm=$(sel,newElm);\n"."\t\t\t if(selElm.is('[value]')) { selElm.attr('value',value[key]);selElm.val(value[key]);} else { selElm.html(value[key]); }\n"."\t\t}\n"."}\n"."\t$(newElm).show(true);"."\n"."\t$(newElm).removeClass('hide');"."});\n";
181183
$retour.="\t$(document).trigger('jsonReady',[data]);\n";

Ajax/common/traits/JsUtilsAjaxTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function jsonDeferred($url, $method="get", $params="{}", $jsCallback=NULL
7171
* @param string $jsCallback javascript code to execute after the request
7272
*/
7373
public function jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL) {
74-
return $this->js->_jsonArray($maskSelector, $url, $method, $params, $jsCallback, NULL,true);
74+
return $this->js->_jsonArray($maskSelector, $url, $method, $params, $jsCallback,"id", NULL,true);
7575
}
7676

7777
/**
@@ -83,7 +83,7 @@ public function jsonArray($maskSelector, $url, $method="get", $params="{}", $jsC
8383
* @param string $jsCallback javascript code to execute after the request
8484
*/
8585
public function jsonArrayDeferred($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL) {
86-
return $this->js->_jsonArray($maskSelector, $url, $method, $params, $jsCallback, NULL, false);
86+
return $this->js->_jsonArray($maskSelector, $url, $method, $params, $jsCallback,"id", NULL, false);
8787
}
8888

8989
/**

Ajax/semantic/html/collections/menus/HtmlMenu.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ public function getItemHeader() {
236236
}
237237

238238
public function run(JsUtils $js){
239+
$this->onClick('if(!$(this).hasClass("dropdown")){$(this).addClass("active").siblings().removeClass("active");}');
239240
$result= parent::run($js);
240241
return $result->setItemSelector(".item");
241242
}

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Ajax\service\JArray;
1111
use Ajax\semantic\html\content\table\HtmlTR;
1212
use Ajax\semantic\html\collections\table\traits\TableTrait;
13+
use Ajax\semantic\html\content\table\HtmlTD;
1314

1415
/**
1516
* Semantic HTML Table component
@@ -57,15 +58,23 @@ public function getPart($key) {
5758

5859
/**
5960
* Returns/create eventually the body of the table
60-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
61+
* @return HtmlTableContent
6162
*/
6263
public function getBody() {
6364
return $this->getPart("tbody");
6465
}
6566

67+
/**
68+
* Returns the number of rows (TR)
69+
* @return int
70+
*/
71+
public function getRowCount() {
72+
return $this->getPart("tbody")->count();
73+
}
74+
6675
/**
6776
* Returns/create eventually the header of the table
68-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
77+
* @return HtmlTableContent
6978
*/
7079
public function getHeader() {
7180
return $this->getPart("thead");
@@ -92,7 +101,7 @@ public function hasPart($key) {
92101
*
93102
* @param int $rowCount
94103
* @param int $colCount
95-
* @return \Ajax\semantic\html\content\table\HtmlTableContent
104+
* @return HtmlTableContent
96105
*/
97106
public function setRowCount($rowCount, $colCount) {
98107
$this->_colCount=$colCount;
@@ -103,7 +112,7 @@ public function setRowCount($rowCount, $colCount) {
103112
* Returns the cell (HtmlTD) at position $row,$col
104113
* @param int $row
105114
* @param int $col
106-
* @return \Ajax\semantic\html\content\HtmlTD
115+
* @return HtmlTD
107116
*/
108117
public function getCell($row, $col) {
109118
return $this->getBody()->getCell($row, $col);
@@ -112,7 +121,7 @@ public function getCell($row, $col) {
112121
/**
113122
* Retuns the row at $rowIndex
114123
* @param int $rowIndex
115-
* @return \Ajax\semantic\html\content\HtmlTR
124+
* @return HtmlTR
116125
*/
117126
public function getRow($rowIndex) {
118127
return $this->getBody()->getRow($rowIndex);
@@ -131,7 +140,7 @@ public function addRow($values=array()) {
131140

132141
/**
133142
* adds and returns a new row
134-
* @return \Ajax\semantic\html\content\table\HtmlTR
143+
* @return HtmlTR
135144
*/
136145
public function newRow() {
137146
return $this->getBody()->newRow($this->_colCount);
@@ -154,7 +163,7 @@ public function setFooterValues($values=array()) {
154163
* Sets values to the col at index $colIndex
155164
* @param int $colIndex
156165
* @param array $values
157-
* @return \Ajax\semantic\html\collections\HtmlTable
166+
* @return HtmlTable
158167
*/
159168
public function setColValues($colIndex, $values=array()) {
160169
$this->getBody()->setColValues($colIndex, $values);
@@ -165,7 +174,7 @@ public function setColValues($colIndex, $values=array()) {
165174
* Sets values to the row at index $rowIndex
166175
* @param int $rowIndex
167176
* @param array $values
168-
* @return \Ajax\semantic\html\collections\HtmlTable
177+
* @return HtmlTable
169178
*/
170179
public function setRowValues($rowIndex, $values=array()) {
171180
$this->getBody()->setRowValues($rowIndex, $values);
@@ -263,7 +272,7 @@ public function fromDatabaseObject($object, $function) {
263272

264273
/**
265274
* @param array $parts
266-
* @return \Ajax\semantic\html\collections\HtmlTable
275+
* @return HtmlTable
267276
*/
268277
public function setCompileParts($parts=["tbody"]) {
269278
$this->_compileParts=$parts;
@@ -286,7 +295,7 @@ public function run(JsUtils $js){
286295
* The callback function called after the insertion of each row when fromDatabaseObjects is called
287296
* callback function takes the parameters $row : the row inserted and $object: the instance of model used
288297
* @param callable $callback
289-
* @return \Ajax\semantic\html\collections\HtmlTable
298+
* @return HtmlTable
290299
*/
291300
public function onNewRow($callback) {
292301
$this->_afterCompileEvents["onNewRow"]=$callback;

Ajax/semantic/html/modules/HtmlRating.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function setMax($max){
3333

3434
/**
3535
* {@inheritDoc}
36-
* @see \Ajax\semantic\html\base\HtmlSemDoubleElement::run()
36+
* @see HtmlSemDoubleElement::run()
3737
*/
3838
public function run(JsUtils $js){
3939
parent::run($js);

Ajax/semantic/traits/SemanticWidgetsTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Ajax\semantic\widgets\dataelement\DataElement;
66
use Ajax\semantic\widgets\dataform\DataForm;
77
use Ajax\semantic\widgets\business\user\FormLogin;
8+
use Ajax\semantic\widgets\datatable\JsonDataTable;
89

910
trait SemanticWidgetsTrait {
1011

@@ -20,6 +21,16 @@ public function dataTable($identifier,$model, $instances){
2021
return $this->addHtmlComponent(new DataTable($identifier,$model,$instances));
2122
}
2223

24+
/**
25+
* @param string $identifier
26+
* @param string $model
27+
* @param array $instances
28+
* @return JsonDataTable
29+
*/
30+
public function jsonDataTable($identifier,$model, $instances){
31+
return $this->addHtmlComponent(new JsonDataTable($identifier,$model,$instances));
32+
}
33+
2334
/**
2435
* @param string $identifier
2536
* @param object $instance

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Ajax\service\JArray;
1313
use Ajax\semantic\widgets\base\InstanceViewer;
1414
use Ajax\semantic\html\collections\table\traits\TableTrait;
15+
use Ajax\semantic\html\collections\HtmlMessage;
1516

1617
/**
1718
* DataTable widget for displaying list of objects
@@ -31,12 +32,17 @@ class DataTable extends Widget {
3132
protected $_visibleHover=false;
3233
protected $_targetSelector;
3334
protected $_refreshSelector;
35+
protected $_emptyMessage;
36+
protected $_json;
37+
protected $_rowClass="";
3438

3539

3640
public function __construct($identifier,$model,$modelInstance=NULL) {
3741
parent::__construct($identifier, $model,$modelInstance);
3842
$this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0,0), false);
3943
$this->_urls=[];
44+
$this->_emptyMessage=new HtmlMessage("","nothing to display");
45+
$this->_emptyMessage->setIcon("info circle");
4046
}
4147

4248
public function run(JsUtils $js){
@@ -100,7 +106,7 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
100106
}
101107

102108
if(isset($this->_pagination) && $this->_pagination->getVisible()){
103-
$this->_generatePagination($table);
109+
$this->_generatePagination($table,$js);
104110
}
105111
if(isset($this->_toolbar)){
106112
$this->_setToolbarPosition($table, $captions);
@@ -119,32 +125,46 @@ protected function _generateContent($table){
119125
if(isset($this->_pagination)){
120126
$objects=$this->_pagination->getObjects($this->_modelInstance);
121127
}
122-
InstanceViewer::setIndex(0);
123-
$table->fromDatabaseObjects($objects, function($instance) use($table){
124-
$this->_instanceViewer->setInstance($instance);
125-
InstanceViewer::$index++;
126-
$values= $this->_instanceViewer->getValues();
127-
if($this->_hasCheckboxes){
128-
$ck=new HtmlCheckbox("ck-".$this->identifier,"");
129-
$field=$ck->getField();
130-
$field->setProperty("value",$this->_instanceViewer->getIdentifier());
131-
$field->setProperty("name", "selection[]");
132-
\array_unshift($values, $ck);
133-
}
134-
$result=$table->newRow();
135-
$result->setIdentifier($this->identifier."-tr-".$this->_instanceViewer->getIdentifier());
136-
$result->setValues($values);
137-
return $result;
138-
});
128+
InstanceViewer::setIndex(0);
129+
$table->fromDatabaseObjects($objects, function($instance) use($table){
130+
return $this->_generateRow($instance, $table);
131+
});
132+
if($table->getRowCount()==0){
133+
$result=$table->addRow();
134+
$result->mergeRow();
135+
$result->setValues([$this->_emptyMessage]);
136+
}
139137
}
140138

141-
private function _generatePagination($table){
139+
protected function _generateRow($instance,&$table){
140+
$this->_instanceViewer->setInstance($instance);
141+
InstanceViewer::$index++;
142+
$values= $this->_instanceViewer->getValues();
143+
if($this->_hasCheckboxes){
144+
$ck=new HtmlCheckbox("ck-".$this->identifier,"");
145+
$field=$ck->getField();
146+
$field->setProperty("value",$this->_instanceViewer->getIdentifier());
147+
$field->setProperty("name", "selection[]");
148+
\array_unshift($values, $ck);
149+
}
150+
$result=$table->newRow();
151+
$result->setIdentifier($this->identifier."-tr-".$this->_instanceViewer->getIdentifier());
152+
$result->setValues($values);
153+
$result->addToProperty("class",$this->_rowClass);
154+
return $result;
155+
}
156+
157+
protected function _generatePagination($table,$js=NULL){
142158
$footer=$table->getFooter();
143159
$footer->mergeCol();
144160
$menu=new HtmlPaginationMenu("pagination-".$this->identifier,$this->_pagination->getPagesNumbers());
145161
$menu->floatRight();
146162
$menu->setActiveItem($this->_pagination->getPage()-1);
147163
$footer->setValues($menu);
164+
$this->_associatePaginationBehavior($menu,$js);
165+
}
166+
167+
protected function _associatePaginationBehavior($menu,$js=NULL){
148168
if(isset($this->_urls["refresh"]))
149169
$menu->postOnClick($this->_urls["refresh"],"{'p':$(this).attr('data-page')}",$this->getRefreshSelector(),["preventDefault"=>false,"jqueryDone"=>"replaceWith"]);
150170
}
@@ -314,4 +334,22 @@ public function setRefreshSelector($_refreshSelector) {
314334
return $this;
315335
}
316336

337+
public function show($modelInstance){
338+
if(\is_array($modelInstance)){
339+
if(\is_array(array_values($modelInstance)[0]))
340+
$modelInstance=\json_decode(\json_encode($modelInstance), FALSE);
341+
}
342+
$this->_modelInstance=$modelInstance;
343+
}
344+
345+
public function getRowClass() {
346+
return $this->_rowClass;
347+
}
348+
349+
public function setRowClass($_rowClass) {
350+
$this->_rowClass=$_rowClass;
351+
return $this;
352+
}
353+
354+
317355
}

Ajax/semantic/widgets/datatable/HasCheckboxesTrait.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* used in DataTable
1010
* @author jc
1111
* @property string identifier
12+
* @property $_compileParts
1213
*/
1314
trait HasCheckboxesTrait{
1415
protected $_hasCheckboxes;
@@ -22,12 +23,12 @@ protected function _runCheckboxes(JsUtils $js){
2223
$checkedMessageCall="";
2324
if($this->_hasCheckedMessage){
2425
$msg=$this->getCheckedMessage();
25-
$checkedMessageFunction="function updateChecked(){var msg='".$msg[0]."',count=\$('#{$this->identifier} [name=\"selection[]\"]:checked').length,all=\$('#{$this->identifier} [name=\"selection[]\"]').length;
26+
$checkedMessageFunction="$('#{$this->identifier}').bind('updateChecked',function() {var msg='".$msg[0]."',count=\$('#{$this->identifier} [name=\"selection[]\"]:checked').length,all=\$('#{$this->identifier} [name=\"selection[]\"]').length;
2627
if(count==1) msg='".$msg[1]."';
2728
else if(count>1) msg='".$msg["other"]."';
2829
\$('#checked-count-".$this->identifier."').contents().filter(function() {return this.nodeType == 3;}).each(function(){this.textContent = msg.replace('{count}',count);});
29-
\$('#toolbar-{$this->identifier} .visibleOnChecked').toggle(count>0);}\$('#toolbar-".$this->identifier." .visibleOnChecked').hide();";
30-
$checkedMessageCall="updateChecked();";
30+
\$('#toolbar-{$this->identifier} .visibleOnChecked').toggle(count>0);});\$('#toolbar-".$this->identifier." .visibleOnChecked').hide();";
31+
$checkedMessageCall="$('#{$this->identifier}').trigger('updateChecked');";
3132
if(isset($this->_checkedClass)){
3233
$checkedMessageCall.="$(this).closest('tr').toggleClass('".$this->_checkedClass."',$(this).prop('checked'));";
3334
}
@@ -37,13 +38,16 @@ protected function _runCheckboxes(JsUtils $js){
3738
var \$parentCheckbox=\$('#ck-main-ck-{$this->identifier}'),\$checkbox=\$('#{$this->identifier} [name=\"selection[]\"]'),allChecked=true,allUnchecked=true;
3839
\$checkbox.each(function() {if($(this).prop('checked')){allUnchecked = false;}else{allChecked = false;}});
3940
if(allChecked) {\$parentCheckbox.checkbox('set checked');}else if(allUnchecked){\$parentCheckbox.checkbox('set unchecked');}else{\$parentCheckbox.checkbox('set indeterminate');};".$checkedMessageCall);
41+
if(\sizeof($this->_compileParts)<3){
42+
$js->trigger("#".$this->identifier." [name='selection[]']","change",true);
43+
}
4044
}
4145

4246
protected function _generateMainCheckbox(&$captions){
4347
$ck=new HtmlCheckbox("main-ck-".$this->identifier,"");
4448
$checkedMessageCall="";
4549
if($this->_hasCheckedMessage)
46-
$checkedMessageCall="updateChecked();";
50+
$checkedMessageCall="$('#{$this->identifier}').trigger('updateChecked');";
4751

4852
$ck->setOnChecked($this->_setAllChecked("true").$checkedMessageCall);
4953
$ck->setOnUnchecked($this->_setAllChecked("false").$checkedMessageCall);

0 commit comments

Comments
 (0)