Skip to content

Commit 09f7029

Browse files
authored
Merge pull request #2 from webfactor/1.0
[EH] implement search logic
2 parents 2bce817 + d106296 commit 09f7029

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

resources/views/fields/select2_from_ajax.blade.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ class="btn btn-primary"
6363
</div>
6464

6565

66-
67-
6866
{{-- ########################################## --}}
6967
{{-- Extra CSS and JS for this particular field --}}
7068
{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}}
@@ -121,6 +119,7 @@ class="btn btn-primary"
121119
data: function (params) {
122120
return {
123121
q: params.term, // search term
122+
searchkey: "{{ $field['attribute'] }}", // search key in database
124123
page: params.page
125124
};
126125
},

src/Traits/CanBeCreatedOnTheFly.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,24 @@ public function getAjaxEntity()
1919
}
2020

2121
/**
22-
* Provides the search algorithm for the select2 field
22+
* Provides the search algorithm for the select2 field. Overwrite it in
23+
* the EntityCrudController if you need some special functionalities
2324
*
2425
* @return mixed
2526
*/
26-
public function ajaxIndex()
27+
public function ajaxIndex(Request $request)
2728
{
28-
// implement search logic
29-
return $this->crud->query->paginate(50);
29+
$search_term = $request->input('q');
30+
$search_key = $request->input('searchkey');
31+
$page = $request->input('page');
32+
33+
if ($search_term) {
34+
$results = $this->crud->query->where($search_key, 'LIKE', '%' . $search_term . '%')->paginate(10);
35+
} else {
36+
$results = $this->crud->query->paginate(10);
37+
}
38+
39+
return $results;
3040
}
3141

3242
/**

0 commit comments

Comments
 (0)