Skip to content

Commit 4dff6d9

Browse files
LimitCallback support
1 parent fa10281 commit 4dff6d9

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ $dataSource = $this->queryObjectDataSourceFactory->create($qo, "id")
5050
$queryObject->{'by' . $field}($value);
5151
}
5252
}
53+
})
54+
->setLimitCallback(function ($offset, $limit, $defaultCallback) use ($query, $itemRepository) {
55+
// This callback is not necessary, but you can do your stuff with $offset and $limit here.
56+
57+
$defaultCallback(); // Run the default action
5358
});
5459

5560
$grid->setDataSource($queryObjectDataSource);

src/QueryObjectDataSource.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class QueryObjectDataSource implements \Ublaboo\DataGrid\DataSource\IDataSource
2929
/** @var callable */
3030
public $sortCallback;
3131

32+
/** @var callable */
33+
public $limitCallback;
34+
3235
/**
3336
* @param \Kdyby\Doctrine\QueryObject $queryObject
3437
* @param \Kdyby\Doctrine\EntityRepository $repo
@@ -65,6 +68,18 @@ public function setSortCallback($callback) {
6568
return $this;
6669
}
6770

71+
/**
72+
* Pro nastavení limitu lze specifikovat callback $callback. Pokud není zadán, je použit defaultní callback $defaultCallback.
73+
* Callback $callback dostane mezi parametry i $defaultCallback, aby ho mohl případně zavolat.
74+
*
75+
* @param callable $callback Dostane parametry $offset, $limit, $defaultCallback
76+
* @return self
77+
*/
78+
public function setLimitCallback($callback) {
79+
$this->limitCallback = $callback;
80+
return $this;
81+
}
82+
6883
protected function getResultSet() {
6984
if (!$this->resultSet) {
7085
$this->resultSet = $this->repo
@@ -144,7 +159,16 @@ public function filterOne(array $filter) {
144159
* @return static
145160
*/
146161
public function limit($offset, $limit) {
147-
$this->getResultSet()->applyPaging($offset, $limit);
162+
$defaultCallback = function () use ($offset, $limit) {
163+
$this->getResultSet()->applyPaging($offset, $limit);
164+
};
165+
166+
if (is_callable($this->limitCallback)) {
167+
$this->limitCallback($offset, $limit, $defaultCallback);
168+
169+
} else {
170+
$defaultCallback();
171+
}
148172

149173
return $this;
150174
}

0 commit comments

Comments
 (0)