I have an API which is return about over 10.000 records it's implemented to return in pagination, each time it return 25 records.
Now, I want to populate all records to select input on HTML page, currently I call ajax to API and parse JSON object to append options. But because the API is returning in pages, I don't know how to query all the records to display all item to user, for now I can only display 25 items.
My idea is to catch the event when user scroll to the end of the list and then fire a API call again for next page, continue populate data to the dropdown dynamically until it reach the last page. But I don't know how to do that, can you guys help me?
This is my code.
HTML
<div class="col-md-6 target-geo-holder">
<select class="form-control target-geo">
</select>
</div>
JS
var url = "API Link";
var list = [];
$.get(url, function (data) {
if (data.success) {
list = data.records;
} else {
console.log("No data available");
}
});
var targetGeo = fragment.find(".target-geo");
targetGeo.empty();
targetGeo.append($("<option />").attr("value", "matchAll").text("None"));
list.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
for (var i = 0; i < list.length; i++) {
var item = list[i];
var option = $("<option>").attr("value", item.id).text(item.name);
targetGeo.append(option);
}