[Easyui] AutoComplete simple autocomplete and Ajax done from the server side

Source: Internet
Author: User

Call the AutoComplete method by using the ID to take the input tag object

<Script>    varsources= [            "ActionScript",            "AppleScript",            "ASP",            "COBOL",            "ColdFusion",            "Erlang",            "Fortran",            "Groovy",            "Haskell",            "Java",            "JavaScript",            "Lisp",            "Perl",            "PHP",            "Python",            "Ruby",            "Scala",            "Scheme"          ]; $(function() {        $( "#tags"). AutoComplete ({source:sources}); });</Script><Body>    <Divclass= "Ui-widget">        <H2>Inquire:<inputID= "tags"></H2>    </Div></Body>

{Source:sources} wraps multiple sources array of string types into JSON.

Ajax server-Side completion:

Java code for server-side Web pages, and Java code only.

String query = request.getparameter ("term");//get the parameters to matchstring[] sources = {"ActionScript",            "AppleScript",            "Asp",            "BASIC",            C,            "C + +",            "Clojure",            "COBOL",            "ColdFusion",            "Erlang",            "Fortran",            "Groovy",            "Haskell",            "Java",            "JavaScript",            "Lisp",            "Perl",            "PHP",            "Python",            "Ruby",            "Scala",            "Scheme"}; StringBuilder Builder=NewStringBuilder ("[");  for(inti=0;i<sources.length;i++) {//iterates through the target array, returning the results that match the criteria        if(NULL!=query) {            if(Sources[i].indexof (query) >= 0) {//string that indicates if Query,sources[i] was entered contains a string queryBuilder.append ("{\" label\ ": \" "+sources[i]+" \ "},");//JSON data stitched into {"label": Sources[i]}            }        }Else{//If you do not enter query, return all Sources[i] as a JSON array.Builder.append ("{\" label\ ": \" "+sources[i]+" \ "},"); }} String result= Builder.tostring ();//converted to a string.    if(Result.endswith (",")) {//because the concatenation result is converted into a string, the array will be more ","result = Result.substring (0,result.length ()-1);//The last comma needs to be truncated.} result+="]";//stitching "]"out.print (Result); 
View Code

Script

$(function() {        $( "#tags"). AutoComplete ({Source:function(request,response) {//The request.term estimate is the string after the input content is submitted. " Term= ' string ', in fact, is equivalent to passing in a            //{"term": JSON data for "string"}. Specifically described in the jQuery1.11.10 Help document, search get second is.$.get ("server/demo4_server.jsp", "term=" +request.term,function(data) {//URL (destination address), data (incoming), callback (callback function)                    varresult =$.parsejson (data); Response (result);//output return result                });    }        }); });
View Code

Parsejson (String strjson) function:

[Easyui] AutoComplete simple autocomplete and Ajax done from the server side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.