var Qs_Form_Element_Autocomplete = { options: {}, elementOptions: {}, init: function(id, options) { if (typeof options.dataType == 'undefined') { options.dataType = 'json'; } if (typeof options.formatResult == 'undefined') { options.formatResult = function (row) { return row.title; } } if (typeof options.formatMatch == 'undefined') { options.formatMatch = function (row, i, max) { return row.title; } } if (typeof options.formatItem == 'undefined') { options.formatItem = function (data, i, total) { return data.title; } } if (typeof options.onNotFound == 'undefined') { options.onNotFound = function (element) { var valueElement = qs.getPreviousTag(element, 'INPUT'); if (valueElement) { $(valueElement).val(''); } var titleElement = qs.getNextTag(element, 'INPUT'); if (titleElement) { $(titleElement).val(''); } } } if (typeof options.parse == 'undefined') { options.parse = function (data) { return Qs_Form_Element_Autocomplete.parse(id, data); } } Qs_Form_Element_Autocomplete.setElementOptions(id, options); $('#' + Qs_Form_Element_Autocomplete.getDisplayId(id)).autocomplete( options.urlOrData, options ).result(function(event, data, formatted) { var valueElement = qs.getPreviousTag(this, 'INPUT'); var titleElement = qs.getNextTag(this, 'INPUT'); var value = ''; var title = ''; if (typeof data != 'undefined') { if (typeof data.value != 'undefined') { value = data.value; } if (typeof data.title != 'undefined') { title = data.title; } } if (valueElement) { $(valueElement).val(value); } if (titleElement) { $(titleElement).val(title); } }); }, parse: function (id, response) { var parsed = []; var options = Qs_Form_Element_Autocomplete.getElementOptions(id); for (var i = 0; i < response.length; i++) { parsed.push({ data: response[i], value: response[i].title, result: options.formatResult && options.formatResult(response[i], response[i].value) || response[i].value }); } return parsed; }, getElementOptions: function (id) { if (typeof Qs_Form_Element_Autocomplete.elementOptions[id] == 'undefined') { return null; } return Qs_Form_Element_Autocomplete.elementOptions[id]; }, getElementOption: function (id, name) { var options = Qs_Form_Element_Autocomplete.getElementOptions(id); if (options) { if (typeof options[name] != 'undefined') { return options[name]; } } return null; }, setElementOption: function (id, name, value) { var options = Qs_Form_Element_Autocomplete.getElementOptions(id); if (options) { options[name] = value; } else { Qs_Form_Element_Autocomplete.elementOptions[id] = {name: value}; } }, setElementOptions: function (id, options) { Qs_Form_Element_Autocomplete.elementOptions[id] = options; }, getDisplayId: function (id) { return Qs_Form_Element_Autocomplete.getSystemId(id); }, getTitleId: function (id) { return Qs_Form_Element_Autocomplete.getSystemId(Qs_Form_Element_Autocomplete.getDisplayId()); }, getSystemId: function (id) { var pos = id.lastIndexOf('-'); var displayId = ''; if (-1 != pos) { pos++; displayId = id.substr(0, pos) + '_' + id.substr(pos); } else { displayId = '_' + id; } return displayId; } }