|
View:
<div>
<input type="text" id="myautocomplete" />
</div>
<script>
$(function () {
$("#myautocomplete").ejAutocomplete({
showPopupButton: true,
showResetIcon: true,
showEmptyResultText: false, // set this property to show no suggestions
width: "50%",
});
$("span .e-search").click(function () { //bind click event for the search icon
dosearching();
});
$("#myautocomplete").keypress(function (e) { //bind Enter keypress event
if (e.which == 13) { //Enter key pressed
alert('You pressed enter!');
dosearching();
}
});
})
function dosearching() { // custom JavaScript function
var strfilter = $('#myautocomplete').val();
$.ajax //post the searched word through AJAX to find the result
({
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ strFilter: strfilter }),
url: "/Autocomplete/GetCARS",
success: function (response) {
var ac = $("#myautocomplete").ejAutocomplete("instance");
if (response != null && response.length) {
ac.suggestionListItems = response;
ac._doneRemaining();
} else
ac._hideResult();
}
})
}
</script>
Controller:
public JsonResult GetCARS(string strFilter)
{
if (strFilter != "")
{
var Data = setListSource() ;
var search = from n in Data where n.text.ToLower().Contains(strFilter.ToLower()) select n; // Filtering based on the searched word
return Json(search, JsonRequestBehavior.AllowGet);
}
else
{
return null;
}
}
|