Skip to content

Commit

Permalink
Merge pull request #206 from lara-zeus/add-table-column
Browse files Browse the repository at this point in the history
allow to customize your table column for each field type
  • Loading branch information
atmonshi authored Nov 12, 2023
2 parents 63ea394 + a4629a8 commit 1045fc0
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Fields/FieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Filament\Forms\Get;
use Filament\Support\Colors\Color;
use Filament\Tables\Columns\Column;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use LaraZeus\Bolt\BoltPlugin;
Expand All @@ -13,6 +16,7 @@
use LaraZeus\Bolt\Facades\Bolt;
use LaraZeus\Bolt\Models\Field;
use LaraZeus\Bolt\Models\FieldResponse;
use LaraZeus\Bolt\Models\Response;
use LaraZeus\BoltPro\Models\Field as FieldPreset;

/** @phpstan-return Arrayable<string,mixed> */
Expand Down Expand Up @@ -191,7 +195,6 @@ public static function getFieldCollectionItemsList(Field | FieldPreset $zeusFiel
}
} else {
if (class_exists($zeusField->options['dataSource'])) {
//@phpstan-ignore-next-line
$dataSourceClass = new $zeusField->options['dataSource'];

Check failure on line 198 in src/Fields/FieldsContract.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $options on an unknown class LaraZeus\BoltPro\Models\Field.

Check failure on line 198 in src/Fields/FieldsContract.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $options on an unknown class LaraZeus\BoltPro\Models\Field.
$getCollection = $dataSourceClass->getModel()::pluck(
$dataSourceClass->getValuesUsing(),
Expand All @@ -202,4 +205,29 @@ public static function getFieldCollectionItemsList(Field | FieldPreset $zeusFiel

return $getCollection;
}

public function TableColumn(Field $field): ?Column
{
return TextColumn::make('zeusData.' . $field->id)
->label($field->name)
->searchable(query: function (Builder $query, string $search): Builder {
return $query
->whereHas('fieldsResponses', function ($query) use ($search) {
$query->where('response', 'like', '%' . $search . '%');
});
})
->getStateUsing(fn (Response $record) => $this->getFieldResponseValue($record, $field))
->html()
->toggleable();
}

public function getFieldResponseValue(Response $record, Field $field): string
{
$fieldResponse = $record->fieldsResponses->where('field_id', $field->id)->first();
if ($fieldResponse === null) {
return '';
}

return (new $field->type)->getResponse($field, $fieldResponse);
}
}

0 comments on commit 1045fc0

Please sign in to comment.