Skip to content

Commit f99fed4

Browse files
committed
DataTable autoPagination
1 parent c6edaba commit f99fed4

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class DataTable extends Widget {
3030
protected $_editBehavior;
3131
protected $_visibleHover=false;
3232
protected $_targetSelector;
33+
protected $_refreshSelector;
3334

3435

3536
public function __construct($identifier,$model,$modelInstance=NULL) {
@@ -145,7 +146,7 @@ private function _generatePagination($table){
145146
$menu->setActiveItem($this->_pagination->getPage()-1);
146147
$footer->setValues($menu);
147148
if(isset($this->_urls["refresh"]))
148-
$menu->postOnClick($this->_urls["refresh"],"{'p':$(this).attr('data-page')}","#".$this->identifier." tbody",["preventDefault"=>false,"jqueryDone"=>"replaceWith"]);
149+
$menu->postOnClick($this->_urls["refresh"],"{'p':$(this).attr('data-page')}",$this->getRefreshSelector(),["preventDefault"=>false,"jqueryDone"=>"replaceWith"]);
149150
}
150151

151152
protected function _getFieldName($index){
@@ -177,7 +178,7 @@ protected function _setToolbarPosition($table,$captions=NULL){
177178
* The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position
178179
* @param int $index postion of the compiled field
179180
* @param callable $callback function called after the field compilation
180-
* @return \Ajax\semantic\widgets\datatable\DataTable
181+
* @return DataTable
181182
*/
182183
public function afterCompile($index,$callback){
183184
$this->_instanceViewer->afterCompile($index,$callback);
@@ -206,7 +207,7 @@ public function getUrls() {
206207
/**
207208
* Sets the associative array of urls for refreshing, updating or deleting
208209
* @param string|array $urls associative array with keys refresh: for refreshing with search field or pagination, edit : for updating a row, delete: for deleting a row
209-
* @return \Ajax\semantic\widgets\datatable\DataTable
210+
* @return DataTable
210211
*/
211212
public function setUrls($urls) {
212213
if(\is_array($urls)){
@@ -222,16 +223,28 @@ public function setUrls($urls) {
222223
/**
223224
* Paginates the DataTable element with a Semantic HtmlPaginationMenu component
224225
* @param number $page the active page number
225-
* @param number $items_per_page
226-
* @param number $pages_visibles
227-
* @param number $total_rowcount
226+
* @param number $total_rowcount the total number of items
227+
* @param number $items_per_page The number of items per page
228+
* @param number $pages_visibles The number of visible pages in the Pagination component
228229
* @return DataTable
229230
*/
230-
public function paginate($page=1,$items_per_page=10,$pages_visibles=4,$total_rowcount=null){
231+
public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=4){
231232
$this->_pagination=new Pagination($items_per_page,$pages_visibles,$page,$total_rowcount);
232233
return $this;
233234
}
234235

236+
/**
237+
* Auto Paginates the DataTable element with a Semantic HtmlPaginationMenu component
238+
* @param number $page the active page number
239+
* @param number $items_per_page The number of items per page
240+
* @param number $pages_visibles The number of visible pages in the Pagination component
241+
* @return DataTable
242+
*/
243+
public function autoPaginate($page=1,$items_per_page=10,$pages_visibles=4){
244+
$this->_pagination=new Pagination($items_per_page,$pages_visibles,$page);
245+
return $this;
246+
}
247+
235248

236249

237250
/**
@@ -289,4 +302,16 @@ public function setTargetSelector($_targetSelector) {
289302
$this->_targetSelector=$_targetSelector;
290303
return $this;
291304
}
305+
306+
public function getRefreshSelector() {
307+
if(isset($this->_refreshSelector))
308+
return $this->_refreshSelector;
309+
return "#".$this->identifier." tbody";
310+
}
311+
312+
public function setRefreshSelector($_refreshSelector) {
313+
$this->_refreshSelector=$_refreshSelector;
314+
return $this;
315+
}
316+
292317
}

Ajax/semantic/widgets/datatable/Pagination.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ class Pagination {
1111

1212
public function __construct($items_per_page=10,$pages_visibles=4,$page=1,$row_count=null){
1313
$this->items_per_page=$items_per_page;
14+
$this->row_count=$row_count;
1415
$this->page=$page;
1516
$this->pages_visibles=$pages_visibles;
1617
$this->visible=true;
17-
$this->row_count=$row_count;
1818
}
1919

2020
public function getObjects($objects){
2121
$auto=(!isset($this->row_count));
22-
$offset = ($this->page - 1) * $this->items_per_page;
2322
$os=$objects;
2423
if(!\is_array($os)){
2524
$os=[];
@@ -38,8 +37,10 @@ public function getObjects($objects){
3837
$this->page = 1;
3938
}
4039
}
41-
if($auto)
40+
if($auto){
41+
$offset = ($this->page - 1) * $this->items_per_page;
4242
return array_slice($os, $offset,$this->items_per_page);
43+
}
4344
return $os;
4445
}
4546

0 commit comments

Comments
 (0)