|
| 1 | +<!-- select2 from ajax multiple --> |
| 2 | +@php |
| 3 | + $connected_entity = new $field['model']; |
| 4 | + $connected_entity_key_name = $connected_entity->getKeyName(); |
| 5 | + $old_value = old($field['name']) ? old($field['name']) : (isset($field['value']) ? $field['value'] : (isset($field['default']) ? $field['default'] : false )); |
| 6 | +@endphp |
| 7 | + |
| 8 | +<div @include('crud::inc.field_wrapper_attributes') > |
| 9 | + <label>{!! $field['label'] !!}</label> |
| 10 | + @include('crud::inc.field_translatable_icon') |
| 11 | + <select |
| 12 | + name="{{ $field['name'] }}[]" |
| 13 | + style="width: 100%" |
| 14 | + id="select2_ajax_multiple_{{ $field['name'] }}" |
| 15 | + @include('crud::inc.field_attributes', ['default_class' => 'form-control']) |
| 16 | + multiple> |
| 17 | + |
| 18 | + @if ($old_value) |
| 19 | + @foreach ($old_value as $item) |
| 20 | + @if (!is_object($item)) |
| 21 | + @php |
| 22 | + $item = $connected_entity->find($item); |
| 23 | + @endphp |
| 24 | + @endif |
| 25 | + <option value="{{ $item->getKey() }}" selected> |
| 26 | + {{ $item->{$field['attribute']} }} |
| 27 | + </option> |
| 28 | + @endforeach |
| 29 | + @endif |
| 30 | + </select> |
| 31 | + |
| 32 | + @if (isset($field['on_the_fly'])) |
| 33 | + <button href="{{ $field['on_the_fly']['create_view'] }}" |
| 34 | + type="button" |
| 35 | + class="btn btn-primary" |
| 36 | + data-toggle="modal" |
| 37 | + data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"> |
| 38 | + {{ trans('backpack::crud.add') }} |
| 39 | + </button> |
| 40 | + <div class="modal fade" |
| 41 | + id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal" |
| 42 | + tabindex="-1" |
| 43 | + role="dialog" |
| 44 | + aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal" |
| 45 | + aria-hidden="true"> |
| 46 | + <div class="modal-dialog" role="document"> |
| 47 | + <div class="modal-content"></div> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + @endif |
| 51 | + |
| 52 | + {{-- HINT --}} |
| 53 | + @if (isset($field['hint'])) |
| 54 | + <p class="help-block">{!! $field['hint'] !!}</p> |
| 55 | + @endif |
| 56 | +</div> |
| 57 | + |
| 58 | + |
| 59 | +{{-- ########################################## --}} |
| 60 | +{{-- Extra CSS and JS for this particular field --}} |
| 61 | +{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}} |
| 62 | +@if ($crud->checkIfFieldIsFirstOfItsType($field, $fields)) |
| 63 | + |
| 64 | + {{-- FIELD CSS - will be loaded in the after_styles section --}} |
| 65 | + @push('crud_fields_styles') |
| 66 | + <!-- include select2 css--> |
| 67 | + <link href="{{ asset('vendor/adminlte/bower_components/select2/dist/css/select2.min.css') }}" rel="stylesheet" type="text/css" /> |
| 68 | + <link href="https://cdnjs.cloudflare.com/ajax/libs/select2-bootstrap-theme/0.1.0-beta.10/select2-bootstrap.min.css" rel="stylesheet" type="text/css" /> |
| 69 | + @endpush |
| 70 | + |
| 71 | + {{-- FIELD JS - will be loaded in the after_scripts section --}} |
| 72 | + @push('crud_fields_scripts') |
| 73 | + <!-- include select2 js--> |
| 74 | + <script src="{{ asset('vendor/adminlte/bower_components/select2/dist/js/select2.min.js') }}"></script> |
| 75 | + @endpush |
| 76 | + |
| 77 | +@endif |
| 78 | + |
| 79 | +<!-- include field specific select2 js--> |
| 80 | +@push('crud_fields_scripts') |
| 81 | +<script> |
| 82 | + jQuery(document).ready(function($) { |
| 83 | + // trigger select2 for each untriggered select2 box |
| 84 | + $("#select2_ajax_multiple_{{ $field['name'] }}").each(function (i, obj) { |
| 85 | + if (!$(obj).hasClass("select2-hidden-accessible")) |
| 86 | + { |
| 87 | + $(obj).select2({ |
| 88 | + theme: 'bootstrap', |
| 89 | + multiple: true, |
| 90 | + placeholder: "{{ $field['placeholder'] }}", |
| 91 | + minimumInputLength: "{{ $field['minimum_input_length'] }}", |
| 92 | + ajax: { |
| 93 | + url: "{{ $field['data_source'] }}", |
| 94 | + dataType: 'json', |
| 95 | + quietMillis: 250, |
| 96 | + data: function (params) { |
| 97 | + return { |
| 98 | + q: params.term, // search term |
| 99 | + searchkey: "{{ $field['attribute'] }}", // search key in database |
| 100 | + page: params.page |
| 101 | + }; |
| 102 | + }, |
| 103 | + processResults: function (data, params) { |
| 104 | + params.page = params.page || 1; |
| 105 | +
|
| 106 | + return { |
| 107 | + results: $.map(data.data, function (item) { |
| 108 | + return { |
| 109 | + text: item["{{$field['attribute']}}"], |
| 110 | + id: item["{{ $connected_entity_key_name }}"] |
| 111 | + } |
| 112 | + }), |
| 113 | + more: data.current_page < data.last_page |
| 114 | + }; |
| 115 | + }, |
| 116 | + cache: true |
| 117 | + }, |
| 118 | + }); |
| 119 | + } |
| 120 | + }); |
| 121 | + }); |
| 122 | +</script> |
| 123 | +@endpush |
| 124 | +{{-- End of Extra CSS and JS --}} |
| 125 | +{{-- ########################################## --}} |
0 commit comments