Skip to content

Commit 81a0081

Browse files
authored
Merge pull request #36 from webfactor/multiple_attributes
Multiple attributes
2 parents 4c53deb + 79de4c8 commit 81a0081

File tree

12 files changed

+167
-231
lines changed

12 files changed

+167
-231
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
.idea/

CHANGELOG.md

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

24+
## 3.0.0 - 2019-07-16 (breaking change, depending on your usage
25+
26+
### Added
27+
- `autofill_attributes`
28+
29+
### Removed
30+
- `attribute` in favor of `autofill_attributes`
31+
32+
### Changed
33+
- blade refactoring
34+
2435
## 2.3.0 - 2019-03-23
2536

2637
### Added

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,39 @@ class EntityCrudController extends CrudController
245245
```
246246
### Auto-fill after store/update
247247

248-
Instant Fields will try to auto-fill the select2 input after creating a new entry. It will assume that an input field exists with the name `name` and will use its value for the triggered ajax search. If you want to use another field for this, just add `attribute` to the `on_the_fly`-array containing the field name you want to use:
248+
Instant Fields will try to auto-fill the select2 input after creating a new entry.
249+
It will assume that an input field exists with the name `name` and will use its value for the triggered ajax search.
250+
251+
You can add an `autofill_attributes` array, if you want to define one or multiple auto-fill columns:
249252

250253
```
251254
'on_the_fly' => [
252255
'entity' => 'entity',
253-
'attribute' => 'company'
256+
'autofill_attributes' => [
257+
'company',
258+
'contact_name',
259+
'address',
260+
]
254261
]
255262
```
263+
Please note that this only works with columns (or appended accessors).
264+
265+
In order for this to work properly on multiple columns, you should implement a custom search logic for the field.
266+
267+
Search logic example:
268+
```
269+
'search_logic' => function ($query, $searchTerms) {
270+
return $query->where(function ($q) use ($searchTerms) {
271+
collect(explode(' ', $searchTerms))->each(function ($searchTerm) use ($q) {
272+
$q->where(function ($sq) use ($searchTerm) {
273+
$sq->where('company', 'LIKE', '%' . $searchTerm . '%')
274+
->orWhere('contact_name', 'LIKE', '%' . $searchTerm . '%')
275+
->orWhereRaw('LOWER(JSON_EXTRACT(address, "$.postcode")) LIKE \'%' . strtolower($searchTerm) . '%\'');
276+
});
277+
});
278+
})->orderBy('name');
279+
```
280+
256281

257282
### Passing current field values to foreign `EntityCrudController`
258283

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
@php($autofill_attributes = json_encode($field['on_the_fly']['autofill_attributes'] ?? ['name']))
12
<span class="input-group-btn">
23
<button
3-
href="#"
4-
type="button"
5-
class="btn btn-primary"
6-
style="border-radius: 0px"
7-
data-toggle="modal"
8-
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').'&create_modal_view='.($field['on_the_fly']['create_modal_view'] ?? 'webfactor::modal.create').'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
4+
href="#"
5+
type="button"
6+
class="btn btn-primary"
7+
style="border-radius: 0px"
8+
data-toggle="modal"
9+
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal"
10+
data-load-url="{{ $field['on_the_fly']['create_modal'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create?field_name='.$field['name'].'&create_modal_view='.($field['on_the_fly']['create_modal_view'] ?? 'webfactor::modal.create').'&autofill_attributes='.$autofill_attributes }}">
1011
<i class="fa fa-plus"></i>
1112
</button>
1213
</span>

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
@php($autofill_attributes = json_encode($field['on_the_fly']['autofill_attributes'] ?? ['name']))
12
<span class="input-group-btn">
23
<button
3-
href="#"
4-
type="button"
5-
class="btn btn-warning {{ isset($field['value']) ?: 'disabled'}}"
6-
style="border-radius: 0px"
7-
data-toggle="modal"
8-
data-id="{{ $field['value'] ?? '' }}"
9-
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'].'&edit_modal_view='.($field['on_the_fly']['edit_modal_view'] ?? 'webfactor::modal.edit').'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
4+
href="#"
5+
type="button"
6+
class="btn btn-warning {{ isset($field['value']) ?: 'disabled'}}"
7+
style="border-radius: 0px"
8+
data-toggle="modal"
9+
data-id="{{ $field['value'] ?? '' }}"
10+
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_edit_modal"
11+
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').'&autofill_attributes='. $autofill_attributes }}">
1112
<i class="fa fa-edit"></i>
1213
</button>
1314
</span>
Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1 @@
1-
@extends('webfactor::modal.create_layout')
2-
3-
@section('header')
4-
<h3 class="box-title">{{ trans('backpack::crud.add_a_new') }} {{ $crud->entity_name }}</h3>
5-
@endsection
6-
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-
18-
@section('content')
19-
<div class="row">
20-
<div class="col-md-10 col-md-offset-1">
21-
@include('crud::inc.grouped_errors')
22-
23-
<!-- load the view from the application if it exists, otherwise load the one in the package -->
24-
@if(view()->exists('vendor.backpack.crud.form_content'))
25-
@include('vendor.backpack.crud.form_content', ['fields' => $fields])
26-
@else
27-
@include('crud::form_content', ['fields' => $fields])
28-
@endif
29-
</div>
30-
</div>
31-
@endsection
32-
33-
@section('footer')
34-
@include('webfactor::modal.inc.create_form_save_buttons')
35-
@endsection
36-
37-
@push('crud_fields_scripts')
38-
<script>
39-
$("#create_{{ $entity }}").submit(function (e) {
40-
e.preventDefault(); // avoid to execute the actual submit of the form.
41-
42-
var requestData = <?php echo json_encode($requestData); ?>;
43-
44-
$.ajax({
45-
type: "PUT",
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-
});
59-
60-
$("#{{ $entity }}_create_modal").modal('hide');
61-
62-
// provide auto-fill
63-
64-
if ($("#select2_ajax_{{ $request->input('field_name') }}").length) {
65-
searchfield = $("#select2_ajax_{{ $request->input('field_name') }}")
66-
} else {
67-
searchfield = $("#select2_ajax_multiple_{{ $request->input('field_name') }}")
68-
}
69-
70-
searchfield.select2('open');
71-
72-
// Get the search box within the dropdown or the selection
73-
// Dropdown = single, Selection = multiple
74-
var search = searchfield.data('select2').dropdown.$search || searchfield.data('select2').selection.$search;
75-
// This is undocumented and may change in the future
76-
var userInput = $("#create_{{ $entity }} [name='{{ $request->input('attribute') }}']").serializeArray();
77-
78-
search.val(userInput[0]['value']);
79-
search.trigger('input');
80-
setTimeout(function () {
81-
$('.select2-results__option').trigger("mouseup");
82-
}, 200);
83-
},
84-
error: function (data) {
85-
new PNotify({
86-
type: "error",
87-
title: "{{ trans('backpack::base.error') }}",
88-
text: "{{ trans('backpack::base.error') }}: " + data.responseJSON
89-
});
90-
}
91-
});
92-
});
93-
</script>
94-
95-
@endpush
1+
@extends('webfactor::modal.save', ['action' => 'create'])

resources/views/modal/delete.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@extends('webfactor::modal.delete_layout')
1+
@extends('webfactor::modal.layout', ['action' => $delete ])
22

33
@section('header')
44
<h3 class="box-title">{{ trans('backpack::crud.delete') }} {{ $crud->entity_name }}</h3>

resources/views/modal/delete_layout.blade.php

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,2 @@
1-
@extends('webfactor::modal.edit_layout')
1+
@extends('webfactor::modal.save', ['action' => 'edit'])
22

3-
@section('header')
4-
<h3 class="box-title">{{ trans('backpack::crud.edit') }} {{ $crud->entity_name }}</h3>
5-
@endsection
6-
7-
@section('content')
8-
<div class="row">
9-
<div class="col-md-10 col-md-offset-1">
10-
@include('crud::inc.grouped_errors')
11-
12-
<!-- load the view from the application if it exists, otherwise load the one in the package -->
13-
@if(view()->exists('vendor.backpack.crud.form_content'))
14-
@include('vendor.backpack.crud.form_content', ['fields' => $fields, 'action' => 'edit'])
15-
@else
16-
@include('crud::form_content', ['fields' => $fields, 'action' => 'edit'])
17-
@endif
18-
</div>
19-
</div>
20-
@endsection
21-
22-
@section('footer')
23-
@include('webfactor::modal.inc.edit_form_save_buttons')
24-
@endsection
25-
26-
@push('crud_fields_scripts')
27-
<script>
28-
$("#edit_{{ $entity }}").submit(function (e) {
29-
30-
$.ajax({
31-
type: "PATCH",
32-
url: "/{{ ltrim($crud->route . '/ajax', '/') }}",
33-
data: $("#edit_{{ $entity }}").serialize(), // serializes the form's elements.
34-
success: function (data) {
35-
new PNotify({
36-
type: "success",
37-
title: "{{ trans('backpack::base.success') }}",
38-
text: "{{ trans('backpack::crud.update_success') }}"
39-
});
40-
41-
$("#{{ $entity }}_edit_modal").modal('hide');
42-
43-
// provide auto-fill
44-
45-
if ($("#select2_ajax_{{ $request->input('field_name') }}").length) {
46-
searchfield = $("#select2_ajax_{{ $request->input('field_name') }}")
47-
} else {
48-
searchfield = $("#select2_ajax_multiple_{{ $request->input('field_name') }}")
49-
}
50-
51-
searchfield.val(null).trigger('change');
52-
searchfield.select2('open');
53-
54-
// Get the search box within the dropdown or the selection
55-
// Dropdown = single, Selection = multiple
56-
var search = searchfield.data('select2').dropdown.$search || searchfield.data('select2').selection.$search;
57-
// This is undocumented and may change in the future
58-
var userInput = $("#edit_{{ $entity }} [name='{{ $request->input('attribute') }}']").serializeArray();
59-
60-
search.val(userInput[0]['value']);
61-
search.trigger('input');
62-
setTimeout(function () {
63-
$('.select2-results__option').trigger("mouseup");
64-
}, 200);
65-
},
66-
error: function (data) {
67-
new PNotify({
68-
type: "error",
69-
title: "{{ trans('backpack::base.error') }}",
70-
text: "{{ trans('backpack::base.error') }}: " + data.responseJSON
71-
});
72-
}
73-
});
74-
75-
e.preventDefault(); // avoid to execute the actual submit of the form.
76-
});
77-
</script>
78-
79-
@endpush

resources/views/modal/edit_layout.blade.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)