Skip to content

Commit

Permalink
Merge pull request #2778 from yajra/p1
Browse files Browse the repository at this point in the history
[10.x] Fix set total & filtered records count
  • Loading branch information
yajra authored May 8, 2022
2 parents d13de45 + a81f9f5 commit 410ca34
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 161 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
command: COMPOSER_ROOT_VERSION=dev-master composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit --verbose
30 changes: 18 additions & 12 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,33 @@ on:

jobs:
static-analysis-phpstan:

name: "Static Analysis with PHPStan"
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php-version:
- "8.1"
php: [8.1]
stability: [prefer-stable]

steps:
- name: "Checkout code"
uses: "actions/checkout@v2"
- name: Checkout code
uses: actions/checkout@v2

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
tools: "cs2pr"
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v1"
- name: Install dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: COMPOSER_ROOT_VERSION=dev-master composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

- name: "Run a static analysis with phpstan/phpstan"
run: "vendor/bin/phpstan --error-format=checkstyle | cs2pr"
run: "vendor/bin/phpstan --error-format=checkstyle"
16 changes: 1 addition & 15 deletions src/CollectionDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static function create($source)
*/
public function count(): int
{
return ($this->collection->count() > $this->totalRecords) ? $this->totalRecords : $this->collection->count();
return $this->collection->count();
}

/**
Expand All @@ -112,8 +112,6 @@ public function columnSearch(): void
continue;
}

$this->isFilterApplied = true;

$regex = $this->request->isRegex($i);
$keyword = $this->request->columnKeyword($i);

Expand Down Expand Up @@ -187,16 +185,6 @@ public function make($mDataSupport = true): JsonResponse
}
}

/**
* Count total items.
*
* @return int
*/
public function totalCount(): int
{
return $this->totalRecords ?: $this->collection->count();
}

/**
* Get results.
*
Expand Down Expand Up @@ -254,8 +242,6 @@ protected function globalSearch(string $keyword): void
$keyword = $this->config->isCaseInsensitive() ? Str::lower($keyword) : $keyword;

$this->collection = $this->collection->filter(function ($row) use ($keyword) {
$this->isFilterApplied = true;

$data = $this->serialize($row);
foreach ($this->request->searchableColumnIndex() as $index) {
$column = $this->getColumnName($index);
Expand Down
62 changes: 40 additions & 22 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ abstract class DataTableAbstract implements DataTable
/**
* Total records.
*
* @var int
* @var int|null
*/
protected int $totalRecords = 0;
protected ?int $totalRecords = null;

/**
* Total filtered records.
*
* @var int
* @var int|null
*/
protected int $filteredRecords = 0;
protected ?int $filteredRecords = null;

/**
* Auto-filter flag.
Expand Down Expand Up @@ -109,17 +109,10 @@ abstract class DataTableAbstract implements DataTable
'DT_RowAttr' => [],
];

/**
* [internal] Track if any filter was applied for at least one column.
*
* @var bool
*/
protected bool $isFilterApplied = false;

/**
* Custom ordering callback.
*
* @var ?callable
* @var callable|null
*/
protected $orderCallback = null;

