Skip to content

Commit 0bebef6

Browse files
committed
Add DataTable partial refresh
1 parent 468e47b commit 0bebef6

File tree

17 files changed

+116
-62
lines changed

17 files changed

+116
-62
lines changed

Ajax/common/html/BaseHtml.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ private function _callSetter($setter,$key,$value,&$array){
4747
return $result;
4848
}
4949

50-
protected function getTemplate(JsUtils $js=NULL) {
51-
return PropertyWrapper::wrap($this->_wrapBefore, $js) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js);
50+
protected function getTemplate(JsUtils $js=NULL,$view=null) {
51+
return PropertyWrapper::wrap($this->_wrapBefore, $js,$view) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js,$view);
5252
}
5353

5454
protected function ctrl($name, $value, $typeCtrl) {
@@ -208,13 +208,15 @@ protected function compile_once(JsUtils $js=NULL, &$view=NULL) {
208208

209209
public function compile(JsUtils $js=NULL, &$view=NULL) {
210210
$this->compile_once($js,$view);
211-
$result=$this->getTemplate($js);
211+
$result=$this->getTemplate($js,$view);
212212
foreach ( $this as $key => $value ) {
213213
if(\strstr($result, "%{$key}%")!==false){
214214
if (\is_array($value)) {
215-
$v=PropertyWrapper::wrap($value, $js);
215+
$v=PropertyWrapper::wrap($value, $js,$view);
216216
}elseif($value instanceof \stdClass){
217217
$v=\print_r($value,true);
218+
}elseif ($value instanceof BaseHtml){
219+
$v=$value->compile($js,$view);
218220
}else{
219221
$v=$value;
220222
}

Ajax/common/html/HtmlCollection.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,12 @@ public function setProperties($properties){
159159
* @return $this
160160
*/
161161
public function setPropertyValues($property,$values){
162-
$i=0;
163162
if(\is_array($values)===false){
164163
$values=\array_fill(0, $this->count(),$values);
165164
}
166-
foreach ($values as $value){
167-
$c=$this->content[$i++];
168-
if(isset($c)){
169-
$c->setProperty($property,$value);
165+
foreach ($values as $i=>$value){
166+
if(isset($this->content[$i])){
167+
$this->content[$i]->setProperty($property,$value);
170168
}
171169
else{
172170
return $this;
@@ -182,14 +180,12 @@ public function setPropertyValues($property,$values){
182180
* @return $this
183181
*/
184182
public function addPropertyValues($property,$values){
185-
$i=0;
186183
if(\is_array($values)===false){
187184
$values=\array_fill(0, $this->count(),$values);
188185
}
189-
foreach ($values as $value){
190-
$c=$this->content[$i++];
191-
if(isset($c)){
192-
$c->addToProperty($property,$value);
186+
foreach ($values as $i=>$value){
187+
if(isset($this->content[$i])){
188+
$this->content[$i++]->addToProperty($property,$value);
193189
}
194190
else{
195191
return $this;

Ajax/common/html/PropertyWrapper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class PropertyWrapper {
88

9-
public static function wrap($input, $js=NULL, $separator=' ', $valueQuote='"') {
9+
public static function wrap($input, $js=NULL, $view=null, $separator=' ', $valueQuote='"') {
1010
if (is_string($input)) {
1111
return $input;
1212
}
@@ -16,7 +16,7 @@ public static function wrap($input, $js=NULL, $separator=' ', $valueQuote='"') {
1616
if (self::containsElement($input) === false) {
1717
$output=self::wrapStrings($input, $separator=' ', $valueQuote='"');
1818
} else {
19-
$output=self::wrapObjects($input, $js, $separator, $valueQuote);
19+
$output=self::wrapObjects($input, $js, $view, $separator, $valueQuote);
2020
}
2121
}
2222
}
@@ -42,16 +42,16 @@ public static function wrapStrings($input, $separator=' ', $valueQuote='"') {
4242
return $result;
4343
}
4444

45-
public static function wrapObjects($input, $js=NULL, $separator=' ', $valueQuote='"') {
46-
return implode($separator, array_map(function ($v) use($js, $separator, $valueQuote) {
45+
public static function wrapObjects($input, $js=NULL, $view=null, $separator=' ', $valueQuote='"') {
46+
return implode($separator, array_map(function ($v) use($js, $view,$separator, $valueQuote) {
4747
if(\is_string($v)){
4848
return $v;
4949
}
5050
if ($v instanceof BaseHtml){
51-
return $v->compile($js);
51+
return $v->compile($js,$view);
5252
}
5353
if (\is_array($v)) {
54-
return self::wrap($v, $js, $separator, $valueQuote);
54+
return self::wrap($v, $js, $view,$separator, $valueQuote);
5555
}
5656
if(!\is_callable($v)){
5757
return $v;

Ajax/common/html/traits/BaseHtmlEventsTrait.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefaul
3131
}
3232
return $this->_addEvent($event, $jsCode);
3333
}
34+
35+
public function trigger($event,$params="[]"){
36+
$this->executeOnRun('$("#'.$this->identifier.'").trigger("'.$event.'",'.$params.');');
37+
}
38+
39+
public function jsTrigger($event,$params="[this]"){
40+
return $this->jsDoJquery("trigger",["'".$event."'",$params]);
41+
}
3442

3543
/**
3644
* @param string $event
@@ -242,7 +250,7 @@ public function jsToggle($value) {
242250
return $this->jsDoJquery("toggle",$value);
243251
}
244252
/**
245-
* @return multitype:
253+
* @return array
246254
*/
247255
public function getEvents() {
248256
return $this->_events;

Ajax/common/traits/JsUtilsActionsTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,20 @@ public function execOn($event, $element, $js, $parameters=array()) {
531531
return $script;
532532
}
533533

534+
public function setJsonToElement($json,$elementClass="_element",$immediatly=true){
535+
$retour="var data={$json};"
536+
."\n\tdata=($.isPlainObject(data))?data:JSON.parse(data);"
537+
."\n\tvar pk=data['pk'];var object=data['object'];"
538+
."\n\tfor(var field in object){"
539+
."\n\tif($('[data-field='+field+']',$('._element[data-ajax='+pk+']')).length){"
540+
."\n\t\t$('[data-field='+field+']',$('._element[data-ajax='+pk+']')).each(function(){"
541+
."\n\t\t\tif($(this).is('[value]')) { $(this).val(object[field]);} else { $(this).html(object[field]); }"
542+
."\n\t});"
543+
."\n}};\n";
544+
$retour.="\t$(document).trigger('jsonReady',[data]);\n";
545+
return $this->exec($retour,$immediatly);
546+
}
547+
534548
/**
535549
* Sets an element draggable (HTML5 drag and drop)
536550
* @param string $element The element selector

Ajax/common/traits/JsUtilsAjaxTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,14 @@ private function _json($url, $method="get",$parameters=[]) {
251251
$parameters=\array_merge($parameters,["hasLoader"=>false]);
252252
$jsCallback=isset($parameters['jsCallback']) ? $parameters['jsCallback'] : "";
253253
$context=isset($parameters['context']) ? $parameters['context'] : "document";
254-
$retour="\tdata=($.isPlainObject(data))?data:JSON.parse(data);\t".$jsCallback.";\n\tfor(var key in data){"
254+
$retour="\tdata=($.isPlainObject(data))?data:JSON.parse(data);\t".$jsCallback.";"
255+
."\n\tfor(var key in data){"
255256
."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n";
256257
$retour.="\t$(document).trigger('jsonReady',[data]);\n";
257258
$parameters["jsCallback"]=$retour;
258259
return $this->_ajax($method, $url,null,$parameters);
259260
}
260-
261+
261262
/**
262263
* Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name
263264
* @param string $url the request url

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ public function __construct( $identifier, $items=array() ){
1919
* @see \Ajax\common\html\BaseHtml::compile()
2020
*/
2121
public function compile(JsUtils $js=NULL,&$view=NULL){
22-
$max=$this->_max;//\sizeof($this->content);
23-
foreach ($this->content as $item){
24-
$item->addClass("pageNum");
22+
$max=$this->_max;
23+
if(!$this->_compiled){
24+
foreach ($this->content as $item){
25+
$item->addClass("pageNum");
26+
}
27+
$this->insertItem(new HtmlIcon("", "left chevron"))->setProperty("data-page", \max([1,$this->_page-1]))->addToProperty("class","_firstPage no-active");
28+
$this->addItem(new HtmlIcon("", "right chevron"))->setProperty("data-page", \min([$max,$this->_page+1]))->setProperty("data-max", $max)->addToProperty("class","_lastPage no-active");
29+
$this->asPagination();
2530
}
26-
$this->insertItem(new HtmlIcon("", "left chevron"))->setProperty("data-page", \max([1,$this->_page-1]))->addToProperty("class","_firstPage no-active");
27-
$this->addItem(new HtmlIcon("", "right chevron"))->setProperty("data-page", \min([$max,$this->_page+1]))->setProperty("data-max", $max)->addToProperty("class","_lastPage no-active");
28-
$this->asPagination();
2931
return parent::compile($js,$view);
3032
}
3133

3234
public function setActivePage($page){
3335
$index=$page-$this->_pages[0];
34-
$item=$this->setActiveItem($index);
36+
$this->setActiveItem($index);
3537
$this->_page=$page;
3638
return $this;
3739
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function run(){
6060
if(!$this->multiple){
6161
$multiple="$(this).closest('tbody').children('tr').removeClass('".$this->class."');";
6262
}
63-
$this->table->onRow($this->event, $multiple."$(this).toggleClass('".$this->class."');",false,false);
63+
$this->table->onRow($this->event, $multiple."$(this).toggleClass('".$this->class."');".$this->table->jsTrigger("activeRowChange","[this]"),false,false);
6464
}
6565

6666
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,18 @@ public function refresh($js){
366366
if(isset($js)){
367367
$js->exec('$("#'.$this->identifier.' tfoot").replaceWith("'.\addslashes($this->_footer).'");',true);
368368
}
369-
//$this->addEvent("execute", '$("#'.$this->identifier.' tfoot").replaceWith("'.\addslashes($this->_footer).'");');
370369
}
371370

372371
public function run(JsUtils $js){
373-
if(isset($this->_activeRowSelector)){
374-
$this->_activeRowSelector->run();
372+
if(!$this->_runned){
373+
if(isset($this->_activeRowSelector)){
374+
$this->_activeRowSelector->run();
375+
}
375376
}
376377
$result= parent::run($js);
377378
if(isset($this->_footer))
378379
$this->_footer->run($js);
380+
$this->_runned=true;
379381
return $result;
380382
}
381383

@@ -453,5 +455,10 @@ public function getInnerScript() {
453455
public function setInnerScript($_innerScript) {
454456
$this->_innerScript = $_innerScript;
455457
}
458+
459+
public function onActiveRowChange($jsCode){
460+
$this->on("activeRowChange",$jsCode);
461+
return $this;
462+
}
456463

457464
}

Ajax/semantic/html/collections/table/traits/TableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function onRowClick($jsCode, $stopPropagation=false, $preventDefault=fals
7676
}
7777

7878
public function onRow($event,$jsCode, $stopPropagation=false, $preventDefault=false){
79-
return $this->_self->addEvent($event."{{tr}}",$jsCode,$stopPropagation,$preventDefault);
79+
return $this->_self->addEvent($event."{{tbody tr}}",$jsCode,$stopPropagation,$preventDefault);
8080
}
8181

8282
public function getOnRow($event, $url, $responseElement="", $parameters=array()){

0 commit comments

Comments
 (0)