Skip to content

Commit

Permalink
decouple DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Apr 16, 2019
1 parent 7eecd4c commit 6bb5e5c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,20 @@ protected function grid()
// set write type, default xlsx
$exporter->setWriteType(\Maatwebsite\Excel\Excel::CSV);

// set the file name
$exporter->setFileName('test-export');

$grid->exporter($exporter);

return $grid;
}

...
```
if you want more control over the output file, you can create a class that extents from `Chenyulingxi\LaravelAdmin\GridExporter`, then inject it's instance to the exporter like this:
```php
$exporter->setDataSource(new TestDataSource());
```
more information reference to [Laravel Excel](https://docs.laravel-excel.com/3.1/exports/extending.html) and [PhpSpreadsheet](https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#styles)

License
Expand Down
2 changes: 1 addition & 1 deletion src/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function headings(): array
return $this->headings;
}

private function getDefaultEvents(): array
protected function getDefaultEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
Expand Down
13 changes: 12 additions & 1 deletion src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Exporter extends AbstractExporter

protected $events = [];

protected $dataSource;

public function format($name, $handler)
{
$this->columnFormatters[$name] = $handler;
Expand All @@ -41,6 +43,11 @@ public function setEvents(array $events)
$this->events = $events;
}

public function setDataSource(DataSource $dataSource)
{
$this->dataSource = $dataSource;
}

public function withHeadings($headings)
{
if (is_bool($headings)) {
Expand Down Expand Up @@ -100,7 +107,11 @@ public function export()
$columns = $this->grid->columns();
}
$headings = $this->getHeadings($columns);
$dataSource = new DataSource();
if ($this->dataSource instanceof DataSource) {
$dataSource = $this->dataSource;
} else {
$dataSource = new DataSource();
}
$dataSource->setHeadings(array_values($headings));
$dataSource->setEvents($this->events);

Expand Down

0 comments on commit 6bb5e5c

Please sign in to comment.