JQuery autocomplete autocomplete

I'm trying to use the jQuery autocomplete function, and after reading a few posts, I still have two questions:

1) I got autocompletion to work with the code below, but I need an array called "data" that needs to be populated from our database. I am trying to use various methods to populate this through AJAX. I tried using $ .get and $ .ajax. What is the correct syntax for this?

2) This array will be large, I will have 60,000 plus values ​​if I just fill the array once. I was wondering if it is possible to execute an AJAX request to populate an array every time a user enters a new letter? Is it better to do this or just fill the array with all the values ​​at once? For the better, which tax system is less?

//This code works
<script type="text/javascript">
  $(document).ready(function(){
  var data = "Facebook Gowalla Foursquare".split(" ");
  $("#search_company").autocomplete(data);
  });
</script>


//display company live search
echo('<form id="form" method="post" action="competitor_unlink.php" onsubmit="return">');
echo('Company: <input id="search_company"/>');
echo('<br/>');
echo('<button type="submit" value="Submit">Submit</button>');
echo('</form>');
+3
source share
4 answers

Take a look at this demo - this is what you want to do (get data using ajax):

http://jqueryui.com/demos/autocomplete/#remote

/ : - (, 50 ), , .

, . :

a String, URL- . , . . - . , , . value-properties, .

String, Autocomplete , URL-, JSON. ( JSONP). "term" URL. , .

, , . :

1) , , , . , " " , "new yo".

2) , . , , (String-Array Object-Array // ). , . . , .

+2

, URL-, JSON jQuery UI.

$("#search_company").autocomplete({
    source: "/Search", // <-- URL of the page you want to do the processing server-side
    minLength: 4 // <-- don't try to run the search until the user enters at least 4 chars
});

URL- querystring "term", . , , .NET, ASP.NET MVC:)

public ActionResult Search(string term) {
    var results = db.Search(term); // <-- this is where you query your DB
    var jqItems = new List<jQueryUIAutoCompleteItem>();
    foreach (var item in results) {
        jqItems.Add(new jQueryUIAutoCompleteItem() {
            value = item.CompanyId.ToString(),
            id = item.CompanyId.ToString(),
            label = item.CompanyName
        });
    }
    return Json(jqItems.ToArray(), JsonRequestBehavior.AllowGet);
}

jQueryUIAutoCompleteItem - , JSON, .

public class jQueryUIAutoCompleteItem {
    public string value { get; set; }
    public string label { get; set; }
    public string id { get; set; }
}
+2

, 60 000 . , Google , .

, , .

( ). , 10 15 . , " ". , ( ) javascript .

+1
source

It may be a little late for this post, but for others who find it. I created a jquery plugin and a jqueryui autocomplete control that works with Foursquare. You can read the message and download the plugin in the Foursquare Autofill Plugin

0
source

Source: https://habr.com/ru/post/1780266/


All Articles