Skip to content

Commit

Permalink
Support computed fields (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski authored Oct 21, 2023
1 parent aa87a20 commit b583baf
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 198 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"nova:install": "npm --prefix='../../vendor/laravel/nova' ci"
},
"devDependencies": {
"@vue/compiler-sfc": "^3.2.22",
"@vue/compiler-sfc": "^3.3.6",
"laravel-mix": "^6.0.41",
"postcss": "^8.3.11",
"vue-loader": "^17.3.0",
Expand Down
11 changes: 8 additions & 3 deletions src/ColumnTogglerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function serializeForIndex(NovaRequest $request, $fields = null): array
$indexFields = $fields ?? parent::indexFields($request);

$columns = $indexFields->map(fn (Field $field) => [
'attribute' => $field->attribute,
'attribute' => $this->generateAttributeName($field),
'label' => $field->name,
'visible' => data_get($field->meta(), 'columnToggleVisible', true),
]);
Expand Down Expand Up @@ -61,10 +61,15 @@ private function filterFields(NovaRequest $request, FieldCollection $fields): Fi
$columns = collect(json_decode(base64_decode($value)));

return $fields
->filter(fn (Field $field) => $columns->contains($field->attribute))
->sortBy(fn (Field $field) => $columns->search($field->attribute));
->filter(fn (Field $field) => $columns->contains($this->generateAttributeName($field)))
->sortBy(fn (Field $field) => $columns->search($this->generateAttributeName($field)));

},
)->values();
}

private function generateAttributeName(Field $field): string
{
return md5(sprintf('%s-%s', $field->name, $field->attribute));
}
}
Loading

0 comments on commit b583baf

Please sign in to comment.