Skip to content

Commit

Permalink
Merge pull request #5310 from mhanoglu/feature/form-checkbox-group
Browse files Browse the repository at this point in the history
Groupping support for checkbox options
  • Loading branch information
jxlwqq authored May 3, 2021
2 parents 0c661b2 + 3fc3099 commit 2c4834c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
24 changes: 24 additions & 0 deletions resources/views/form/checkbox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@

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

@if($groups)

@foreach($groups as $group => $options)

<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>

@foreach($options as $option => $label)

<div class="checkbox icheck">

<label>
<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;
</label>

</div>

@endforeach

@endforeach

@else

@foreach($options as $option => $label)

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

@endforeach

@endif

<input type="hidden" name="{{$name}}[]">

@include('admin::form.help-block')
Expand Down
17 changes: 17 additions & 0 deletions src/Form/Field/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Checkbox extends MultipleSelect

protected $canCheckAll = false;

protected $groups = null;

protected static $css = [
'/vendor/laravel-admin/AdminLTE/plugins/iCheck/all.css',
];
Expand Down Expand Up @@ -57,6 +59,20 @@ public function canCheckAll()
return $this;
}

/**
* Set chekbox groups.
*
* @param array
*
* @return $this
*/
public function groups($groups = [])
{
$this->groups = $groups;

return $this;
}

/**
* Set checked.
*
Expand Down Expand Up @@ -110,6 +126,7 @@ public function render()
'checked' => $this->checked,
'inline' => $this->inline,
'canCheckAll' => $this->canCheckAll,
'groups' => $this->groups,
]);

if ($this->canCheckAll) {
Expand Down

0 comments on commit 2c4834c

Please sign in to comment.