Skip to content

Instantly share code, notes, and snippets.

@pxpm
Created December 4, 2020 14:19
Show Gist options
  • Save pxpm/38a4b2fd4ab65447945920b82845d326 to your computer and use it in GitHub Desktop.
Save pxpm/38a4b2fd4ab65447945920b82845d326 to your computer and use it in GitHub Desktop.
<!-- checklist -->
@php
$model = new $field['model'];
$key_attribute = $model->getKeyName();
$identifiable_attribute = $field['attribute'];
// calculate the checklist options
if (!isset($field['options'])) {
$field['options'] = $field['model']::all()->pluck($identifiable_attribute, $key_attribute)->toArray();
} else {
$field['options'] = call_user_func($field['options'], $field['model']::query());
}
// calculate the value of the hidden input
$field['value'] = old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? [];
if ($field['value'] instanceof Illuminate\Database\Eloquent\Collection) {
$field['value'] = $field['value']->pluck($key_attribute)->toArray();
}elseif(is_string($field['value'])){
$field['value'] = json_decode($field['value']);
}
// define the init-function on the wrapper
$field['wrapper']['data-init-function'] = $field['wrapper']['data-init-function'] ?? 'bpFieldInitChecklist';
@endphp
@include('crud::fields.inc.wrapper_start')
<label>{!! $field['label'] !!}</label>
@include('crud::fields.inc.translatable_icon')
<input type="hidden" value='@json($field['value'])' name="{{ $field['name'] }}">
<div class="row">
@foreach ($field['options'] as $key => $option)
<div class="col-sm-4">
<div class="checkbox">
<label class="font-weight-normal">
<input type="checkbox" value="{{ $key }}"> {{ $option }}
</label>
</div>
</div>
@endforeach
</div>
{{-- HINT --}}
@if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
@endif
@include('crud::fields.inc.wrapper_end')
{{-- ########################################## --}}
{{-- Extra CSS and JS for this particular field --}}
{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}}
@if ($crud->fieldTypeNotLoaded($field))
@php
$crud->markFieldTypeAsLoaded($field);
@endphp
{{-- FIELD JS - will be loaded in the after_scripts section --}}
@push('crud_fields_scripts')
<script>
function bpFieldInitChecklist(element) {
var hidden_input = element.find('input[type=hidden]');
var selected_options = JSON.parse(hidden_input.val() || '[]');
var checkboxes = element.find('input[type=checkbox]');
var container = element.find('.row');
// set the default checked/unchecked states on checklist options
console.log(selected_options);
if(selected_options != null) {
checkboxes.each(function(key, option) {
var id = $(this).val();
if (selected_options.map(String).includes(id)) {
$(this).prop('checked', 'checked');
} else {
$(this).prop('checked', false);
}
});
}
// when a checkbox is clicked
// set the correct value on the hidden input
checkboxes.click(function() {
var newValue = [];
checkboxes.each(function() {
if ($(this).is(':checked')) {
var id = $(this).val();
newValue.push(id);
}
});
hidden_input.val(JSON.stringify(newValue));
});
}
</script>
@endpush
@endif
{{-- End of Extra CSS and JS --}}
{{-- ########################################## --}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment