I am using ajax for live searching, but the problem is that It is shown only one result
when I am using .html() but when I am using append() it works but every word i write it to duplicate the results,
here is my code:
in controller,
JavaScript
x
7
1
$patient = Patient::select('id', 'avatar')
2
->where('phone_number', 'like', '%' . $search_query . '%')
3
->orWhere('first_name', 'like', '%' . $search_query . '%')
4
->limit(15)
5
->get();
6
return $patient;
7
ajax code in blade
JavaScript
1
33
33
1
$("#search-eng").keyup(function() {
2
let search_query = $(this).val();
3
if (search_query != "") {
4
$.ajax({
5
url: '{{ url('/appointment/calander_patient_search') }}/' +
6
search_query,
7
type: "GET",
8
dataType: "json",
9
success: function(data) {
10
$("#search-eng-show-list").show();
11
12
if (data !== "") {
13
14
$.each(data, function(key, value) {
15
16
$('#search-eng-show-list').html(
17
18
'<a data-id="' + value.id + '"' value.second_name + '</a>');
19
});
20
}
21
if (data == "") {
22
$('#search-eng-show-list').html(
23
'<a><i "></i>No Record</a>'
24
);
25
}
26
},
27
});
28
} else {
29
$("#search-eng-show-list").empty();
30
$("#search-eng-show-list").hide();;
31
}
32
});
33
Advertisement
Answer
Yes you set your content in your loop statement, so it will only take the last content.
You can use some buffer variable :
JavaScript
1
11
11
1
if (data !== "") {
2
var html = '';
3
4
$.each(data, function(key, value) {
5
html += '<a data-id="' + value.id + '"' value.second_name + '</a>');
6
}
7
8
$('#search-eng-show-list').html(html);
9
10
// ....
11