From e0cbf2ca25173a5411fb32b995d94025264aadac Mon Sep 17 00:00:00 2001 From: Craig Harman Date: Fri, 12 Jul 2019 16:13:45 +0800 Subject: [PATCH] Added ability to choose which columns can be searched --- changelog.md | 10 ++++++---- readme.md | 2 ++ src/InertiaTable.php | 17 ++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/changelog.md b/changelog.md index 6074ab1..494be57 100644 --- a/changelog.md +++ b/changelog.md @@ -2,17 +2,19 @@ All notable changes to `InertiaTable` will be documented in this file. -## TODO: -- Seperate column listing from column filterable so that user can choose which columns are filterable (InertiaTable.php) +## Version 1.0.3 + +### Added +- Can stipulate which fields are searchable ## Version 1.0.2 -## Fixed +### Fixed - Filter columns are based on $columns array ## Version 1.0.1 -## Fixed +### Fixed - Model/controller generation names diff --git a/readme.md b/readme.md index 8745cde..d35d587 100644 --- a/readme.md +++ b/readme.md @@ -66,6 +66,8 @@ class UsersController extends Controller { ``` The index method takes a model and an array of column names which you wish to display as parameters. The array is optional, InertiaTable will show all columns by default. +You can also stipulate which columns can be searched by adding a third parameter, an array of column names that can be filtered. If left blank all columns are searchable. + 3) You will need to create your front end. It is recommend you use [inertia-table-vue](https://github.com/Harmonic/inertia-table-vue) for Vue projects. A JS example is provided at the bottom of [that repository](https://github.com/Harmonic/inertia-table-vue). ## Change log diff --git a/src/InertiaTable.php b/src/InertiaTable.php index 4860337..4a493c0 100644 --- a/src/InertiaTable.php +++ b/src/InertiaTable.php @@ -7,17 +7,16 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Request; -class InertiaTable -{ +class InertiaTable { /** * Generates inertia view data for model. * * @param InertiaModel $model The model to use to retrieve data - * @param array $columns An array of column names to send to front end (null for all columns) + * @param array $columns An array of column names to send to front end (null for all columns)' + * @param array $filterable A subset of the $columns array containing names of columsn that can be filtered * @return void */ - public function index(InertiaModel $model, array $columns = null) - { + public function index(InertiaModel $model, array $columns = null, array $filterable = null) { $modelName = class_basename($model); if ($columns == null) { // default to all columns @@ -25,14 +24,18 @@ public function index(InertiaModel $model, array $columns = null) $columns = Schema::getColumnListing($table); } + if ($filterable == null) { + $filterable = $columns; + } + $modelPlural = Str::plural($modelName); - return Inertia::render($modelPlural.'/Index', [ + return Inertia::render($modelPlural . '/Index', [ 'filters' => Request::all('search', 'trashed'), 'order' => Request::all('orderColumn', 'orderDirection'), strtolower($modelPlural) => $model ->order(Request::input('orderColumn') ?? 'name', Request::input('orderDirection')) - ->filter(Request::only('search', 'trashed'), $columns) + ->filter(Request::only('search', 'trashed'), $filterable) ->get() ->transform(function ($item) use ($columns) { $data = [];