I am using an api which has names of exercises in it. I want to make a search box with an autocomplete drop down (like suggetions google gives you before finish up what you type) But i want to use the api as results.
I managed to get the readings out of the api.
let data;
async function getExercises () {
let url = 'https://wger.de/api/v2/exercise/?format=json'
while (url) {
const res = await fetch(url)
data = await res.json()
for (const item of data.results) {
console.log(item.name)
}
url = data.next
}
$(document).ready(function() {
BindControls();
});
function BindControls() {
$('#exercise-search').autocomplete({
source: data,
minLength: 0,
scroll: true
}).focus(function() {
$(this).autocomplete("search", "");
});
}
}
I am trying to make the drop down using the api results but cant get it to work.
<input id="exercise-search" class="form-control" type="text" name="data">
p, div, input {
font: 16px Calibri;
}
.ui-autocomplete {
cursor:pointer;
height:120px;
overflow-y:scroll;
}
these are the libraries i imported:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
the console log in my browser doesnt seem to have any errors.
Any insight would be appreciated
Try something like this https://jsfiddle.net/voaf1sLg/ .
What it boils down to is that you do not store those search results anywhere . I've modified the code for your async function to return the full array with all results (after all those 33 API calls, eh!), then return a fulfilled promise with said auto-complete entries. Modify your code accordingly.
async function getEx() {
let url = 'https://wger.de/api/v2/exercise/?format=json'
const array = [];
while (url) {
const res = await fetch(url)
data = await res.json()
for (const item of data.results) {
console.log(item.name)
array.push(item.name);
}
url = data.next
}
return array;
}
$(function() {
let tags = [];
getEx().then(res => {
$( "#tags" ).autocomplete({
source: res
});
});
} );
I can see your total result count is 685,its better if we can full those records in one shot. but if its not possible then i just modify your code accordingly using recurssion.
var sourcearray = []
var getData = function(url) {
$.getJSON(url, function(d) {
Array.prototype.push.apply(sourcearray, d.results);
if (d.next != null) {
getData(d.next);
} else {
console.log(sourcearray)
var config={
minLength: 1,
source: sourcearray,
focus: function(event, ui) {
$("#suggest").val(ui.item.license_author);
return false;
},
select: function(event, ui) {
$("#suggest").val(ui.item.license_author);
return false;
}
};
$("#suggest").autocomplete(config).autocomplete("instance")._renderItem = function(ul, item) {
return $("<li>").append("<div>" + item.license_author + "<br>" + item.description + "</div>").appendTo(ul);
};
}
})
}
$(function() {
getData("https://wger.de/api/v2/exercise/?format=json")
});
here is working fiddle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.