Skip to content

Commit

Permalink
Added ability to choose which columns can be searched
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Harman committed Jul 12, 2019
1 parent 014028a commit e0cbf2c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
10 changes: 6 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions src/InertiaTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@
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
$table = $model->getTable();
$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 = [];
Expand Down

0 comments on commit e0cbf2c

Please sign in to comment.