Skip to content

Commit 511caeb

Browse files
authored
Merge pull request #26 from webfactor/2.0
2.0
2 parents afcb67e + b64cd1f commit 511caeb

14 files changed

+463
-94
lines changed

CHANGELOG.md

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

24+
## 2.0.0 - 2019-02-08
25+
26+
### Added
27+
- new layout: inline!
28+
- edit button!
29+
- support for dependencies
30+
- use all form values in custom search logic
31+
- adjust pagination (default: 10)
32+
- autofill select on creating an entity
33+
34+
If your are upgrading from version 1.x please just update your routes to use "any" method (see readme)
35+
36+
2437
## 1.4.0 - 2018-11-11
2538

2639
### Added

README.md

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Quality Score][ico-code-quality]][link-code-quality]
99
[![Total Downloads][ico-downloads]][link-downloads]
1010

11-
This is a package for [Laravel Backpack](https://laravel-backpack.readme.io/docs) and provides CRUD field types which allow to create a related CRUD entity on-the-fly while adding/editing another.
11+
This is a package for [Laravel Backpack](https://laravel-backpack.readme.io/docs) and provides CRUD field types which allow to create and edit a related CRUD entity on-the-fly while adding/editing another.
1212

1313
![Screenshot](https://webfactor.de/files/modal_1.png)
1414

@@ -51,15 +51,16 @@ in your routes file you have to add one additional route in your `CRUD::resource
5151
<?php
5252

5353
CRUD::resource('entity', 'EntityCrudController')->with(function () {
54-
Route::match(['get', 'post'],'entity/ajax/{create?}', 'EntityCrudController@handleAjaxRequest');
54+
Route::any('entity/ajax/{mode?}', 'EntityCrudController@handleAjaxRequest');
5555
});
5656
```
5757

58-
The trait/route will handle three situations for you:
58+
The trait/route will handle the following requests for you:
5959

6060
- search on triggered entity
61-
- retrieve the HTML for the modal
61+
- retrieve the HTML for the create and edit modal
6262
- store entity from modal
63+
- update entity from modal
6364

6465
### Available Fields
6566

@@ -68,15 +69,35 @@ There are two field types available in this package which allow you an instant c
6869
- [select2_from_ajax](https://laravel-backpack.readme.io/docs/crud-fields#section-select2_from_ajax)
6970
- [select2_from_ajax_multiple](https://laravel-backpack.readme.io/docs/crud-fields#section-select2_from_ajax_multiple)
7071

71-
To use instant creation capability of these field types you have to add the `on-the-fly` key and set a name for the entity.
72+
For `select2_from_ajax` also "edit" is available.
7273

74+
### Field Definition
75+
76+
Please set the `$ajaxEntity` property by using the setter in the `setup()`-method of the (foreign) EntityCrudController that is **triggered** by an "instant field":
77+
78+
```php
79+
<?php
80+
81+
use Webfactor\Laravel\Backpack\InstantFields\InstantFields;
82+
83+
class EntityCrudController extends CrudController
84+
{
85+
use InstantFields;
86+
87+
public function setup()
88+
{
89+
// other Backpack options
90+
91+
$this->setAjaxEntity('entity');
92+
93+
// fields/columns definitions
94+
}
95+
}
7396
```
74-
'on_the_fly' => [
75-
'entity' => 'entity' // e.g. user, contact, company, job etc...
76-
]
77-
```
97+
98+
In the field definition of the `EntityCrudController` where your instant field is setup you will have to set the above name in the `on_the_fly`-Array.
7899

79-
If you use Laravel Backpack Crud >=3.4.11 you don't have to publish the provided fields, you can use them directly from the package by using the `view_namespace` key.
100+
> Note: If you use Laravel Backpack Crud >=3.4.11 you don't have to publish the provided fields, you can use them directly from the package by using the `view_namespace` key.
80101
81102
Example:
82103

@@ -90,35 +111,50 @@ Example:
90111
'entity' => 'entity',
91112
'attribute' => 'name',
92113
'placeholder' => 'Choose',
114+
'pagination' => 20, // optional, default: 10
93115
'minimum_input_length' => 0,
94116
'on_the_fly' => [
95-
'entity' => 'entity',
117+
'entity' => 'entity', // e. g. user, contact, company etc...
118+
119+
// optional:
120+
121+
'create' => false
122+
'edit' => false
123+
'create_modal' => 'path to custom create modal'
124+
'edit_modal' => 'path to custom edit modal'
125+
'attribute' => '...' // see auto-fill below in readme
96126
],
127+
'dependencies' => ['field1', 'field2'...], // optional, resets this field when changing the given ones
97128
],
98129
```
99130

100-
## Multiple instant fields
131+
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.
132+
133+
## List view
101134

102-
If you want to use more than one instant field in a CrudController you have to set the `$ajaxEntity` property by using the setter in the `setup()`-method of the EntityCrudController that is triggered by an "instant field". This has to be the same name as in the field definition:
135+
With this package your are also able to add a create button for the foreign CRUD entity in your list view of Backpack! Just add the following line in your `EntityCrudController`:
103136

104137
```php
105138
<?php
106139

107-
use Webfactor\Laravel\Backpack\InstantFields\InstantFields;
108-
109-
class EntityCrudController extends CrudController
110-
{
111-
use InstantFields;
112-
113-
public function setup()
114-
{
115-
// other Backpack options
140+
$this->addInstantCreateButtonToList(
141+
$entity, // foreign entity
142+
$content, // content of the button
143+
$entity_id, // the name of the ID of the current entity will be forwarded by this
144+
$class, // optional, default: 'btn btn-xs btn-default', the css class of the button
145+
$position, // optional, default: beginning, the position of the button in the line
146+
$button_view // optional, you can override the used button blade by your own
147+
);
116148

117-
$this->setAjaxEntity('entity');
118-
119-
// fields/columns definitions
120-
}
121-
}
149+
// Example:
150+
151+
$this->addInstantCreateButtonToList(
152+
'order',
153+
'<i class="fa fa-cart-plus"></i>',
154+
'task_id',
155+
'btn btn-sm btn-info',
156+
'end'
157+
);
122158
```
123159

124160
## Customization
@@ -143,22 +179,24 @@ The "instant field" triggers the `ajaxIndex()` of the `EntityCrudController` whe
143179
By adding `search_logic` to the field defintion you can implement your own searching behavior:
144180

145181
```
146-
'search_logic' => function($query, $searchTerm) {
182+
'search_logic' => function($query, $searchTerm, $form) { // Collection $form is optional
147183
return $query->where('name', 'like', '%'.$searchTerm.'%')
148184
->whereActive()
149185
->whereSomethingElse();
150186
},
151187
```
152188

189+
`Collecion $form` is an optional parameter and provides all current values of your CRUD form. You can use it to manipulate your search depending on actual inputs and in combination with `dependencies` (see [Backpack Documentation](https://backpackforlaravel.com/docs/3.5/crud-fields#select2_from_ajax))
190+
153191
Furthermore you can then use `attibute` to display enriched values in the dropdown by using an accessor on the model.
154192

155193
### Search data source
156194

157-
If needed you are free to use the `data_source` attribute from the original field blades which come with Laravel Backpack. This is the URL that is triggered by the select2 field for searching.
195+
If needed you are free to use the `data_source` and `method` attributes from the original field blades which come with Laravel Backpack. This is the URL that is triggered by the select2 field for searching.
158196

159197
### Request validation
160198

161-
You can also use Request Validation! Just set the `$ajaxStoreRequest` property by using the provided setter method:
199+
You can also use Request Validation! Just set the `$ajaxStoreRequest` and/or `$ajaxUpdateRequest` property by using the provided setter method:
162200

163201
```php
164202
<?php
@@ -175,11 +213,22 @@ class EntityCrudController extends CrudController
175213

176214
$this->setAjaxEntity('entity');
177215
$this->setAjaxStoreRequest(\RequestNamespace\StoreRequest::class);
216+
$this->setAjaxUpdateRequest(\RequestNamespace\UpdateRequest::class);
178217

179218
// fields/columns definitions
180219
}
181220
}
182221
```
222+
### Auto-fill after store/update
223+
224+
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:
225+
226+
```
227+
'on_the_fly' => [
228+
'entity' => 'entity',
229+
'attribute' => 'company'
230+
]
231+
```
183232

184233
### Fields
185234

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
<button href="{{ $field['on_the_fly']['create_view'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create' }}"
1+
<span class="input-group-btn">
2+
<button
3+
href="#"
24
type="button"
35
class="btn btn-primary"
6+
style="border-radius: 0px"
47
data-toggle="modal"
5-
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal">
6-
{{ trans('backpack::crud.add') }}
7-
</button>
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') }}">
10+
<i class="fa fa-plus"></i>
11+
</button>
12+
</span>
813
<div class="modal fade"
9-
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
14+
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal"
1015
tabindex="-1"
1116
role="dialog"
12-
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
17+
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal"
1318
aria-hidden="true">
1419
<div class="modal-dialog" role="document">
1520
<div class="modal-content"></div>
1621
</div>
1722
</div>
23+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<span class="input-group-btn">
2+
<button
3+
href="#"
4+
type="button"
5+
class="btn btn-warning"
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'].'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}">
11+
<i class="fa fa-pencil"></i>
12+
</button>
13+
</span>
14+
<div class="modal fade"
15+
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_edit_modal"
16+
tabindex="-1"
17+
role="dialog"
18+
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_edit_modal"
19+
aria-hidden="true">
20+
<div class="modal-dialog" role="document">
21+
<div class="modal-content"></div>
22+
</div>
23+
</div>
24+

0 commit comments

Comments
 (0)