Skip to content

Commit 38b4abd

Browse files
committed
Add option to set exportFormat from Column builder.
1 parent 961526f commit 38b4abd

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ You can set the export type by setting the property to `csv` or `xlsx`. Default
8686
<livewire:export-button :table-id="$dataTable->getTableId()" type="csv" />
8787
```
8888

89+
## Formatting Columns
90+
91+
You can format column by setting it via Column definition on you DataTable service class.
92+
93+
```phpt
94+
Column::make('mobile')->exportFormat('00000000000'),
95+
```
96+
97+
The format above will treat mobile numbers with leading zeroes.
8998

9099
## Contributing
91100

src/Exports/DataTableQueuedExport.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use Illuminate\Support\Collection;
66
use Maatwebsite\Excel\Concerns\Exportable;
77
use Maatwebsite\Excel\Concerns\FromQuery;
8+
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
89
use Maatwebsite\Excel\Concerns\WithHeadings;
910
use Maatwebsite\Excel\Concerns\WithMapping;
11+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
1012
use Yajra\DataTables\Html\Column;
1113

12-
class DataTableQueuedExport implements FromQuery, WithMapping, WithHeadings
14+
class DataTableQueuedExport implements FromQuery, WithMapping, WithHeadings, WithColumnFormatting
1315
{
1416
use Exportable;
1517

@@ -40,4 +42,26 @@ public function headings(): array
4042
{
4143
return $this->columns->pluck('title')->toArray();
4244
}
45+
46+
public function columnFormats(): array
47+
{
48+
$formats = [];
49+
50+
$this->columns
51+
->each(function (Column $column, $index) use (&$formats) {
52+
$formats[$this->num2alpha($index - 1)] = $column['exportFormat'] ?? NumberFormat::FORMAT_TEXT;
53+
})
54+
->toArray();
55+
56+
return $formats;
57+
}
58+
59+
protected function num2alpha($n)
60+
{
61+
for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) {
62+
$r = chr($n % 26 + 0x41) . $r;
63+
}
64+
65+
return $r;
66+
}
4367
}

0 commit comments

Comments
 (0)