Skip to content

Commit

Permalink
Merge pull request #62 from leantony/2.0
Browse files Browse the repository at this point in the history
Add support for disabling of global search field
  • Loading branch information
leantony authored Jun 17, 2018
2 parents e0a5f0a + ec85a70 commit 4e28cf7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/Events/GridInitialized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright (c) 2018.
* @author Antony [leantony] Chacha
*/

namespace Leantony\Grid\Events;

use Leantony\Grid\GridInterface;

class GridInitialized
{
/**
* @var GridInterface
*/
public $grid;

/**
* @var array
*/
public $params;

/**
* GridInitialized constructor.
* @param GridInterface $grid
* @param array $params
*/
public function __construct(GridInterface $grid, array $params)
{
$this->grid = $grid;
$this->params = $params;
}

}
31 changes: 31 additions & 0 deletions src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Leantony\Grid\Buttons\RendersButtons;
use Leantony\Grid\Columns\CreatesColumns;
use Leantony\Grid\Columns\GridColumnsInterface;
use Leantony\Grid\Events\GridInitialized;
use Leantony\Grid\Events\UserActionRequested;
use Leantony\Grid\Filters\AddsColumnFilters;
use Leantony\Grid\Filters\GridFilterInterface;
Expand Down Expand Up @@ -116,6 +117,13 @@ abstract class Grid implements Htmlable, GridInterface, GridButtonsInterface, Gr
*/
protected $tableColumns = [];

/**
* Controls rendering or not, for the search form
*
* @var bool
*/
protected $shouldRenderSearchForm = true;

/**
* Create the grid
*
Expand All @@ -129,6 +137,8 @@ public function create(array $params): GridInterface
$this->__set($k, $v);
}
$this->init();
// initialized event
event('grid.initialized', new GridInitialized($this, $params));
// do filter, export, paginate, search = main user actions
$result = event(
'grid.fetch_data',
Expand All @@ -139,6 +149,17 @@ public function create(array $params): GridInterface
return $this;
}

/**
* Define rendering of the search form
*
* @return GridInterface
*/
public function withoutSearchForm(): GridInterface
{
$this->shouldRenderSearchForm = false;
return $this;
}

/**
* Get the selected sort direction
*
Expand Down Expand Up @@ -299,6 +320,16 @@ protected function setGridDataItems(array $result): void
}
}

/**
* Grid should render search form
*
* @return bool
*/
public function shouldRenderSearchForm()
{
return $this->shouldRenderSearchForm;
}

/**
* Render the search form on the grid
*
Expand Down
33 changes: 33 additions & 0 deletions src/Listeners/GridWasInitialized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright (c) 2018.
* @author Antony [leantony] Chacha
*/

namespace Leantony\Grid\Listeners;

use Leantony\Grid\Events\GridInitialized;

class GridWasInitialized
{
/**
* Create the event listener.
*
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param GridInitialized $event
* @return mixed
* @throws \Throwable
*/
public function handle(GridInitialized $event)
{
//
}
}
1 change: 1 addition & 0 deletions src/Providers/GridServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function registerCustomEvents(): void
// events
Event::listen('grid.fetch_data', 'Leantony\\Grid\\Listeners\\HandleUserAction@handle');
Event::listen('grid.column_processed', 'Leantony\\Grid\\Listeners\\AddExtraAttributesToProcessedColumn@handle');
Event::listen('grid.initialized', 'Leantony\\Grid\\Listeners\\GridWasInitialized@handle');
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/resources/views/grid/grid.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<div class="card-body">
<!-- search form -->
<div class="row">
{!! $grid->renderSearchForm() !!}
@if($grid->shouldRenderSearchForm())
{!! $grid->renderSearchForm() !!}
@endif
<!-- toolbar buttons -->
@if($grid->hasButtons('toolbar'))
<div class="col-md-{{ $grid->getGridToolbarSize()[1] }}">
Expand All @@ -22,7 +24,7 @@
@endforeach
</div>
</div>
@endif
@endif
<!-- end toolbar buttons -->
</div>
<!-- end search form -->
Expand Down
15 changes: 15 additions & 0 deletions tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,19 @@ public function grid_can_add_footer()

$this->assertContains("Total:${existingTotal}", $content);
}

/**
* @throws \Exception
* @test
* @throws \Throwable
*/
public function grid_can_disable_rendering_of_search_form()
{
$grid = $this->getGridInstances()['users_default'];
/** @var $grid UsersGrid */
$grid->withoutSearchForm();
$id = $grid->getId();
$content = $grid->render();
$this->assertNotContains("<form method='GET' id=\"${id}\"", $content);
}
}

0 comments on commit 4e28cf7

Please sign in to comment.