[jQuery Advanced] Autocomplete plugin autocompleter

script:

1

2

<script src="js/jquery.js" type="text/javascript"></script>

<script src="js/jquery.autocompleter.min.js" type="text/javascript"></script>

 

style:

1

<link rel="stylesheet" href="css/jquery.autocompleter.css">

 

Define your autocompleter:

1

2

3

$(function () {

    $('input').autocompleter({ source: 'path/to/get_data_request' });

});

 

or for local JSON sources:

1

2

3

4

5

6

7

8

var data = [

    "value""1""label""one" },

    "value""2""label""two" },

    "value""3""label""three" }

];

$(function () {

    $('input').autocompleter({ source: data });

});

If you do not define a value in the source object, the label will be used as the default value in the input field after selection.

 

method:

Change the definition of the option after the plugin:

1

$('#input').autocompleter('option', data);

 

E.g:

1

2

3

4

$('#input').autocompleter('option', {

    limit: 5,

    template: '<img src="{{ image }}" alt="Image for autocompleter list item" /> {{ label }}'

});

 

Open list:

1

$('#input').autocompleter('open');

 

Close the page:

1

$('#input').autocompleter('close');

Destroy the plugin:

1

$('#input').autocompleter('destroy');

 

Clear all caches:

1

$.autocompleter('clearCache');

 

Set default value:

1

2

3

$.autocompleter('defaults', {

    customClass: 'myClassForAutocompleter'

});

 

E.g:

Autocompleter 1st name input cache, matching emphasis limit and 5 results.

 

form:

1

2

3

4

5

6

<label for="gender_male">Male</label>

<input type="radio" name="gender" value="male" id="gender_male" checked="checked" />

<label for="gender_female">Female</label>

<input type="radio" name="gender" value="female" id="gender_female" />

<label for="firstname">First Name</label>

<input type="text" name="firstname" id="firstname" />

 

JavaScript:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

$(function () {

    $("#firstname").autocompleter({

        limit: 5,

        cache: true,

        combine: function () {

            var gender = $("input:radio[name=gender]:checked").val();

            return {

                gender: gender

            };

        },

        callback: function (value, index) {

            console.log( "Value "+value+" are selected (with index "+index+")." );

        }

    });

});

 

The main structure:

1

2

3

4

5

6

7

<div class="autocompleter" id="autocompleter-1">

    <ul class="autocompleter-list">

        <li class="autocompleter-item">First</li>

        ...

        <li class="autocompleter-item">Last</li>

    </ul>

</div>

 

Example: use in Angular2 client

    size:any=0;
    ngAfterViewInit() {
        let thiss=this;
            $('#autocomplete-ajax').autocomplete({
                minChars:2,
                lookup: function (query,done){
                thiss.getProject(query, function () {
                    debugger;
                    if (thiss.rootList && thiss.rootList.length > 0 ) {
                        thiss.size=0;
                        thiss.tempurl=thiss.route.url
                        .map(s=>s)
                        let path=thiss.tempurl.source._value[0].path;
                        if(path!='qiyefengxian'){
                            thiss.List=[];
                        }
                      var result = {
                          suggestions: thiss.List
                      };
                      done(result)
                    }else{
                      thiss.size=1;
                      thiss.id='';
                      thiss.List=[];
                      
                      var result = {
                        suggestions:  thiss.List
                      };
                      done(result)

                  }
              });  
            },
            
            autoFill:true,
            cacheLength:1,
            matchContains: true,
            onSelect: function (data) { 
              console.log("data",data)
              thiss.rootEntity.gsName=data.value;
              thiss.id=data.data;
              thiss.find();//查询一条数据的方法
            }
          });
        
    }
    //根据名称模糊查询 
    getProject(name:any,callback: any):any{
        let contion:any={
          name:name
      }
    
    console.log("name",name)
      this.httpService.find("Controller/selectName2",contion).then((response)=>{
        if(response.json().data==null){
          return false;
        }
        this.List=response.json().data;
        if (typeof callback == "function") {
            callback();
        }
         
      })
    }

 

Background query results, List<ResultEntity>

public class ResultEntity implements Serializable{
	
	private String value;
	
	private String data;

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	@Override
	public String toString() {
		return "ResultEntity [value=" + value + ", data=" + data + "]";
	}
	

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325861594&siteId=291194637