From 206d224eb58be4fe14dd7c8141d4d0b36eb79789 Mon Sep 17 00:00:00 2001 From: Sebastien Routier Date: Mon, 7 May 2018 12:50:53 -0400 Subject: [PATCH] Fix for issue #431: Not all records are exported when calling DataGrid::buildCSV() --- src/DataSet.php | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/DataSet.php b/src/DataSet.php index 9b3d7eb1..55d021a2 100644 --- a/src/DataSet.php +++ b/src/DataSet.php @@ -182,10 +182,19 @@ public function build() case "PDOStatement": //orderby is handled by the code providing the PDOStatement - //calculate page variable. - $limit = $this->limit ? $this->limit : 100000; - $current_page = $this->url->value('page'.$this->cid, 0); - $offset = (max($current_page-1,0)) * $limit; + // Calculate page variable. + // $limit is set to only export a subset + if (isset($this->limit)) { + $limit = $this->limit; + $current_page = $this->url->value('page'.$this->cid, 0); + $offset = (max($current_page-1,0)) * $limit; + } + // $limit is null to export every records. + else { + $limit = null; + $current_page = 0; + $offset = 0; + } $rows = array(); $skip = $cnt = 0; @@ -195,11 +204,15 @@ public function build() $skip++; continue; } - //gather the rows to render - elseif ($cnt <= $limit ) { + //gather the rows to render. + else { $rows[$cnt] = $row; $cnt++; } + // If limit is set and we are passed it, break out of loop. + if (isset($limit) && ($cnt > $limit)) { + break; + } } $this->data = $rows; @@ -226,9 +239,19 @@ public function build() } } - $limit = $this->limit ? $this->limit : 100000; - $current_page = $this->url->value('page'.$this->cid, 0); - $offset = (max($current_page-1,0)) * $limit; + // $limit is set to only export a subset + if (isset($this->limit)) { + $limit = $this->limit; + $current_page = $this->url->value('page'.$this->cid, 0); + $offset = (max($current_page-1,0)) * $limit; + } + // $limit is null to export every records. + else { + $limit = count($this->source); + $current_page = 0; + $offset = 0; + } + $this->data = array_slice($this->source, $offset, $limit); $this->total_rows = count($this->source); $this->paginator = new LengthAwarePaginator($this->data, $this->total_rows, $limit, $current_page,