30/06/2011, 12:00
|
| | Fecha de Ingreso: mayo-2010 Ubicación: Barcelona
Mensajes: 7
Antigüedad: 14 años, 8 meses Puntos: 0 | |
segunda parte código
Código:
function selectItem(li, extraData) {
if (!li) {
li = document.createElement("li");
li.extra = [];
li.selectValue = "";
}
var v = $.trim(li.selectValue ? li.selectValue : li.innerHTML);
input.lastSelected = v;
prev = v;
$results.html("");
$input.val(v);
hideResultsNow();
if (options.onItemSelect) {
setTimeout(function () { options.onItemSelect(li, extraData) }, 1);
}
};
function showResults() {
//var pos = findPos(input);
var iWidth = (options.width > 0) ? options.width : $input.width();
/*$results.css({
width: parseInt(iWidth) + "px",
top: (pos.y + input.offsetHeight - 1) + "px",
left: pos.x + xAdj + "px"
}).show();*/
$results.css({
width: parseInt(iWidth) + "px",
top: ($input.offset().top + input.offsetHeight - 1) + "px",
left: $input.offset().left + xAdj + "px"
}).show();
};
function hideResults() {
if (timeout) clearTimeout(timeout);
//timeout = setTimeout(hideResultsNow, 200);
hideResultsNow();
};
function hideResultsNow() {
if (hidingResults) {
return;
}
hidingResults = true;
if (timeout) {
clearTimeout(timeout);
}
var v = $input.val();
if ($results.is(":visible")) {
$results.hide();
}
if (options.mustMatch) {
if (!input.lastSelected || input.lastSelected != v) {
selectItem(null);
}
}
hidingResults = false;
};
function receiveData(q, data) {
if (data) {
//results.innerHTML = "";
$("ul", $results).remove();
if (!hasFocus || data.length == 0) return hideResultsNow();
/*if ($.browser.msie) {
// we put a styled iframe behind the calendar so HTML SELECT elements don't show through
$results.append(document.createElement('iframe'));
}*/
$results.append(dataToDom(data));
showResults();
} else {
hideResultsNow();
}
};
function parseData(data) {
if (!data) return null;
var parsed = [];
var rows = data.split(options.lineSeparator);
for (var i = 0; i < rows.length; i++) {
var row = $.trim(rows[i]);
if (row) {
parsed[parsed.length] = row.split(options.cellSeparator);
}
}
return parsed;
};
function dataToDom(data) {
var ul = document.createElement("ul");
var num = data.length;
// limited results to a max number
if ((options.maxItemsToShow > 0) && (options.maxItemsToShow < num)) num = options.maxItemsToShow;
for (var i = 0; i < num; i++) {
var row = data[i];
if (!row) continue;
var li = document.createElement("li");
if (options.formatItem) {
li.innerHTML = options.formatItem(row, i, num);
li.selectValue = row[0];
li.otherValue = row[1];
} else {
li.innerHTML = row[0];
li.selectValue = row[0];
li.otherValue = row[1];
}
var extra = null;
if (row.length > 1) {
extra = [];
for (var j = 1; j < row.length; j++) {
extra[extra.length] = row[j];
}
}
li.extra = extra;
ul.appendChild(li);
$(li).hover(
function () { $("li", ul).removeClass("ac_over"); $(this).addClass("ac_over"); active = $("li", ul).indexOf($(this).get(0)); },
function () { $(this).removeClass("ac_over"); }
).click(function (e) {
e.preventDefault();
e.stopPropagation();
selectItem(this)
});
if (i == 0) {
$(li).addClass("ac_over");
active = 0;
}
}
$(ul).mousedown(function () {
mouseDownOnSelect = true;
}).mouseup(function () {
mouseDownOnSelect = false;
});
return ul;
};
function requestData(q) {
var data = null;
var sc = document.createElement('script');
sc.type = 'text/javascript';
sc.src = '/AutoComplete.ashx?query=' + encodeURI(q) + '&languageCode=ES&limit=10&format=script&' + Math.random();
document.getElementsByTagName('head')[0].appendChild(sc);
};
this.success = function (data, q) {
if (valueSelected) return;
if ($input.val().length < options.minChars) return;
receiveData(q, data);
}
function makeUrl(q) {
var sep = options.url.indexOf('?') == -1 ? '?' : '&';
var url = options.url + sep + "query=" + encodeURI(q);
for (var i in options.extraParams) {
url += "&" + i + "=" + encodeURI(options.extraParams[i]);
}
return url;
};
this.setExtraParams = function (p) {
options.extraParams = p;
};
function findPos(obj) {
var curleft = obj.offsetLeft || 0;
var curtop = obj.offsetTop || 0;
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft
curtop += obj.offsetTop
}
return { x: curleft, y: curtop };
}
}
jQuery.fn.autocomplete = function(url, options, data) {
options = options || {};
options.url = url;
options.data = ((typeof data == "object") && (data.constructor == Array)) ? data : null;
// Set default values for required options
options = $.extend({
inputClass: "ac_input",
resultsClass: "ac_results",
lineSeparator: "\n",
cellSeparator: "|",
minChars: 3,
delay: 10,
mustMatch: 0,
extraParams: {},
loadingElement: null,
selectFirst: true,
selectOnly: true,
maxItemsToShow: 10,
width: 0,
attachedToBody: false
}, options);
options.width = parseInt(options.width, 10);
this.each(function() {
var input = this;
new jQuery.autocomplete(input, options);
});
// Don't break the chain
return this;
}
jQuery.fn.autocompleteArray = function(data, options) {
return this.autocomplete(null, options, data);
}
jQuery.fn.indexOf = function(e) {
for (var i = 0; i < this.length; i++) {
if (this[i] == e) return i;
}
return -1;
};
function bind(element) {
var url = "/AutoComplete.ashx";
$(function () {
$(element).autocomplete(url, {
maxItemsToShow: 10,
selectFirst: true,
selectOnly: true,
attachedToBody: true,
onItemSelect: function (item, extraData) {
$("#selectedLocationID").val(item.extra[1]);
$("#selectedFileName").val(item.extra[0]);
$("#selectedCityName").val(item.selectValue);
if (!(extraData && extraData.eventType == "blur")) {
$(element).focus();
}
},
formatItem: function (row, i, num) {
var originalLength = row[0].split(",").length;
var display = row[0].ellipsisString(47);
var parts = display.split(",");
var result = "";
if (parts.length > 2) {
return result = parts[0] + ", " + parts[1] + ", <b>" + parts[2] + "</b>";
} else if (parts.length == 2 && originalLength == 3) {
return result = parts[0] + ", " + parts[1];
} else if (parts.length == 2) {
return result = parts[0] + ", <b>" + parts[1] + "</b>"
} else {
return result = parts[0];
}
},
width: element.width,
extraParams: { languageCode: "ES", format: "script", limit: "10" }
});
});
}
Última edición por onpixel; 26/04/2012 a las 06:10 |