Skip to content

Commit c03e532

Browse files
authored
Merge pull request #30 from webfactor/2.2
2.2
2 parents 58c12ed + 0111885 commit c03e532

File tree

11 files changed

+128
-47
lines changed

11 files changed

+128
-47
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2121
### Security
2222
- Nothing
2323

24+
## 2.2.0 - 2019-03-17
25+
26+
### Added
27+
- modal_view parameters to use own modal view
28+
- `serialize` parameter to define which current field values should be passed to the foreign CrudController
29+
2430
## 2.1.0 - 2019-02-10
2531

2632
### Added

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,36 @@ With this package your are also able to add a create button for the foreign CRUD
164164

165165
### Modal view
166166

167+
#### By route
168+
167169
By default the modals are loaded automatically by using `entity` in `on_the_fly` of the field definition, e.g. resulting in `backpack_url($field['on_the_fly']['entity']).'/ajax/create'` for the create modal.
168170

169171
You can overwrite this behavior for all modals by setting an attribute:
170172

171173
```
172174
'on_the_fly' => [
173175
'entity' => 'entity',
174-
'create_modal' => 'route/to/modal/html',
175-
'edit_modal' => 'route/to/modal/html',
176-
'delete_modal' => 'route/to/modal/html',
176+
'create_modal' => 'route/to/modal/html',
177+
'edit_modal' => 'route/to/modal/html',
178+
'delete_modal' => 'route/to/modal/html',
177179
]
178180
```
179181

180182
> Please be aware that by using this attributes you will be completely responsible for the content of the modal! The defined request has to provide valid HTML which is then filled in `<div class="modal-content"></div>`
181183
184+
#### By view
185+
186+
Instead of defining a route you can also use a custom view by:
187+
188+
```
189+
'on_the_fly' => [
190+
'entity' => 'entity',
191+
'create_modal_view' => 'view.to.create.modal',
192+
'edit_modal_view' => 'view.to.edit.modal',
193+
'delete_modal_view' => 'view.to.delete.modal',
194+
]
195+
```
196+
182197
### Search logic
183198

