Skip to content

Commit 290fb46

Browse files
committed
Merge branch '2.0'
2 parents eae5459 + 2ee8e24 commit 290fb46

File tree

6 files changed

+163
-121
lines changed

6 files changed

+163
-121
lines changed

src/Filters/AddsColumnFilters.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111

1212
trait AddsColumnFilters
1313
{
14+
// filters
15+
protected static $FILTER_DATE = "date";
16+
protected static $FILTER_DATE_RANGE = "daterange";
17+
protected static $FILTER_TEXT = "text";
18+
protected static $FILTER_SELECT = "select";
19+
protected static $FILTER_BOOLEAN = "boolean";
20+
21+
protected $defaultBooleanData = [0 => 'False', 1 => 'True'];
22+
1423
/**
1524
* Add a filter to the column. It will be rendered just below the column name, as a type defined below
1625
*
@@ -28,36 +37,42 @@ public function pushFilter($columnName, $columnData): GenericFilter
2837
$filterInstance = null;
2938
if (!$filterType instanceof GenericFilter) {
3039
switch ($filterType) {
31-
case 'date':
40+
case self::$FILTER_DATE:
3241
{
3342
$filterInstance = $this->addDateFilter(
3443
$filterEnabled, $columnName, $filterDataAttributes, $filterClass
3544
);
3645
break;
3746
}
38-
case 'daterange':
47+
case self::$FILTER_DATE_RANGE:
3948
{
4049
// uses https://github.com/dangrossman/bootstrap-daterangepicker.git
4150
$filterInstance = $this->addTextFilter(
4251
$filterEnabled, $columnName, $filterClass . ' date-range'
4352
);
4453
break;
4554
}
46-
case 'text':
55+
case self::$FILTER_TEXT:
4756
{
4857
// use text for any other filter type. E.g a custom one you might need
4958
$filterInstance = $this->addTextFilter(
5059
$filterEnabled, $columnName, $filterClass
5160
);
5261
break;
5362
}
54-
case 'select':
63+
case self::$FILTER_SELECT:
5564
{
5665
$filterInstance = $this->addSelectFilter(
5766
$filterEnabled, $columnName, $columnData['data'] ?? []
5867
);
5968
break;
6069
}
70+
case self::$FILTER_BOOLEAN: {
71+
$filterInstance = $this->addSelectFilter(
72+
$filterEnabled, $columnName, $columnData['data'] ?? collect($this->defaultBooleanData)
73+
);
74+
break;
75+
}
6176
default:
6277
throw new InvalidArgumentException("Unknown filterType type " . $filterType . " for " . $columnName);
6378
}

tests/PackageTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,17 @@ public function grid_can_add_custom_filter_titles()
253253
$this->assertContains($existingFilterTextSampleExisting, $content);
254254
$this->assertNotContains($existingFilterTextSampleNotExisting, $content);
255255
}
256-
}
256+
257+
/**
258+
* @throws \Throwable
259+
* @test
260+
*/
261+
public function grid_can_add_boolean_filter()
262+
{
263+
$filterText = "False";
264+
$grid = $this->getGridInstances()['users_customized'];
265+
/** @var $grid UsersGrid */
266+
$content = $grid->render();
267+
$this->assertContains($filterText, $content);
268+
}
269+
}

tests/Setup/Grids/UsersGrid.php

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,33 @@ class UsersGrid extends Grid implements UsersGridInterface
3636
protected $linkableRows = false;
3737

3838
/**
39-
* Set the columns to be displayed.
40-
*
41-
* @return void
42-
* @throws \Exception if an error occurs during parsing of the data
43-
*/
39+
* Set the columns to be displayed.
40+
*
41+
* @return void
42+
* @throws \Exception if an error occurs during parsing of the data
43+
*/
4444
public function setColumns()
4545
{
4646
$this->columns = [
47-
"id" => [
48-
"label" => "ID",
49-
"filter" => [
50-
"enabled" => true,
51-
"operator" => "="
52-
],
53-
"styles" => [
54-
"column" => "grid-w-10"
55-
]
56-
],
57-
"name" => [
58-
"search" => [
59-
"enabled" => true
60-
],
61-
"filter" => [
62-
"enabled" => true,
63-
"operator" => "="
64-
]
65-
],
47+
"id" => [
48+
"label" => "ID",
49+
"filter" => [
50+
"enabled" => true,
51+
"operator" => "="
52+
],
53+
"styles" => [
54+
"column" => "grid-w-10"
55+
]
56+
],
57+
"name" => [
58+
"search" => [
59+
"enabled" => true
60+
],
61+
"filter" => [
62+
"enabled" => true,
63+
"operator" => "="
64+
]
65+
],
6666
"role_id" => [
6767
'label' => 'Role',
6868
'export' => false,
@@ -76,25 +76,25 @@ public function setColumns()
7676
'data' => Role::query()->pluck('name', 'id')
7777
]
7878
],
79-
"email" => [
80-
"search" => [
81-
"enabled" => true
82-
],
83-
"filter" => [
84-
"enabled" => true,
85-
"operator" => "="
86-
]
87-
],
88-
"created_at" => [
89-
"sort" => false,
90-
"date" => "true",
91-
"filter" => [
92-
"enabled" => true,
93-
"type" => "date",
94-
"operator" => "<="
95-
]
96-
]
97-
];
79+
"email" => [
80+
"search" => [
81+
"enabled" => true
82+
],
83+
"filter" => [
84+
"enabled" => true,
85+
"operator" => "="
86+
]
87+
],
88+
"created_at" => [
89+
"sort" => false,
90+
"date" => "true",
91+
"filter" => [
92+
"enabled" => true,
93+
"type" => "date",
94+
"operator" => "<="
95+
]
96+
]
97+
];
9898
}
9999

100100
/**
@@ -117,10 +117,10 @@ public function setRoutes()
117117
}
118118

119119
/**
120-
* Return a closure that is executed per row, to render a link that will be clicked on to execute an action
121-
*
122-
* @return Closure
123-
*/
120+
* Return a closure that is executed per row, to render a link that will be clicked on to execute an action
121+
*
122+
* @return Closure
123+
*/
124124
public function getLinkableCallback(): Closure
125125
{
126126
return function ($gridName, $item) {
@@ -129,10 +129,10 @@ public function getLinkableCallback(): Closure
129129
}
130130

131131
/**
132-
* Configure rendered buttons, or add your own
133-
*
134-
* @return void
135-
*/
132+
* Configure rendered buttons, or add your own
133+
*
134+
* @return void
135+
*/
136136
public function configureButtons()
137137
{
138138
// call `addRowButton` to add a row button
@@ -145,11 +145,11 @@ public function configureButtons()
145145
}
146146

147147
/**
148-
* Returns a closure that will be executed to apply a class for each row on the grid
149-
* The closure takes two arguments - `name` of grid, and `item` being iterated upon
150-
*
151-
* @return Closure
152-
*/
148+
* Returns a closure that will be executed to apply a class for each row on the grid
149+
* The closure takes two arguments - `name` of grid, and `item` being iterated upon
150+
*
151+
* @return Closure
152+
*/
153153
public function getRowCssStyle(): Closure
154154
{
155155
return function ($gridName, $item) {

0 commit comments

Comments
 (0)