Skip to content

Commit 70d7cc7

Browse files
committed
[EH] add modified select2_from_ajax field
1 parent 23d5f7f commit 70d7cc7

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<!-- select2 from ajax -->
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+
<?php $entity_model = $crud->model; ?>
11+
12+
<select
13+
name="{{ $field['name'] }}"
14+
style="width: 100%"
15+
id="select2_ajax_{{ $field['name'] }}"
16+
@include('crud::inc.field_attributes', ['default_class' => 'form-control'])
17+
>
18+
19+
@if ($old_value)
20+
@php
21+
$item = $connected_entity->find($old_value);
22+
@endphp
23+
@if ($item)
24+
25+
{{-- allow clear --}}
26+
@if ($entity_model::isColumnNullable($field['name']))
27+
<option value="" selected>
28+
{{ $field['placeholder'] }}
29+
</option>
30+
@endif
31+
32+
<option value="{{ $item->getKey() }}" selected>
33+
{{ $item->{$field['attribute']} }}
34+
</option>
35+
@endif
36+
@endif
37+
</select>
38+
39+
@if (isset($field['on_the_fly']))
40+
<button href="{{ $field['on_the_fly']['create_view'] }}"
41+
type="button"
42+
class="btn btn-primary"
43+
data-toggle="modal"
44+
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal">
45+
{{ trans('backpack::crud.add') }}
46+
</button>
47+
<div class="modal fade"
48+
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
49+
tabindex="-1"
50+
role="dialog"
51+
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
52+
aria-hidden="true">
53+
<div class="modal-dialog" role="document">
54+
<div class="modal-content"></div>
55+
</div>
56+
</div>
57+
@endif
58+
59+
{{-- HINT --}}
60+
@if (isset($field['hint']))
61+
<p class="help-block">{!! $field['hint'] !!}</p>
62+
@endif
63+
</div>
64+
65+
66+
67+
68+
{{-- ########################################## --}}
69+
{{-- Extra CSS and JS for this particular field --}}
70+
{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}}
71+
@if ($crud->checkIfFieldIsFirstOfItsType($field, $fields))
72+
73+
{{-- FIELD CSS - will be loaded in the after_styles section --}}
74+
@push('crud_fields_styles')
75+
<!-- include select2 css-->
76+
<link href="{{ asset('vendor/adminlte/bower_components/select2/dist/css/select2.min.css') }}" rel="stylesheet"
77+
type="text/css"/>
78+
<link
79+
href="https://cdnjs.cloudflare.com/ajax/libs/select2-bootstrap-theme/0.1.0-beta.10/select2-bootstrap.min.css"
80+
rel="stylesheet" type="text/css"/>
81+
{{-- allow clear --}}
82+
@if ($entity_model::isColumnNullable($field['name']))
83+
<style type="text/css">
84+
.select2-selection__clear::after {
85+
content: ' {{ trans('backpack::crud.clear') }}';
86+
}
87+
</style>
88+
@endif
89+
@endpush
90+
91+
{{-- FIELD JS - will be loaded in the after_scripts section --}}
92+
@push('crud_fields_scripts')
93+
<!-- include select2 js-->
94+
<script src="{{ asset('vendor/adminlte/bower_components/select2/dist/js/select2.min.js') }}"></script>
95+
@endpush
96+
97+
@endif
98+
99+
<!-- include field specific select2 js-->
100+
@push('crud_fields_scripts')
101+
<script>
102+
jQuery(document).ready(function ($) {
103+
// trigger select2 for each untriggered select2 box
104+
$("#select2_ajax_{{ $field['name'] }}").each(function (i, obj) {
105+
if (!$(obj).hasClass("select2-hidden-accessible")) {
106+
107+
$(obj).select2({
108+
theme: 'bootstrap',
109+
multiple: false,
110+
placeholder: "{{ $field['placeholder'] }}",
111+
minimumInputLength: "{{ $field['minimum_input_length'] }}",
112+
113+
{{-- allow clear --}}
114+
@if ($entity_model::isColumnNullable($field['name']))
115+
allowClear: true,
116+
@endif
117+
ajax: {
118+
url: "{{ $field['data_source'] }}",
119+
dataType: 'json',
120+
quietMillis: 250,
121+
data: function (params) {
122+
return {
123+
q: params.term, // search term
124+
page: params.page
125+
};
126+
},
127+
processResults: function (data, params) {
128+
params.page = params.page || 1;
129+
130+
var result = {
131+
results: $.map(data.data, function (item) {
132+
textField = "{{ $field['attribute'] }}";
133+
return {
134+
text: item[textField],
135+
id: item["{{ $connected_entity_key_name }}"]
136+
}
137+
}),
138+
more: data.current_page < data.last_page
139+
};
140+
141+
return result;
142+
},
143+
cache: true
144+
},
145+
})
146+
{{-- allow clear --}}
147+
@if ($entity_model::isColumnNullable($field['name']))
148+
.on('select2:unselecting', function (e) {
149+
$(this).val('').trigger('change');
150+
// console.log('cleared! '+$(this).val());
151+
e.preventDefault();
152+
})
153+
@endif
154+
;
155+
156+
}
157+
});
158+
});
159+
</script>
160+
@endpush
161+
{{-- End of Extra CSS and JS --}}
162+
{{-- ########################################## --}}

src/InstantFieldsServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class InstantFieldsServiceProvider extends ServiceProvider
1717
*/
1818
public function boot()
1919
{
20-
//
20+
// publish views
21+
$this->publishes([__DIR__ . '/../resources/views' => resource_path('views/vendor/backpack/crud')], 'views');
2122
}
2223

2324
/**

0 commit comments

Comments
 (0)