Skip to content

Commit

Permalink
implement smart paging
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Jan 28, 2020
1 parent 1d5aba3 commit cd80100
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
11 changes: 8 additions & 3 deletions php/Job/Space/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function run()
}

$data = null;
$total = 0;
$total = null;
$next = false;

try {
$criteria = Criteria::index($this->index)
Expand Down Expand Up @@ -54,7 +55,11 @@ public function run()
[$total] = $this->getMapper()->getClient()
->call("box.space.$this->space.index.$indexName:count", $key);
} catch (Exception $e) {
// ignore total calculation exception
$criteria = $criteria->andLimit($this->limit+1);
$extra = $this->getMapper()->getClient()->getSpace($this->space)
->select($criteria);
// next page flag
$next = count($extra) > count($data);
}

} catch (Exception $e) {
Expand All @@ -73,6 +78,6 @@ public function run()
}
}

return compact('data', 'total');
return compact('data', 'total', 'next');
}
}
29 changes: 19 additions & 10 deletions public/admin/js/Space/toolbar/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,31 @@ Ext.define('Admin.Space.toolbar.Collection', {
var pageCount = Math.ceil(store.getTotalCount() / store.pageSize) || 1;
var currentPage = store.currentPage;

this.down('[name=row-counter]').setValue(store.getTotalCount());
if (store.proxy.lastResponse.total == null) {
this.down('[name=row-counter]').setValue('-');
this.down('[name=total-pages]').setText('-');
} else {
this.down('[name=row-counter]').setValue(store.getTotalCount());
this.down('[name=total-pages]').setText(pageCount);
}

var stats = this.down('[name=paging-stats]');
var first = this.down('[name=first-page]');
var prev = this.down('[name=previous-page]');
var current = this.down('[name=current-page]');
var delimeter = this.down('[name=current-page-delimiter]');
var total = this.down('[name=total-pages]');
var next = this.down('[name=next-page]');
var last = this.down('[name=last-page]');

first.setDisabled(currentPage == 1);
prev.setDisabled(currentPage == 1);
current.setValue(currentPage);
total.setText(pageCount);
next.setDisabled(currentPage == pageCount);
last.setDisabled(currentPage == pageCount);

// unknown row count
if (store.proxy.lastResponse.total == null) {
next.setDisabled(!store.proxy.lastResponse.next);
last.setDisabled(true);
}
},

getDefaultItems() {
Expand Down Expand Up @@ -106,9 +114,8 @@ Ext.define('Admin.Space.toolbar.Collection', {
labelWidth: 65,
name: 'row-counter',
readOnly: true,
value: 0,
width: 20,
xtype: 'numberfield',
xtype: 'textfield',
}, ' ',{
xtype: 'numberfield',
minValue: 1,
Expand All @@ -129,8 +136,8 @@ Ext.define('Admin.Space.toolbar.Collection', {
}
var store = this.up('grid').store;
if (store.pageSize != v) {
this.up('grid').store.setPageSize(v);
this.up('grid').store.loadPage(1);
store.setPageSize(v);
store.loadPage(1);
}
localStorage.setItem('admin-page-size', v);
}
Expand Down Expand Up @@ -164,7 +171,9 @@ Ext.define('Admin.Space.toolbar.Collection', {
keyup(field) {
var store = this.up('grid').store;
var pageCount = Math.ceil(store.getTotalCount() / store.pageSize);
if(field.value <= pageCount && field.value >= 0) {
if (store.proxy.lastResponse.total == null) {
this.up('grid').store.loadPage(field.value || 1);
} else if (field.value <= pageCount && field.value >= 0) {
this.up('grid').store.loadPage(field.value || 1);
} else if(field.value > pageCount) {
this.up('grid').store.loadPage(pageCount);
Expand Down
1 change: 1 addition & 0 deletions public/admin/js/data/proxy/PagingDispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

dispatch(this.job, params)
.then(response => {
this.lastResponse = response;
var resultSet = new Ext.data.ResultSet({
total: response.total,
count: (response.data || []).length,
Expand Down

0 comments on commit cd80100

Please sign in to comment.