├── LICENSE ├── README.md ├── ajaxcall.html ├── img └── loading.gif ├── sampledata.json ├── tAutocomplete.html ├── tautocomplete.css ├── tautocomplete.jquery.json └── tautocomplete.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 vyasrao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Project not maintained anymore, happy to transfer the owenership! 2 | ============= 3 | 4 | tAutocomplete 5 | ============= 6 | 7 | Autocomplete Table is a jQuery plugin that provides suggestions from a multi-column table in a dropdown list while the visitors type into the input field.

8 | Complete details and demo is available at http://vyasrao.github.io/tAutocomplete/ 9 | 10 | New releases
11 | 1. Delay search result
12 | 2. Highlight search word among result
13 | 3. Hide/ show columns
14 | 4. Display complete details on the selected row. 15 | -------------------------------------------------------------------------------- /ajaxcall.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 | 11 | 43 | 44 |




45 |
46 |

47 | Text:
48 | ID:
49 |
50 | 51 | -------------------------------------------------------------------------------- /img/loading.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vyasrao/tAutocomplete/01bee1d7dca71d8dd3df0d9431446f690717b20f/img/loading.gif -------------------------------------------------------------------------------- /sampledata.json: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vyasrao/tAutocomplete/01bee1d7dca71d8dd3df0d9431446f690717b20f/sampledata.json -------------------------------------------------------------------------------- /tAutocomplete.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vyasrao/tAutocomplete/01bee1d7dca71d8dd3df0d9431446f690717b20f/tAutocomplete.html -------------------------------------------------------------------------------- /tautocomplete.css: -------------------------------------------------------------------------------- 1 | .adropdown { 2 | background-color: #fff; 3 | margin: 0; 4 | padding: 0; 5 | display: block; 6 | top: 0px; 7 | left: 0px; 8 | text-align: left; 9 | position: absolute; 10 | box-shadow: 0px 0px 20px rgba(0,0,0,0.10), 0px 10px 20px rgba(0,0,0,0.05), 0px 20px 20px rgba(0,0,0,0.05), 0px 30px 20px rgba(0,0,0,0.05); 11 | } 12 | .adropdown table { 13 | font-family: 'Arial'; 14 | margin: 35px 1px 0px 1px; 15 | border-collapse: collapse; 16 | border-bottom: 2px solid #00cccc; 17 | border-spacing: 0px; 18 | display: none; 19 | color: #999; 20 | cursor: default; 21 | width: 100%; 22 | border-left: 0px; 23 | border-right: 0px; 24 | border-top: 0px; 25 | } 26 | 27 | .adropdown tr:hover { 28 | background: #f4f4f4; 29 | color: #555; 30 | } 31 | 32 | .adropdown th { 33 | background: #00cccc; 34 | color: #fff; 35 | text-transform: uppercase; 36 | font-size: 12px; 37 | padding: 5px 10px; 38 | text-align: center; 39 | } 40 | 41 | .adropdown tr td { 42 | font-family: "Open Sans", "Helvetica Neue", Helvetica, sans-serif; 43 | margin: 0; 44 | font-variant: normal; 45 | font-size: 12px; 46 | padding: 5px; 47 | min-height: 15px; 48 | border-bottom: 1px solid #eee; 49 | border-left: 0px; 50 | border-right: 0px; 51 | border-top: 0px; 52 | text-align: left; 53 | } 54 | 55 | .adropdown .selected { 56 | background: #17D5D5; 57 | padding-bottom: 8px; 58 | color: #fff; 59 | } 60 | 61 | .adropdown div { 62 | position: absolute; 63 | } 64 | 65 | .adropdown table caption { 66 | caption-side: bottom; 67 | padding: 10px; 68 | display: none; 69 | font-size: 12px; 70 | } 71 | 72 | .acontainer { 73 | position: relative; 74 | display: inline-block; 75 | } 76 | 77 | .acontainer input { 78 | border-radius: 2px; 79 | color: #000; 80 | font-family: 'Open Sans', sans-serif; 81 | font-size: 1em; 82 | transition: background 0.3s ease-in-out; 83 | width: 100%; 84 | padding: 2px 25px 2px 5px; 85 | outline: 0; 86 | border-left: 1px solid #ccc; 87 | border-top: 1px solid #ccc; 88 | border-bottom: 1px solid #ccc; 89 | border-right: 10px solid #ccc; 90 | margin: 0px; 91 | } 92 | 93 | .acontainer input:focus { 94 | padding: 2px 25px 2px 5px; 95 | margin: 0px; 96 | outline: none; 97 | border-color: #9ecaed; 98 | box-shadow: 0 0 2px #9ecaed; 99 | } 100 | 101 | .acontainer input::-ms-clear { 102 | width: 0; 103 | height: 0; 104 | } 105 | .inputfocus { 106 | outline: 0; 107 | min-width: 140px; 108 | margin: 5px 0px 5px 5px !important; 109 | position: relative; 110 | z-index: 1000; 111 | } 112 | .loading { 113 | background: url(img/loading.gif) no-repeat right center; 114 | } 115 | .word-highlight{ 116 | background-color: #FFFF88; 117 | } 118 | .highlight { 119 | z-index: 999; 120 | } 121 | 122 | .aclassic { 123 | background-color: transparent; 124 | margin: 0; 125 | padding: 0; 126 | display: block; 127 | top: 0px; 128 | left: 0px; 129 | text-align: left; 130 | position: absolute; 131 | } 132 | 133 | .aclassic table { 134 | display: none; 135 | font-family: Arial, Helvetica, sans-serif; 136 | color: #666; 137 | font-size: 12px; 138 | background: #eaebec; 139 | border: #ccc 0px solid; 140 | -moz-border-radius: 3px; 141 | -webkit-border-radius: 3px; 142 | border-radius: 3px; 143 | -moz-box-shadow: 0 1px 2px #d1d1d1; 144 | -webkit-box-shadow: 0 1px 2px #d1d1d1; 145 | box-shadow: 0 1px 2px #d1d1d1; 146 | } 147 | 148 | .aclassic table th { 149 | padding: 5px 10px; 150 | border-top: 1px solid #fafafa; 151 | border-bottom: 1px solid #e0e0e0; 152 | text-align: center; 153 | background: #ededed; 154 | background: -webkit-gradient(linear, left top, left bottom, from(#ededed), to(#ebebeb)); 155 | background: -moz-linear-gradient(top, #ededed, #ebebeb); 156 | font-weight: bold; 157 | } 158 | 159 | .aclassic table tr:first-child th:first-child { 160 | -moz-border-radius-topleft: 3px; 161 | -webkit-border-top-left-radius: 3px; 162 | border-top-left-radius: 3px; 163 | } 164 | 165 | .aclassic table tr:first-child th:last-child { 166 | -moz-border-radius-topright: 3px; 167 | -webkit-border-top-right-radius: 3px; 168 | border-top-right-radius: 3px; 169 | } 170 | 171 | .aclassic table td:first-child { 172 | text-align: left; 173 | padding: 5px; 174 | border-left: 0; 175 | } 176 | 177 | .aclassic table td { 178 | padding: 5px; 179 | border-top: 1px solid #ffffff; 180 | border-bottom: 1px solid #e0e0e0; 181 | } 182 | 183 | .aclassic table tr:nth-child(even) { 184 | background: #f6f6f6; 185 | background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#f6f6f6)); 186 | background: -moz-linear-gradient(top, #f8f8f8, #f6f6f6); 187 | } 188 | 189 | .aclassic table tr:nth-child(odd) { 190 | background: #fafafa; 191 | background: -webkit-gradient(linear, left top, left bottom, from(#fbfbfb), to(#fafafa)); 192 | background: -moz-linear-gradient(top, #fbfbfb, #fafafa); 193 | } 194 | 195 | .aclassic table tr:last-child td { 196 | border-bottom: 0; 197 | } 198 | 199 | .aclassic table tr:last-child td:first-child { 200 | -moz-border-radius-bottomleft: 3px; 201 | -webkit-border-bottom-left-radius: 3px; 202 | border-bottom-left-radius: 3px; 203 | } 204 | 205 | .aclassic table tr:last-child td:last-child { 206 | -moz-border-radius-bottomright: 3px; 207 | -webkit-border-bottom-right-radius: 3px; 208 | border-bottom-right-radius: 3px; 209 | } 210 | 211 | .aclassic table tr:hover { 212 | background: #f2f2f2; 213 | background: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#f0f0f0)); 214 | background: -moz-linear-gradient(top, #f2f2f2, #f0f0f0); 215 | } 216 | 217 | .aclassic .selected td { 218 | background: #a8cfee; 219 | background: -webkit-gradient(linear, left top, left bottom, from(#9ecaed), to(#a8cfee)); 220 | background: -moz-linear-gradient(top, #9ecaed, #a8cfee); 221 | padding-bottom: 8px; 222 | color: #555; 223 | } 224 | 225 | .aclassic div { 226 | position: absolute; 227 | } 228 | 229 | .aclassic table caption { 230 | caption-side: bottom; 231 | padding: 10px; 232 | display: none; 233 | font-size: 12px; 234 | } 235 | .awhite { 236 | background-color: transparent; 237 | margin: 0; 238 | padding: 0; 239 | display: block; 240 | top: 0px; 241 | left: 0px; 242 | text-align: left; 243 | position: absolute; 244 | } 245 | 246 | .awhite table { 247 | display: none; 248 | font-family: Arial, Helvetica, sans-serif; 249 | color: #666; 250 | font-size: 12px; 251 | background-color: #fff; 252 | border: 0px solid #ccc; 253 | -webkit-border-radius: 8px; 254 | -moz-border-radius: 8px; 255 | border-radius: 8px; 256 | -webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2); 257 | -moz-box-shadow: 0 5px 10px rgba(0,0,0,.2); 258 | box-shadow: 0 5px 10px rgba(0,0,0,.2); 259 | } 260 | .awhite table th { 261 | padding: 10px 10px; 262 | text-align: center; 263 | font-weight: bold; 264 | } 265 | 266 | .awhite table tr:first-child th:first-child { 267 | -moz-border-radius-topleft: 8px; 268 | -webkit-border-top-left-radius: 8px; 269 | border-top-left-radius: 8px; 270 | } 271 | 272 | .awhite table tr:first-child th:last-child { 273 | -moz-border-radius-topright: 8px; 274 | -webkit-border-top-right-radius: 8px; 275 | border-top-right-radius: 8px; 276 | } 277 | 278 | .awhite table td { 279 | padding: 5px; 280 | border: 0px; 281 | } 282 | .awhite table tr:last-child td:nth-child(2) { 283 | -moz-border-radius-bottomleft:8px; 284 | -webkit-border-bottom-left-radius:8px; 285 | border-bottom-left-radius:8px; 286 | } 287 | .awhite table tr:last-child td:last-child { 288 | -moz-border-radius-bottomright:8px; 289 | -webkit-border-bottom-right-radius:8px; 290 | border-bottom-right-radius:8px; 291 | } 292 | 293 | .awhite table tr:hover { 294 | background: #f2f2f2; 295 | } 296 | 297 | .awhite .selected td { 298 | padding-bottom: 8px; 299 | color: #fff; 300 | background-color: #0097cf; 301 | } 302 | 303 | .awhite div { 304 | position: absolute; 305 | } 306 | 307 | .awhite table caption { 308 | caption-side: bottom; 309 | padding: 10px; 310 | display: none; 311 | font-size: 12px; 312 | } -------------------------------------------------------------------------------- /tautocomplete.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tautocomplete", 3 | "title": "Autocomplete-Table", 4 | "description": "Automplete drop-down lists the details in a table format. The plugin is useful when multiple details has to be displayed.", 5 | "keywords": [ 6 | "autocomplete", 7 | "table" 8 | ], 9 | "version": "1.0.0", 10 | "author": { 11 | "name": "Vedavyas" 12 | }, 13 | "licenses": [ 14 | { 15 | "type": "MIT", 16 | "url": "https://raw.githubusercontent.com/vyasrao/tAutocomplete/master/LICENSE" 17 | } 18 | ], 19 | "bugs": "https://github.com/vyasrao/tautocomplete/issues", 20 | "homepage": "http://vyasrao.github.io/tAutocomplete", 21 | "docs": "http://vyasrao.github.io/tAutocomplete", 22 | "dependencies": { 23 | "jquery": ">=1.5" 24 | } 25 | } -------------------------------------------------------------------------------- /tautocomplete.js: -------------------------------------------------------------------------------- 1 | (function ($) { 2 | "use strict"; 3 | 4 | $.fn.tautocomplete = function (options, callback) { 5 | 6 | // default parameters 7 | var settings = $.extend({ 8 | width: "500px", 9 | columns: [], 10 | hide: [false], 11 | onchange: null, 12 | norecord: "No Records Found", 13 | dataproperty: null, 14 | regex: "^[a-zA-Z0-9\b]+$", 15 | data: null, 16 | placeholder: null, 17 | theme: "default", 18 | ajax: null, 19 | delay: 1000, 20 | highlight:'word-highlight' 21 | }, options); 22 | 23 | var cssClass = { 24 | "default": "adropdown", 25 | "classic": "aclassic", 26 | "white": "awhite"}; 27 | 28 | settings.theme = cssClass[settings.theme]; 29 | 30 | // initialize DOM elements 31 | var el = { 32 | ddDiv: $("
", { class: settings.theme }), 33 | ddTable: $("
", { style: "width:" + settings.width }), 34 | ddTableCaption: $("" + settings.norecord + ""), 35 | ddTextbox: $("") 36 | }; 37 | 38 | var keys = { 39 | UP: 38, 40 | DOWN: 40, 41 | ENTER: 13, 42 | TAB: 9, 43 | BACKSPACE: 8 44 | }; 45 | 46 | var errors = { 47 | columnNA: "Error: Columns Not Defined", 48 | dataNA: "Error: Data Not Available" 49 | }; 50 | 51 | // plugin properties 52 | var tautocomplete = { 53 | id: function () { 54 | return el.ddTextbox.data("id"); 55 | }, 56 | text: function () { 57 | return el.ddTextbox.data("text"); 58 | }, 59 | searchdata: function () { 60 | return el.ddTextbox.val(); 61 | }, 62 | settext: function (text) { 63 | el.ddTextbox.val(text); 64 | }, 65 | isNull: function () { 66 | if (el.ddTextbox.data("text") == "" || el.ddTextbox.data("text") == null) 67 | return true; 68 | else 69 | return false; 70 | }, 71 | all: function(){ 72 | return selectedData; 73 | } 74 | }; 75 | 76 | // delay function which listens to the textbox entry 77 | var delay = (function () { 78 | var timer = 0; 79 | return function (callsback, ms) { 80 | clearTimeout(timer); 81 | timer = setTimeout(callsback, ms); 82 | }; 83 | })(); 84 | 85 | // key/value containing data of the selcted row 86 | var selectedData = {}; 87 | 88 | var focused = false; 89 | 90 | // check if the textbox is focused. 91 | if (this.is(':focus')) { 92 | focused = true; 93 | } 94 | 95 | // get number of columns 96 | var cols = settings.columns.length; 97 | 98 | var orginalTextBox = this; 99 | 100 | // wrap the div for style 101 | this.wrap("
"); 102 | 103 | // create a textbox for input 104 | this.after(el.ddTextbox); 105 | el.ddTextbox.attr("autocomplete", "off"); 106 | el.ddTextbox.css("width", this.width + "px"); 107 | el.ddTextbox.css("font-size", this.css("font-size")); 108 | el.ddTextbox.attr("placeholder", settings.placeholder); 109 | 110 | // check for mandatory parameters 111 | if (settings.columns == "" || settings.columns == null) { 112 | el.ddTextbox.attr("placeholder", errors.columnNA); 113 | } 114 | else if ((settings.data == "" || settings.data == null) && settings.ajax == null) { 115 | el.ddTextbox.attr("placeholder", errors.dataNA); 116 | } 117 | 118 | // append div after the textbox 119 | this.after(el.ddDiv); 120 | 121 | // hide the current text box (used for stroing the values) 122 | this.hide(); 123 | 124 | // append table after the new textbox 125 | el.ddDiv.append(el.ddTable); 126 | el.ddTable.attr("cellspacing", "0"); 127 | 128 | // append table caption 129 | el.ddTable.append(el.ddTableCaption); 130 | 131 | // create table columns 132 | var header = ""; 133 | for (var i = 0; i <= cols - 1; i++) { 134 | header = header + "" + settings.columns[i] + "" 135 | } 136 | header = header + "" 137 | el.ddTable.append(header); 138 | 139 | // assign data fields to the textbox, helpful in case of .net postbacks 140 | { 141 | var id = "", text = ""; 142 | 143 | if (this.val() != "") { 144 | var val = this.val().split("#$#"); 145 | id = val[0]; 146 | text = val[1]; 147 | } 148 | 149 | el.ddTextbox.attr("data-id", id); 150 | el.ddTextbox.attr("data-text", text); 151 | el.ddTextbox.val(text); 152 | } 153 | 154 | if (focused) { 155 | el.ddTextbox.focus(); 156 | } 157 | 158 | // event handlers 159 | 160 | // autocomplete key press 161 | el.ddTextbox.keyup(function (e) { 162 | //return if up/down/return key 163 | if ((e.keyCode < 46 || e.keyCode > 105) && (e.keyCode != keys.BACKSPACE)) { 164 | e.preventDefault(); 165 | return; 166 | } 167 | //delay for 1 second: wait for user to finish typing 168 | delay(function () { 169 | processInput(); 170 | }, settings.delay); 171 | }); 172 | 173 | // process input 174 | function processInput() 175 | { 176 | if (el.ddTextbox.val() == "") { 177 | hideDropDown(); 178 | return; 179 | } 180 | 181 | // hide no record found message 182 | el.ddTableCaption.hide(); 183 | 184 | el.ddTextbox.addClass("loading"); 185 | 186 | if (settings.ajax != null) 187 | { 188 | var tempData = null; 189 | if ($.isFunction(settings.ajax.data)) { 190 | tempData = settings.ajax.data.call(this); 191 | } 192 | else{ 193 | tempData = settings.ajax.data; 194 | } 195 | // get json data 196 | $.ajax({ 197 | type: settings.ajax.type || 'GET', 198 | dataType: 'json', 199 | contentType: settings.ajax.contentType || 'application/json; charset=utf-8', 200 | headers: settings.ajax.headers || { 'Content-Type': 'application/x-www-form-urlencoded' }, 201 | data: tempData || null, 202 | url: settings.ajax.url, 203 | success: ajaxData, 204 | error: function (xhr, ajaxOptions, thrownError) { 205 | el.ddTextbox.removeClass("loading"); 206 | alert('Error: ' + xhr.status || ' - ' || thrownError); 207 | } 208 | }); 209 | } 210 | else if ($.isFunction(settings.data)) { 211 | var data = settings.data.call(this); 212 | jsonParser(data); 213 | } 214 | else { 215 | // default function 216 | null; 217 | } 218 | } 219 | 220 | // call on Ajax success 221 | function ajaxData(jsonData) 222 | { 223 | if (settings.ajax.success == null || settings.ajax.success == "" || (typeof settings.ajax.success === "undefined")) 224 | { 225 | jsonParser(jsonData); 226 | } 227 | else { 228 | if ($.isFunction(settings.ajax.success)) { 229 | var data = settings.ajax.success.call(this, jsonData); 230 | jsonParser(data); 231 | } 232 | } 233 | } 234 | 235 | // do not allow special characters 236 | el.ddTextbox.keypress(function (event) { 237 | var regex = new RegExp(settings.regex); 238 | var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); 239 | 240 | if (!regex.test(key)) { 241 | event.preventDefault(); 242 | return false; 243 | } 244 | }); 245 | 246 | // textbox keypress events (return key, up and down arrow) 247 | el.ddTextbox.keydown(function (e) { 248 | 249 | var tbody = el.ddTable.find("tbody"); 250 | var selected = tbody.find(".selected"); 251 | 252 | if (e.keyCode == keys.ENTER) { 253 | e.preventDefault(); 254 | select(); 255 | } 256 | if (e.keyCode == keys.UP) { 257 | el.ddTable.find(".selected").removeClass("selected"); 258 | if (selected.prev().length == 0) { 259 | tbody.find("tr:last").addClass("selected"); 260 | } else { 261 | selected.prev().addClass("selected"); 262 | } 263 | } 264 | if (e.keyCode == keys.DOWN) { 265 | tbody.find(".selected").removeClass("selected"); 266 | if (selected.next().length == 0) { 267 | tbody.find("tr:first").addClass("selected"); 268 | } else { 269 | el.ddTable.find(".selected").removeClass("selected"); 270 | selected.next().addClass("selected"); 271 | } 272 | } 273 | }); 274 | 275 | // row click event 276 | el.ddTable.delegate("tr", "mousedown", function () { 277 | el.ddTable.find(".selected").removeClass("selected"); 278 | $(this).addClass("selected"); 279 | select(); 280 | }); 281 | 282 | // textbox blur event 283 | el.ddTextbox.focusout(function () { 284 | hideDropDown(); 285 | // clear if the text value is invalid 286 | if ($(this).val() != $(this).data("text")) { 287 | 288 | var change = true; 289 | if ($(this).data("text") == "") { 290 | change = false; 291 | } 292 | 293 | $(this).data("text", ""); 294 | $(this).data("id", ""); 295 | $(this).val(""); 296 | orginalTextBox.val(""); 297 | 298 | if (change) { 299 | onChange(); 300 | } 301 | } 302 | }); 303 | 304 | function select() { 305 | 306 | var selected = el.ddTable.find("tbody").find(".selected"); 307 | 308 | el.ddTextbox.data("id", selected.find('td').eq(0).text()); 309 | el.ddTextbox.data("text", selected.find('td').eq(1).text()); 310 | 311 | for(var i=0; i < cols; i++) 312 | { 313 | selectedData[settings.columns[i]] = selected.find('td').eq(i).text(); 314 | } 315 | 316 | el.ddTextbox.val(selected.find('td').eq(1).text()); 317 | orginalTextBox.val(selected.find('td').eq(0).text() + '#$#' + selected.find('td').eq(1).text()); 318 | hideDropDown(); 319 | onChange(); 320 | el.ddTextbox.focus(); 321 | } 322 | 323 | function onChange() 324 | { 325 | // onchange callback function 326 | if ($.isFunction(settings.onchange)) { 327 | settings.onchange.call(this); 328 | } 329 | else { 330 | // default function for onchange 331 | } 332 | } 333 | 334 | function hideDropDown() { 335 | el.ddTable.hide(); 336 | el.ddTextbox.removeClass("inputfocus"); 337 | el.ddDiv.removeClass("highlight"); 338 | el.ddTableCaption.hide(); 339 | } 340 | 341 | function showDropDown() { 342 | 343 | var cssTop = (el.ddTextbox.height() + 20) + "px 1px 0px 1px"; 344 | var cssBottom = "1px 1px " + (el.ddTextbox.height() + 20) + "px 1px"; 345 | 346 | // reset div top, left and margin 347 | el.ddDiv.css("top", "0px"); 348 | el.ddDiv.css("left", "0px"); 349 | el.ddTable.css("margin", cssTop); 350 | 351 | el.ddTextbox.addClass("inputfocus"); 352 | el.ddDiv.addClass("highlight"); 353 | el.ddTable.show(); 354 | 355 | // adjust div top according to the visibility 356 | if (!isDivHeightVisible(el.ddDiv)) { 357 | el.ddDiv.css("top", -1 * (el.ddTable.height()) + "px"); 358 | el.ddTable.css("margin", cssBottom); 359 | if (!isDivHeightVisible(el.ddDiv)) { 360 | el.ddDiv.css("top", "0px"); 361 | el.ddTable.css("margin", cssTop); 362 | $('html, body').animate({ 363 | scrollTop: (el.ddDiv.offset().top - 60) 364 | }, 250); 365 | } 366 | } 367 | // adjust div left according to the visibility 368 | if (!isDivWidthVisible(el.ddDiv)) { 369 | el.ddDiv.css("left", "-" + (el.ddTable.width() - el.ddTextbox.width() - 20) + "px"); 370 | } 371 | } 372 | function jsonParser(jsonData) { 373 | try{ 374 | el.ddTextbox.removeClass("loading"); 375 | 376 | // remove all rows from the table 377 | el.ddTable.find("tbody").find("tr").remove(); 378 | 379 | // regular expression for word highlight 380 | var re = null; 381 | if(settings.highlight != null){ 382 | var highlight = true; 383 | var re = new RegExp(el.ddTextbox.val(),"gi"); 384 | } 385 | 386 | var i = 0, j = 0; 387 | var row = null, cell = null; 388 | if (jsonData != null) { 389 | for (i = 0; i < jsonData.length; i++) { 390 | 391 | // display only 15 rows of data 392 | if (i >= 15) 393 | continue; 394 | 395 | var obj = jsonData[i]; 396 | row = ""; 397 | j = 0; 398 | 399 | for (var key in obj) { 400 | 401 | // return on column count 402 | if (j <= cols) { 403 | cell = obj[key] + ""; 404 | 405 | if(highlight){ 406 | cell = cell.replace(re,"$&"); 407 | } 408 | row = row + "" + cell + ""; 409 | } 410 | else { 411 | continue; 412 | } 413 | j++; 414 | } 415 | // append row to the table 416 | el.ddTable.append("" + row + ""); 417 | } 418 | } 419 | // show no records exists 420 | if (i == 0) 421 | el.ddTableCaption.show(); 422 | 423 | // hide columns 424 | for(var i=0; (i< settings.hide.length) && (i< cols) ; i++) 425 | { 426 | if(!settings.hide[i]) 427 | el.ddTable.find('td:nth-child('+ (i+1) +')').hide(); 428 | } 429 | 430 | el.ddTable.find("tbody").find("tr:first").addClass('selected'); 431 | showDropDown(); 432 | } 433 | catch (e) 434 | { 435 | alert("Error: " + e); 436 | } 437 | } 438 | return tautocomplete; 439 | }; 440 | }(jQuery)); 441 | 442 | function isDivHeightVisible(elem) { 443 | var docViewTop = $(window).scrollTop(); 444 | var docViewBottom = docViewTop + $(window).height(); 445 | 446 | var elemTop = $(elem).offset().top; 447 | var elemBottom = elemTop + $(elem).height(); 448 | 449 | return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom) 450 | && (elemBottom <= docViewBottom) && (elemTop >= docViewTop)); 451 | } 452 | 453 | function isDivWidthVisible(elem) { 454 | var docViewLeft = $(window).scrollLeft(); 455 | var docViewRight = docViewLeft + $(window).width(); 456 | 457 | var elemLeft = $(elem).offset().left; 458 | var elemRight = elemLeft + $(elem).width(); 459 | 460 | return ((elemRight >= docViewLeft) && (elemLeft <= docViewRight) 461 | && (elemRight <= docViewRight) && (elemLeft >= docViewLeft)); 462 | } --------------------------------------------------------------------------------