184199
The "instant field" triggers the `ajaxIndex()` of the `EntityCrudController` where the field is defined and uses the fields `model` and `attribute` parameters to perform the search on the foreign model.
@@ -237,6 +252,18 @@ Instant Fields will try to auto-fill the select2 input after creating a new entr
237252
]
238253
```
239254

255+
### Passing current field values to foreign `EntityCrudController`
256+
257+
Sometimes you will need current values to be used for the creation of an foreign entity. You may define `serialize` with the IDs of the fields you need in the store request:
258+
259+
```
260+
'on_the_fly' => [
261+
'serialize' => ['type_id', 'name'],
262+
]
263+
```
264+
265+
So the current values of `type_id` and `name` will be available in the `$request` of the `ajaxStore` method of your foreign `EntityCrudController` (you will have to overwrite it).
266+
240267
### Fields
241268

242269
Publish the fields in your project and modify functionality

resources/views/fields/inc/button-add.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class="btn btn-primary"
66
style="border-radius: 0px"
77
data-toggle="modal"
88
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal"
9-
data-load-url="{{ $field['on_the_fly']['create_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create?field_name='.$field['name'].'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
9+
data-load-url="{{ $field['on_the_fly']['create_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create?field_name='.$field['name'].'&attribute='.($field['on_the_fly']['attribute'] ?? 'name').'&create_modal_view='.($field['on_the_fly']['create_modal_view'] ?? 'webfactor::modal.create').'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
1010
<i class="fa fa-plus"></i>
1111
</button>
1212
</span>

resources/views/fields/inc/button-delete.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class="btn btn-danger {{ isset($field['value']) ?: 'disabled'}}"
77
data-toggle="modal"
88
data-id="{{ $field['value'] ?? '' }}"
99
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_delete_modal"
10-
data-load-url="{{ $field['on_the_fly']['delete_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/delete?field_name='.$field['name'].'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
10+
data-load-url="{{ $field['on_the_fly']['delete_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/delete?field_name='.$field['name'].'&delete_modal_view='.($field['on_the_fly']['delete_modal_view'] ?? 'webfactor::modal.delete').'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
1111
<i class="fa fa-trash"></i>
1212
</button>
1313
</span>

resources/views/fields/inc/button-edit.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class="btn btn-warning {{ isset($field['value']) ?: 'disabled'}}"
77
data-toggle="modal"
88
data-id="{{ $field['value'] ?? '' }}"
99
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_edit_modal"
10-
data-load-url="{{ $field['on_the_fly']['edit_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/edit?field_name='.$field['name'].'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
10+
data-load-url="{{ $field['on_the_fly']['edit_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/edit?field_name='.$field['name'].'&edit_modal_view='.($field['on_the_fly']['edit_modal_view'] ?? 'webfactor::modal.edit').'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
1111
<i class="fa fa-pencil"></i>
1212
</button>
1313
</span>

resources/views/fields/select2_from_ajax.blade.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ class="form-control"
9797
// load create modal content
9898
$("#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal").on('show.bs.modal', function (e) {
9999
var loadurl = $(e.relatedTarget).data('load-url');
100+
var form = $(e.relatedTarget).closest('form');
100101
101-
$(this).find('.modal-content').load(loadurl);
102+
var data = form.serializeArray().filter(function(index){
103+
return $.inArray(index.name, <?php echo json_encode($field['on_the_fly']['serialize'] ?? []); ?>) >= 0;
104+
});
105+
106+
$(this).find('.modal-content').load(loadurl + '&' + $.param(data));
102107
});
103108
104109
// load edit/delete modal content

resources/views/fields/select2_from_ajax_multiple.blade.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@
7272
// load create modal content
7373
$("#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal").on('show.bs.modal', function (e) {
7474
var loadurl = $(e.relatedTarget).data('load-url');
75-
$(this).find('.modal-content').load(loadurl);
75+
var form = $(e.relatedTarget).closest('form');
76+
77+
var data = form.serializeArray().filter(function (index) {
78+
return $.inArray(index.name, <?php echo json_encode($field['on_the_fly']['serialize'] ?? []); ?>) >= 0;
79+
});
80+
81+
$(this).find('.modal-content').load(loadurl + '&' + $.param(data));
7682
});
7783
7884
// trigger select2 for each untriggered select2 box

resources/views/modal/create.blade.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
<h3 class="box-title">{{ trans('backpack::crud.add_a_new') }} {{ $crud->entity_name }}</h3>
55
@endsection
66

7+
@php
8+
$requestData = [];
9+
10+
foreach ($request->input() as $key => $item) {
11+
$requestData[] = [
12+
'name' => $key,
13+
'value' => $item,
14+
];
15+
}
16+
@endphp
17+
718
@section('content')
819
<div class="row">
920
<div class="col-md-10 col-md-offset-1">
@@ -28,25 +39,32 @@
2839
$("#create_{{ $entity }}").submit(function (e) {
2940
e.preventDefault(); // avoid to execute the actual submit of the form.
3041
42+
var requestData = <?php echo json_encode($requestData); ?>;
43+
3144
$.ajax({
3245
type: "PUT",
33-
url: "/{{ ltrim($crud->route . '/ajax', '/') }}",
34-
data: $("#create_{{ $entity }}").serialize(), // serializes the form's elements.
35-
success: function (data) {
36-
new PNotify({
37-
type: "success",
38-
title: "{{ trans('backpack::base.success') }}",
39-
text: "{{ trans('backpack::crud.insert_success') }}"
40-
});
46+
url: "/{{ ltrim($crud->route . '/ajax', '/') }}",
47+
data
48+
:
49+
$("#create_{{ $entity }}").serialize() + '&' + $.param(requestData), // serializes the form's elements.
50+
success
51+
:
52+
53+
function (data) {
54+
new PNotify({
55+
type: "success",
56+
title: "{{ trans('backpack::base.success') }}",
57+
text: "{{ trans('backpack::crud.insert_success') }}"
58+
});
4159
42-
$("#{{ $entity }}_create_modal").modal('hide');
60+
$("#{{ $entity }}_create_modal").modal('hide');
4361
44-
// provide auto-fill
62+
// provide auto-fill
4563
46-
if ($("#select2_ajax_{{ $field_name }}").length) {
47-
searchfield = $("#select2_ajax_{{ $field_name }}")
64+
if ($("#select2_ajax_{{ $request->input('field_name') }}").length) {
65+
searchfield = $("#select2_ajax_{{ $request->input('field_name') }}")
4866
} else {
49-
searchfield = $("#select2_ajax_multiple_{{ $field_name }}")
67+
searchfield = $("#select2_ajax_multiple_{{ $request->input('field_name') }}")
5068
}
5169
5270
searchfield.select2('open');
@@ -55,7 +73,7 @@
5573
// Dropdown = single, Selection = multiple
5674
var search = searchfield.data('select2').dropdown.$search || searchfield.data('select2').selection.$search;
5775
// This is undocumented and may change in the future
58-
var userInput = $("#create_{{ $entity }} [name='{{ $attribute }}']").serializeArray();
76+
var userInput = $("#create_{{ $entity }} [name='{{ $request->input('attribute') }}']").serializeArray();
5977
6078
search.val(userInput[0]['value']);
6179
search.trigger('input');

resources/views/modal/delete.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
{{ trans('backpack::crud.delete_confirm') }}<br><br>
1313

14-
{{ $entry->{$attribute} }}
14+
{{ $entry->{$request->input('attribute')} }}
1515

1616
</div>
1717
</div>
@@ -39,7 +39,7 @@
3939
$("#{{ $entity }}_delete_modal").modal('hide');
4040
4141
// Clear select
42-
$("#select2_ajax_{{ $field_name }}").val(null).trigger('change');
42+
$("#select2_ajax_{{ $request->input('field_name') }}").val(null).trigger('change');
4343
},
4444
error: function (data) {
4545
new PNotify({

resources/views/modal/edit.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
4343
// provide auto-fill
4444
45-
if ($("#select2_ajax_{{ $field_name }}").length) {
46-
searchfield = $("#select2_ajax_{{ $field_name }}")
45+
if ($("#select2_ajax_{{ $request->input('field_name') }}").length) {
46+
searchfield = $("#select2_ajax_{{ $request->input('field_name') }}")
4747
} else {
48-
searchfield = $("#select2_ajax_multiple_{{ $field_name }}")
48+
searchfield = $("#select2_ajax_multiple_{{ $request->input('field_name') }}")
4949
}
5050
5151
searchfield.val(null).trigger('change');
@@ -55,7 +55,7 @@
5555
// Dropdown = single, Selection = multiple
5656
var search = searchfield.data('select2').dropdown.$search || searchfield.data('select2').selection.$search;
5757
// This is undocumented and may change in the future
58-
var userInput = $("#edit_{{ $entity }} [name='{{ $attribute }}']").serializeArray();
58+
var userInput = $("#edit_{{ $entity }} [name='{{ $request->input('attribute') }}']").serializeArray();
5959
6060
search.val(userInput[0]['value']);
6161
search.trigger('input');

0 commit comments

Comments
 (0)