samedi 18 juin 2016

Autocomplete jquery function doesn't find some values from the source on MVC .NET project

I use autocomplete jquery function to search for items in a textfield of a form on my MVC project. When at least there is one letter on the textfield, it searches the items related to that letter.

The problem is that when I put the first letter of one value from my source the autocomplete jquery function doesn't find it, it throws "there is no results" but it should throw some items because those items with that initial letter exists.

I'm sure the code is working correctly because it works for a determined group of items of my source, I mean when I want to search an item which starts with the letter "a" the autocomplete function throws all the items which has the "a" letter as the first letter but when I search by the word "e" the autocomplete function throws me no results found but It has to throw all the item starting with the "e" letter because those items with that "e" as first letter exists.

The source is a table located in my azure database (the autocomplete function is on my web app deployed on azure) and I get all the data from this table by a function on my controller script. I decided to use autocomplete function because it seems to me suitable when you have to find an item from a large amount of items. The table has roughly 100 000 items or more. One hypothesis that I have is that the autocomplete function can't search all of this 100 000 items (more than 100 000 actually) and it just could find some items, but I think it's not logical or maybe yes, maybe for that amount of items I should use another search engine or something like that. Any Idea, suggestion or answer will be well appreciated.

Thanks in advance.

Here is my autocomplete function code

<script type="text/javascript">
        $(function () {
            $("#alergiaSelected").autocomplete({
                source: '@Url.Action("AutocompleteSuggestionsForName")',
                focus: function (event, ui) {
                    $("#alergiaSelected").val(ui.item.NOMBRE_MEDICAMENTO);
                    return false;
                },
                select: function (event, ui) {
                    $("#ID_FARM_MEDICAMENTO").val(ui.item.ID_FARM_MEDICAMENTO);
                    $("#alergiaSelected").val(ui.item.NOMBRE_MEDICAMENTO);
                    return false;
                }
            }).data("ui-autocomplete")._renderItem = function (ul, item) {
                return $("<li>")
                .append("<a>" + item.NOMBRE_MEDICAMENTO + "<br>" + item.PRESENTACION + "</a>")
                .appendTo(ul);
            };
        });
    </script>

and the textfield is

            @Html.EditorFor(model => model.alergiaSelected, new { @class = "text-danger form-control autocomplete" } )

The function that the source parameter in the autocomplete function calls is this:

public JsonResult AutocompleteSuggestionsForName (string term="")
    {
        IEnumerable<CATALOGO> cat = db.CATALOGOes.Where(n => n.NOMBRE_MEDICAMENTO.ToUpper().StartsWith(term.ToUpper()));

        return Json(cat, JsonRequestBehavior.AllowGet);
    }

Aucun commentaire:

Enregistrer un commentaire