mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
refactor
This commit is contained in:
@@ -494,7 +494,7 @@ ImportColumn::make('sku')
|
||||
->example('ABC123')
|
||||
```
|
||||
|
||||
Or if you want to add more than one example row, you can pass array to `examples()` method:
|
||||
Or if you want to add more than one example row, you can pass an array to the `examples()` method:
|
||||
|
||||
```php
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
|
||||
@@ -334,24 +334,26 @@ trait CanImportRecords
|
||||
$columns,
|
||||
));
|
||||
|
||||
$rowsCount = array_reduce(
|
||||
$columnExamples = array_map(
|
||||
fn (ImportColumn $column): array => $column->getExamples(),
|
||||
$columns,
|
||||
function (?int $maxCount, ImportColumn $column) {
|
||||
return max($maxCount, count($column->getExamples()));
|
||||
},
|
||||
0
|
||||
);
|
||||
|
||||
$examples = [];
|
||||
foreach ($columns as $column) {
|
||||
$columnExamples = $column->getExamples();
|
||||
$exampleRowsCount = array_reduce(
|
||||
$columnExamples,
|
||||
fn (int $count, array $exampleData): int => max($count, count($exampleData)),
|
||||
initial: 0,
|
||||
);
|
||||
|
||||
for($i = 0; $i < $rowsCount; $i++) {
|
||||
$examples[$i][] = $columnExamples[$i] ?? '';
|
||||
$exampleRows = [];
|
||||
|
||||
foreach ($columnExamples as $exampleData) {
|
||||
for ($i = 0; $i < $exampleRowsCount; $i++) {
|
||||
$exampleRows[$i][] = $exampleData[$i] ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
$csv->insertAll($examples);
|
||||
$csv->insertAll($exampleRows);
|
||||
|
||||
return response()->streamDownload(function () use ($csv) {
|
||||
echo $csv->toString();
|
||||
|
||||
@@ -119,12 +119,12 @@ class ImportColumn extends Component
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $examples
|
||||
*/
|
||||
public function examples(mixed $examples): static
|
||||
{
|
||||
if (!is_array($examples) && !$examples instanceof Closure) {
|
||||
if (
|
||||
(! is_array($examples)) &&
|
||||
(! $examples instanceof Closure)
|
||||
) {
|
||||
$examples = Arr::wrap($examples);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user