Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
<script type="text/javascript">
    var $ = jQuery.noConflict();
    var records = [];
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $(document).ready(function () {
        jQuery.ajax({
            url: '@Url.Action("GetAllCity", "Home")',
            type: "GET",
            success: function (data) {
                debugger;
                if (data != null) {
                    for (var k in data) {
                        records.push(data[k].Name);
                    }
                }
            }
        });

        jQuery('#txtCityName').autocomplete({
            source: records
        });
    });
</script>


autocomplete searches from availableTags but not searches from records array list.After array creation both looks like same array but autocomplete is not working on records array.
Posted
Updated 18-Sep-15 3:31am
v6
Comments
Krunal Rohit 18-Sep-15 8:56am    
your records[] array contain any data after the AJAX call ?

-KR
itsathere 18-Sep-15 9:08am    
after json result i have checked arrylist on mouse over on records[], it shows array like availableTags but autopopulate not working on records[].
Krunal Rohit 18-Sep-15 9:12am    
That's what I'm asking, are you getting any record using AJAX call ?

-KR
itsathere 18-Sep-15 9:13am    
Yes.I have created a alert message after for loop; alert also showing data but autopopulate..
Krunal Rohit 18-Sep-15 9:33am    
Call your autocomplete function in success with data.

-KR

1 solution

C#
<script type="text/javascript">
    var $ = jQuery.noConflict();
    var source= [];
    $(document).ready(function () {
        jQuery.ajax({
            url: '@Url.Action("GetAllCity", "Home")',
            type: "GET",
            success: function (data) {
                debugger;
                if (data != null) {
                    for (var k in data) {
                        records.push(data[k].Name);
                    }
             jQuery('#txtCityName').autocomplete({
              source: source
source: function (request, response) {
                            var term = jQuery.ui.autocomplete.escapeRegex(request.term)
                                , startsWithMatcher = new RegExp("^" + term, "i")
                                , startsWith = jQuery.grep(source, function (value) {
                                    return startsWithMatcher.test(value.label || value.value || value);
                                })
                                , containsMatcher = new RegExp(term, "i")
                                , contains = jQuery.grep(source, function (value) {
                                    return jQuery.inArray(value, startsWith) < 0 &&
                                        containsMatcher.test(value.label || value.value || value);
                                });

                            response(startsWith.concat(contains));
                        }
              });
                }
            }
        });
 
        
    });
</script>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900