Skip to content

Commit eae5459

Browse files
authored
Merge pull request #79 from leantony/2.0
Add support for using custom title for filters
2 parents a5cecf4 + 1c46494 commit eae5459

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

src/Filters/GenericFilter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,13 @@ public function render()
158158
case 'text':
159159
// all filters apart from dropdowns are rendered as text elements.
160160
// css classes or js libraries can be used to change this
161-
return view('leantony::grid.filters.text', $this->compactData(func_get_args()))->render();
161+
return view('leantony::grid.filters.text', $this->compactData(
162+
array_collapse(func_get_args())
163+
))->render();
162164
case 'select':
163-
return view('leantony::grid.filters.dropdown', $this->compactData(func_get_args()))->render();
165+
return view('leantony::grid.filters.dropdown', $this->compactData(
166+
array_collapse(func_get_args())
167+
))->render();
164168
default:
165169
throw new \Exception("Unknown filter type.");
166170
}

src/Listeners/AddExtraAttributesToProcessedColumn.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function handle(ColumnProcessed $event)
3434

3535
$this->addHtmlCheckForLabel($data, $col);
3636

37+
$this->addTitleForFilter($data, $col);
38+
3739
// make sure the column object is returned
3840
return $col;
3941
}
@@ -54,4 +56,22 @@ public function addHtmlCheckForLabel($columnData, $col): void
5456

5557
$col->useRawHtmlForLabel = $useRawHtmlForLabel;
5658
}
59+
60+
/**
61+
* Add title for filter. This will not be used if not set
62+
* So, the default one set on the filter will be used if this one is unavailable
63+
*
64+
* @param $columnData
65+
* @param $col
66+
*/
67+
public function addTitleForFilter($columnData, $col): void
68+
{
69+
if (isset($columnData['filter'])) {
70+
$filterTitle = $columnData['filter']['title'] ?? null;
71+
} else {
72+
$filterTitle = null;
73+
}
74+
75+
$col->filterTitle = $filterTitle;
76+
}
5777
}

src/resources/views/grid/filter.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
@foreach($columns as $row)
2-
@if(!$row->filter)
1+
@foreach($columns as $col)
2+
@if(!$col->filter)
33
<th></th>
4-
@elseif(!$row->filter->enabled)
4+
@elseif(!$col->filter->enabled)
55
<th></th>
66
@else
77
<th>
8-
{!! $row->filter !!}
8+
{!! $col->filter->render(['titleSetOnColumn' => $col->filterTitle]) !!}
99
</th>
1010
@endif
1111
@if($loop->last)

src/resources/views/grid/filters/text.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<input type="text" name="{{ $name }}" id="{{ $id }}"
22
form="{{ $formId }}"
3-
class="{{ $class }}" value="{{ request($name) }}" title="{{ $title }}" placeholder="{{ $title }}"
3+
class="{{ $class }}" value="{{ request($name) }}" title="{{ $titleSetOnColumn ?? $title }}" placeholder="{{ $titleSetOnColumn ?? $title }}"
44
@foreach($dataAttributes as $k => $v)
55
data-{{ $k }}={{ $v }}
66
@endforeach

tests/PackageTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,22 @@ public function grid_can_render_using_custom_layout()
235235
// default layout uses a bootstrap4 card
236236
$this->assertContains("dummy", $content);
237237
}
238+
239+
/**
240+
* @throws \Throwable
241+
* @test
242+
*/
243+
public function grid_can_add_custom_filter_titles()
244+
{
245+
$filterText = "filter-by-foo-bar";
246+
$existingFilterTextSampleNotExisting = "filter by name";
247+
$existingFilterTextSampleExisting = "filter by created_at";
248+
$grid = $this->getGridInstances()['users_customized'];
249+
/** @var $grid UsersGrid */
250+
$content = $grid->render();
251+
252+
$this->assertContains($filterText, $content);
253+
$this->assertContains($existingFilterTextSampleExisting, $content);
254+
$this->assertNotContains($existingFilterTextSampleNotExisting, $content);
255+
}
238256
}

tests/Setup/Grids/UsersGridCustomized.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function setColumns()
4646
"id" => [
4747
"label" => "ID",
4848
"filter" => [
49+
"title" => "filter-by-foo-bar",
4950
"enabled" => true,
5051
"operator" => "="
5152
],

0 commit comments

Comments
 (0)