Skip to content

Commit 2c4834c

Browse files
authored
Merge pull request z-song#5310 from mhanoglu/feature/form-checkbox-group
Groupping support for checkbox options
2 parents 0c661b2 + 3fc3099 commit 2c4834c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

resources/views/form/checkbox.blade.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@
1515

1616
@include('admin::form.error')
1717

18+
@if($groups)
19+
20+
@foreach($groups as $group => $options)
21+
22+
<p style="{{ $canCheckAll ? 'margin: 15px 0 0 0;' : 'margin: 7px 0 0 0;' }}padding-bottom: 5px;border-bottom: 1px solid #eee;display: inline-block;">{{ $group }}</p>
23+
24+
@foreach($options as $option => $label)
25+
26+
<div class="checkbox icheck">
27+
28+
<label>
29+
<input type="checkbox" name="{{$name}}[]" value="{{$option}}" class="{{$class}}" {{ false !== array_search($option, array_filter(old($column, $value ?? []))) || ($value === null && in_array($option, $checked)) ?'checked':'' }} {!! $attributes !!} />&nbsp;{{$label}}&nbsp;&nbsp;
30+
</label>
31+
32+
</div>
33+
34+
@endforeach
35+
36+
@endforeach
37+
38+
@else
39+
1840
@foreach($options as $option => $label)
1941

2042
{!! $inline ? '<span class="icheck">' : '<div class="checkbox icheck">' !!}
@@ -27,6 +49,8 @@
2749

2850
@endforeach
2951

52+
@endif
53+
3054
<input type="hidden" name="{{$name}}[]">
3155

3256
@include('admin::form.help-block')

src/Form/Field/Checkbox.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Checkbox extends MultipleSelect
1010

1111
protected $canCheckAll = false;
1212

13+
protected $groups = null;
14+
1315
protected static $css = [
1416
'/vendor/laravel-admin/AdminLTE/plugins/iCheck/all.css',
1517
];
@@ -57,6 +59,20 @@ public function canCheckAll()
5759
return $this;
5860
}
5961

62+
/**
63+
* Set chekbox groups.
64+
*
65+
* @param array
66+
*
67+
* @return $this
68+
*/
69+
public function groups($groups = [])
70+
{
71+
$this->groups = $groups;
72+
73+
return $this;
74+
}
75+
6076
/**
6177
* Set checked.
6278
*
@@ -110,6 +126,7 @@ public function render()
110126
'checked' => $this->checked,
111127
'inline' => $this->inline,
112128
'canCheckAll' => $this->canCheckAll,
129+
'groups' => $this->groups,
113130
]);
114131

115132
if ($this->canCheckAll) {

0 commit comments

Comments
 (0)