|
| 1 | +///<reference path="../jquery/jquery.d.ts" /> |
| 2 | +///<reference path="../jquery-autocomplete/jquery.autocomplete.d.ts" /> |
| 3 | + |
| 4 | + |
| 5 | +// ---------------------------------------------------------------------------------------- |
| 6 | +// --------------------------------- WEBSITE EXAMPLE -------------------------------------- |
| 7 | +// ---------- https://www.devbridge.com/sourcery/components/jquery-autocomplete/ ---------- |
| 8 | +// ---------------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +var input = $('#autocomplete'); |
| 11 | +var options = {}; |
| 12 | + |
| 13 | +input.autocomplete('disable'); |
| 14 | +input.autocomplete('setOptions', options); |
| 15 | + |
| 16 | +input.autocomplete().disable(); |
| 17 | +input.autocomplete().setOptions(options); |
| 18 | + |
| 19 | +// Ajax lookup: |
| 20 | +input.autocomplete({ |
| 21 | + serviceUrl: '/autocomplete/countries', |
| 22 | + onSelect: function (suggestion) { |
| 23 | + alert('You selected: ' + suggestion.value + ', ' + suggestion.data); |
| 24 | + } |
| 25 | +}); |
| 26 | + |
| 27 | +// Local lookup (no ajax): |
| 28 | +var countries = [ |
| 29 | + { value: 'Andorra', data: 'AD' }, |
| 30 | + // ... |
| 31 | + { value: 'Zimbabwe', data: 'ZZ' } |
| 32 | +]; |
| 33 | + |
| 34 | +input.autocomplete({ |
| 35 | + lookup: countries, |
| 36 | + onSelect: function (suggestion) { |
| 37 | + alert('You selected: ' + suggestion.value + ', ' + suggestion.data); |
| 38 | + } |
| 39 | +}); |
| 40 | + |
| 41 | +// Non standard query/results |
| 42 | +input.autocomplete({ |
| 43 | + paramName: 'searchString', |
| 44 | + transformResult: function(response: any, originalQuery: string): AutocompleteResponse { |
| 45 | + return { |
| 46 | + suggestions: $.map(response.myData, function (dataItem) { |
| 47 | + return {value: dataItem.valueField, data: dataItem.dataField}; |
| 48 | + }) |
| 49 | + }; |
| 50 | + } |
| 51 | +}); |
| 52 | + |
| 53 | + |
| 54 | +// ---------------------------------------------------------------------------------------- |
| 55 | +// ------------------------------ TEST INSTANCE METHODS ----------------------------------- |
| 56 | +// ---------------------------------------------------------------------------------------- |
| 57 | + |
| 58 | +input.autocomplete().setOptions(options); |
| 59 | +input.autocomplete().clear(); |
| 60 | +input.autocomplete().clearCache(); |
| 61 | +input.autocomplete().disable(); |
| 62 | +input.autocomplete().enable(); |
| 63 | +input.autocomplete().hide(); |
| 64 | +input.autocomplete().dispose(); |
| 65 | + |
| 66 | + |
| 67 | +// ---------------------------------------------------------------------------------------- |
| 68 | +// ------------------------------ TEST DEFAULT OPTIONS ------------------------------------ |
| 69 | +// ---------------------------------------------------------------------------------------- |
| 70 | + |
| 71 | +input.autocomplete({ |
| 72 | + |
| 73 | + //----------------o AJAX SETTINGS |
| 74 | + |
| 75 | + serviceUrl: '/autocomplete/countries', |
| 76 | + type: 'GET', |
| 77 | + dataType: 'text', |
| 78 | + paramName: 'query', |
| 79 | + params: {}, |
| 80 | + deferRequestBy: 0, |
| 81 | + ajaxSettings: {}, |
| 82 | + |
| 83 | + //----------------o CONFIG SETTINGS |
| 84 | + |
| 85 | + noCache: false, |
| 86 | + delimiter: "-", |
| 87 | + onSearchStart(query: string) { |
| 88 | + console.log("query: ", query); |
| 89 | + }, |
| 90 | + onSearchComplete(query: string, suggestions: AutocompleteSuggestion[]) { |
| 91 | + console.log("query: ", query); |
| 92 | + console.log("suggestions: ", suggestions); |
| 93 | + }, |
| 94 | + onSearchError(query: string, jqXHR: JQueryXHR, textStatus: string, errorThrown: any) { |
| 95 | + console.log("query: ", query); |
| 96 | + console.log("jqXHR: ", jqXHR); |
| 97 | + console.log("textStatus: ", textStatus); |
| 98 | + console.log("errorThrown: ", errorThrown); |
| 99 | + }, |
| 100 | + transformResult(response: any, originalQuery: string): AutocompleteResponse { |
| 101 | + return { |
| 102 | + suggestions: [ |
| 103 | + { value: 'Andorra', data: 'AD' }, |
| 104 | + // ... |
| 105 | + { value: 'Zimbabwe', data: 'ZZ' } |
| 106 | + ] |
| 107 | + } |
| 108 | + }, |
| 109 | + onSelect(suggestion: AutocompleteSuggestion) { |
| 110 | + console.log("suggestions: ", suggestion); |
| 111 | + }, |
| 112 | + minChars: 1, |
| 113 | + lookupLimit: 1, |
| 114 | + lookup: [ |
| 115 | + { value: 'Andorra', data: 'AD' }, |
| 116 | + // ... |
| 117 | + { value: 'Zimbabwe', data: 'ZZ' } |
| 118 | + ], |
| 119 | + lookupFilter(suggestion: AutocompleteSuggestion, query: string, queryLowercase: string): any { |
| 120 | + return query !== "query" |
| 121 | + }, |
| 122 | + triggerSelectOnValidInput: true, |
| 123 | + preventBadQueries: true, |
| 124 | + autoSelectFirst: false, |
| 125 | + onHide(container: any) { |
| 126 | + console.log("container: ", container); |
| 127 | + }, |
| 128 | + |
| 129 | + //----------------o PRESENTATION SETTINGS |
| 130 | + |
| 131 | + beforeRender(container: any) { |
| 132 | + console.log("container: ", container); |
| 133 | + }, |
| 134 | + formatResult(suggestion: AutocompleteSuggestion, currentValue: string): string { |
| 135 | + return currentValue; |
| 136 | + }, |
| 137 | + groupBy: [ |
| 138 | + { value: 'Chicago Blackhawks', data: { category: 'NHL' } }, |
| 139 | + { value: 'Chicago Bulls', data: { category: 'NBA' } } |
| 140 | + ], |
| 141 | + maxHeight: 300, |
| 142 | + width: "auto", |
| 143 | + zIndex: 9999, |
| 144 | + appendTo: document.body, |
| 145 | + forceFixPosition: false, |
| 146 | + orientation: "bottom", |
| 147 | + preserveInput: false, |
| 148 | + showNoSuggestionNotice: false, |
| 149 | + noSuggestionNotice: "No results", |
| 150 | + onInvalidateSelection() { |
| 151 | + console.log("onInvalidateSelection"); |
| 152 | + }, |
| 153 | + tabDisabled: false |
| 154 | +}); |
0 commit comments