Expand Down Expand Up @@ -453,7 +446,7 @@ public function with($key, $value = ''): static
* @param callable $value
* @return $this
*/
public function withQuery($key, callable $value): static
public function withQuery(string $key, callable $value): static
{
$this->appends[$key] = $value;

Expand Down Expand Up @@ -492,7 +485,7 @@ public function blacklist(array $blacklist): static
* @param string|array $whitelist
* @return $this
*/
public function whitelist($whitelist = '*'): static
public function whitelist(array|string $whitelist = '*'): static
{
$this->columnDef['whitelist'] = $whitelist;

Expand All @@ -505,7 +498,7 @@ public function whitelist($whitelist = '*'): static
* @param bool $state
* @return $this
*/
public function smart($state = true): static
public function smart(bool $state = true): static
{
$this->config->set('datatables.search.smart', $state);

Expand All @@ -518,7 +511,7 @@ public function smart($state = true): static
* @param bool $state
* @return $this
*/
public function startsWithSearch($state = true): static
public function startsWithSearch(bool $state = true): static
{
$this->config->set('datatables.search.starts_with', $state);

Expand All @@ -531,7 +524,7 @@ public function startsWithSearch($state = true): static
* @param bool $multiTerm
* @return $this
*/
public function setMultiTerm($multiTerm = true): static
public function setMultiTerm(bool $multiTerm = true): static
{
$this->config->set('datatables.search.multi_term', $multiTerm);

Expand All @@ -544,20 +537,36 @@ public function setMultiTerm($multiTerm = true): static
* @param int $total
* @return $this
*/
public function setTotalRecords($total): static
public function setTotalRecords(int $total): static
{
$this->skipTotalRecords();
$this->totalRecords = $total;

return $this;
}

/**
* Skip total records and set the recordsTotal equals to recordsFiltered.
* This will improve the performance by skipping the total count query.
*
* @return $this
*
* @deprecated Just use setTotalRecords instead.
*/
public function skipTotalRecords(): static
{
$this->totalRecords = 0;

return $this;
}

/**
* Set filtered records manually.
*
* @param int $total
* @return $this
*/
public function setFilteredRecords($total): static
public function setFilteredRecords(int $total): static
{
$this->filteredRecords = $total;

Expand Down Expand Up @@ -651,7 +660,6 @@ abstract protected function defaultOrdering(): void;
public function filter(callable $callback, $globalSearch = false): static
{
$this->autoFilter = $globalSearch;
$this->isFilterApplied = true;
$this->filterCallback = $callback;

return $this;
Expand Down Expand Up @@ -721,7 +729,7 @@ protected function filterRecords(): void

$this->columnSearch();
$this->searchPanesSearch();
$this->filteredRecords = $this->isFilterApplied ? $this->filteredCount() : $this->totalRecords;
$this->filteredCount();
}

/**
Expand Down Expand Up @@ -778,14 +786,24 @@ protected function searchPanesSearch(): void
// Add support for search pane.
}

/**
* Count total items.
*
* @return int
*/
public function totalCount(): int
{
return $this->totalRecords ??= $this->count();
}

/**
* Count filtered items.
*
* @return int
*/
protected function filteredCount(): int
{
return $this->filteredRecords ?: $this->count();
return $this->filteredRecords ??= $this->count();
}

/**
Expand Down
65 changes: 5 additions & 60 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ class QueryDataTable extends DataTableAbstract
*/
protected $limitCallback = null;

/**
* Flag to skip total records count query.
*
* @var bool
*/
protected bool $skipTotalRecords = false;

/**
* Flag to keep the select bindings.
*
Expand Down Expand Up @@ -103,9 +96,7 @@ public static function canCreate($source): bool
public function make($mDataSupport = true): JsonResponse
{
try {
$this->prepareQuery();

$results = $this->results();
$results = $this->prepareQuery()->results();
$processed = $this->processResults($results, $mDataSupport);
$data = $this->transform($results, $processed);

Expand All @@ -117,8 +108,10 @@ public function make($mDataSupport = true): JsonResponse

/**
* Prepare query by executing count, filter, order and paginate.
*
* @return $this
*/
protected function prepareQuery(): void
protected function prepareQuery(): static
{
if (! $this->prepared) {
$this->totalRecords = $this->totalCount();
Expand All @@ -131,22 +124,8 @@ protected function prepareQuery(): void
}

$this->prepared = true;
}

/**
* Count total items.
*
* @return int
*/
public function totalCount(): int
{
if ($this->skipTotalRecords) {
$this->isFilterApplied = true;

return 1;
}

return $this->totalRecords ?: $this->count();
return $this;
}

/**
Expand Down Expand Up @@ -215,19 +194,6 @@ public function results(): Collection
return $this->query->get();
}

/**
* Skip total records and set the recordsTotal equals to recordsFiltered.
* This will improve the performance by skipping the total count query.
*
* @return $this
*/
public function skipTotalRecords(): static
{
$this->skipTotalRecords = true;

return $this;
}

/**
* Keep the select bindings.
*
Expand Down Expand Up @@ -268,8 +234,6 @@ public function columnSearch(): void
$keyword = $this->getColumnSearchKeyword($index);
$this->compileColumnSearch($index, $column, $keyword);
}

$this->isFilterApplied = true;
}
}

Expand Down Expand Up @@ -628,26 +592,9 @@ protected function searchPanesSearch(): void
} else {
$this->query->whereIn($column, $values);
}

$this->isFilterApplied = true;
}
}

/**
* Count filtered items.
*
* @return int
*/
protected function filteredCount(): int
{
$this->filteredRecords = $this->filteredRecords ?: $this->count();
if ($this->skipTotalRecords) {
$this->totalRecords = $this->filteredRecords;
}

return $this->filteredRecords;
}

/**
* Resolve callback parameter instance.
*
Expand Down Expand Up @@ -769,8 +716,6 @@ protected function globalSearch(string $keyword): void
} else {
$this->compileQuerySearch($query, $column, $keyword);
}

$this->isFilterApplied = true;
});
});
}
Expand Down
Loading

0 comments on commit 410ca34

Please sign in to comment.