phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
JsonDataTable.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use Ajax\JsUtils;
10 
16 class JsonDataTable extends DataTable {
17  protected $_modelClass="_jsonArrayModel";
18  protected $_rowModelCallback;
19 
20  public function __construct($identifier, $model, $modelInstance=NULL) {
21  parent::__construct($identifier, $model, $modelInstance);
22  $this->_rowClass="_json _element";
23  }
24 
25  protected function _generateContent($table){
26  $this->_addRowModel($table);
27  parent::_generateContent($table);
28  }
29 
30  protected function _addRowModel($table){
31  $fields=$this->_instanceViewer->getSimpleProperties();
32  $row=$this->_createRow($table, $this->_modelClass,$fields);
33  $row->setProperty("style","display:none;");
34  $table->getBody()->_addRow($row);
35  }
36 
37  protected function _createRow($table,$rowClass,$fields){
38  $object=JReflection::jsonObject($this->_model);
39  if(isset($this->_rowModelCallback)){
40  $callback=$this->_rowModelCallback;
41  $callback($object);
42  }
43  $row=$this->_generateRow($object, $fields,$table,"_jsonArrayChecked");
44  $row->setClass($rowClass." _element");
45  return $row;
46  }
47 
52  protected function _associatePaginationBehavior(JsUtils $js=NULL,$offset=null){
53  $callback=null;
54  $menu=$this->_pagination->getMenu();
55 
56  if(isset($js)){
58  $callback=$js->getScript($offset).$this->getHtmlComponent()->getInnerScript();
59  $callback.=$js->trigger("#".$id." [name='selection[]']","change",false)."$('#".$id." tbody .ui.checkbox').checkbox();".$js->execOn("change", "#".$id." [name='selection[]']", $this->_getCheckedChange($js));
60  $callback.=$this->_generatePaginationScript($id);
61  if(isset($this->_urls["refresh"])){
62  if(isset($menu))
63  $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]);
64  }
65  }
66 
67  }
68 
69  protected function _generatePaginationScript($id){
70  return ";var page=parseInt($(self).attr('data-page')) || 1;var pages_visibles=$('#pagination-{$id} .item').length-2;
71  var lastPage=$('#pagination-{$id} ._lastPage');
72  var middle= Math.ceil((pages_visibles-1)/ 2);
73  var first=Math.max(1,page-middle);var max=lastPage.attr('data-max');
74  var last=Math.min(max,first+pages_visibles-1);
75  if(last-pages_visibles+1>=0)
76  first=Math.min(first,last-pages_visibles+1);
77  var number=first;
78  $('#pagination-{$id} .item.pageNum').each(function(){
79  $(this).attr('data-page',number);
80  $(this).html(number);
81  number++;
82  });
83  $('#pagination-{$id} .item').removeClass('active');
84  $('#pagination-{$id} [data-page='+page+']:not(.no-active)').addClass('active');
85  $('#pagination-{$id} ._firstPage').attr('data-page',Math.max(1,page-1));
86  lastPage.attr('data-page',Math.min(lastPage.attr('data-max'),page+1));
87  $('#{$id}').trigger('pageChange');$('#{$id}').trigger('activeRowChange');$('#pagination-{$id}').show();";
88  }
89  protected function _compileSearchFieldBehavior(JsUtils $js=NULL){
90 
91  }
92  protected function _associateSearchFieldBehavior(JsUtils $js=NULL,$offset=null){
93  if(isset($this->_searchField) && isset($js) && isset($this->_urls["refresh"])){
95  $callback=$js->getScript($offset).$this->getHtmlComponent()->getInnerScript();
96  $callback.=$js->trigger("#".$id." [name='selection[]']","change",false)."$('#".$id." tbody .ui.checkbox').checkbox();".$js->execOn("change", "#".$id." [name='selection[]']", $this->_getCheckedChange($js));
97  $callback.="$('#pagination-{$id}').hide();$('#".$this->identifier."').trigger('searchTerminate',[$(self).val()]);";
98  $js->jsonArrayOn("change", "#".$this->_searchField->getDataField()->getIdentifier(),"#".$this->_identifier." tr.".$this->_modelClass, $this->_urls["refresh"],"post",["params"=>"{'s':$(self).val(),'_model':'".JString::doubleBackSlashes($this->_model)."'}","jsCallback"=>$callback]);
99  }
100  }
101 
102 
103 
112  public function jsJsonArray($url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
113  $parameters=\array_merge($parameters,["modelSelector"=>"#".$this->_identifier." tr.".$this->_modelClass,"url"=>$url,"method"=>$method,"params"=>$params,"jsCallback"=>$jsCallback]);
114  return new AjaxCall("jsonArray", $parameters);
115  }
116 
117  public function jsClear(){
118  return "$('#{$this->identifier} tbody').find('._json').remove();";
119  }
120 
121  public function clearOn(BaseHtml $element,$event, $stopPropagation=false, $preventDefault=false){
122  return $element->addEvent($event, $this->jsClear(),$stopPropagation,$preventDefault);
123  }
124 
125  public function clearOnClick(BaseHtml $element,$stopPropagation=false, $preventDefault=false){
126  return $this->clearOn($element, "click",$stopPropagation,$preventDefault);
127  }
128 
129  public function jsonArrayOn(BaseHtml $element,$event,$url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
130  return $element->_addEvent($event, $this->jsJsonArray($url,$method,$params,$jsCallback,$parameters));
131  }
132 
133  public function jsonArrayOnClick(BaseHtml $element,$url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
134  return $this->jsonArrayOn($element, "click", $url,$method,$params,$jsCallback,$parameters);
135  }
136 
145  public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=null){
146  return parent::paginate($page, $total_rowcount,$items_per_page,$pages_visibles);
147  }
148 
150  $this->_rowModelCallback=$_rowModelCallback;
151  return $this;
152  }
153 }
_generateRow($instance, $fields, &$table, $checkedClass=null, $uuids=null)
Definition: DataTable.php:288
BaseHtml for HTML components.
Definition: BaseHtml.php:17
DataTable widget for displaying list of objects.
Definition: DataTable.php:27
static jsonObject($classname)
Definition: JReflection.php:11
clearOnClick(BaseHtml $element, $stopPropagation=false, $preventDefault=false)
_associateSearchFieldBehavior(JsUtils $js=NULL, $offset=null)
clearOn(BaseHtml $element, $event, $stopPropagation=false, $preventDefault=false)
jsonArrayOnClick(BaseHtml $element, $url, $method="get", $params="{}", $jsCallback=NULL, $parameters=[])
__construct($identifier, $model, $modelInstance=NULL)
paginate($page, $total_rowcount, $items_per_page=10, $pages_visibles=null)
Paginates the DataTable element with a Semantic HtmlPaginationMenu component.
_associatePaginationBehavior(JsUtils $js=NULL, $offset=null)
JQuery PHP library.
Definition: JsUtils.php:23
jsJsonArray($url, $method="get", $params="{}", $jsCallback=NULL, $parameters=[])
Returns a new AjaxCall object, must be compiled using $jquery object.
jsonArrayOn(BaseHtml $element, $event, $url, $method="get", $params="{}", $jsCallback=NULL, $parameters=[])