I am new to javascript and jquery. I am trying to create an autocomplete jqueryui with model data as its source. I am stuck at what is the best way of how to do this. I have tried to initialise the data in document.ready like this:
var listAddress = [];
foreach (var item in Model.allBuildings)
{
//adding into address array all addresses for label and its id.
@: listAddress.push({ label: "@Html.Raw(item.Address)", id: "@item.ApartmentBlockID" });*@
}
The autocomplete works, but I keep getting messages from developer tools that
Violation] 'setTimeout' handler took 113ms
My question is what is a better way to use model data as source for autocomplete? My biggest puzzle is that I have not set anywhere a settimeout function! The error is pointing to the settimeout function in the jqueryui script??
Update: This is my view
// first autocomplete
<div class="col-md-10">
@Html.HiddenFor(model => model.renovationDetail.ApartmentBlockID, new { @id = "hidden_apartblockID" })
@Html.EditorFor(model => model.BuildingID, new { htmlAttributes = new { @class = "form-control", @id = "show_buildingID" } })
@Html.ValidationMessageFor(model => model.renovationDetail.ApartmentBlockID, "", new { @class = "text-danger" })
</div>
</div>
//second autocomplete
<div class="form-group">
@Html.LabelFor(model => model.allBuildings.First().Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control", @id = "show_address" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div
This is my javascript for address autocomplete(I do the same for the other one):
////function to load building addresses when page loads.
function ChangeAddressForSelect() {
//creating autocomplete for address
$('#show_address')
.blur(
function () {
var keyEvent = $.Event("keydown");
keyEvent.keyCode = $.ui.keyCode.ENTER;
$(this).trigger(keyEvent);
// })
.autocomplete({
//source: '/Renovations/GetAddressForEdit',
source: function (request, response) {
response($.ui.autocomplete.filter(listAddress,
request.term));
},
minLength: 0,
scroll: true,
select: function (event, ui) {
//set tagids to save
//$("#hidden_apartblockID").val(ui.item.id);
//// Tags for display
//this.value = ui.item.value;
return false;
},
focus: function () { $(this).autocomplete("search"); return false; },
.blur(function () {
//$(this).autocomplete('enable');
});
What is the most effective way to use model data as a source for each autcomplete? Should I switch to ajax or will ajax sourced data slow the page load?