williamorazi
12-17-2010, 06:04 PM
Hey,
I'm working on an auto-suggest for our search function, but I am running into a wall (possibly due to lack of sleep).
Basically, the user enters a search term, which searches our mySQL db, and if the search matches with a part number or description, it will output those results. My jam is that I would like the output to highlight the matching input, which I've gotten to work 95% correct.
for (var i=0;i<arr.length;i++) // Loop through results to indicate highlighting
{
// Test condition to see if input matches part number
if (this.sInp.toLowerCase()==arr[i].value.toLowerCase()) // Tests to see if input matches part number and highlights input to match
{
var val = arr[i].value;
var st = val.toLowerCase().indexOf( this.sInp.toLowerCase());
var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);
var span = _b.DOM.cE("span", {}, output, true);
if (arr[i].value != "")
{
var br = _b.DOM.cE("br", {}); // Inserts line break
span.appendChild(br);
var small = _b.DOM.cE("small", {}, arr[i].info); // Produces second line (info or value)
span.appendChild(small);
}
var a = _b.DOM.cE("a", { href:"#" });
var tl = _b.DOM.cE("span", {className:"tl"}, " ");
var tr = _b.DOM.cE("span", {className:"tr"}, " ");
a.appendChild(tl);
a.appendChild(tr);
a.appendChild(span);
a.name = i+1;
// Modified to submit on click
a.onclick = function () { pointer.setHighlightedValue();
var formName = (pointer.oP.whereSubmit);
if (formName != null) {
var form = document.getElementById(formName);
form.submit();
}
return false; };
// End submit modification
a.onmouseover = function () { pointer.setHighlight(this.name); };
var li = _b.DOM.cE( "li", {}, a );
ul.appendChild( li );
}
else // Tests if input matches any part of description and highlights input to match
{
var val = arr[i].info;
var st = val.toLowerCase().indexOf( this.sInp.toLowerCase() );
var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);
var span = _b.DOM.cE("span", {}, output, true);
if (arr[i].info != "")
{
var br = _b.DOM.cE("br", {});
span.appendChild(br);
var small = _b.DOM.cE("small", {}, arr[i].value);
span.appendChild(small);
}
var a = _b.DOM.cE("a", { href:"#" });
var tl = _b.DOM.cE("span", {className:"tl"}, " ");
var tr = _b.DOM.cE("span", {className:"tr"}, " ");
a.appendChild(tl);
a.appendChild(tr);
a.appendChild(span);
a.name = i+1;
// Modified to submit on click
a.onclick = function () { pointer.setHighlightedValue();
var formName = (pointer.oP.whereSubmit);
if (formName != null) {
var form = document.getElementById(formName);
form.submit();
}
return false; };
// End submit modification
a.onmouseover = function () { pointer.setHighlight(this.name); };
var li = _b.DOM.cE( "li", {}, a );
ul.appendChild( li );
}
}
// End Input Highlight
My issue seems to be my conditional statement. Basically, the user's input must match the part number ("value") exactly for it to output the results is a specific format. I would like it to be able to have something similar to mySQL's wildcard ("%") it will search the entire part number ("value") and not just the beginning to find a match.
Sorry if this doesn't make much sense, I will attempt to clarify if needed, but again running on 0 sleep.
Thanks for any help.
I'm working on an auto-suggest for our search function, but I am running into a wall (possibly due to lack of sleep).
Basically, the user enters a search term, which searches our mySQL db, and if the search matches with a part number or description, it will output those results. My jam is that I would like the output to highlight the matching input, which I've gotten to work 95% correct.
for (var i=0;i<arr.length;i++) // Loop through results to indicate highlighting
{
// Test condition to see if input matches part number
if (this.sInp.toLowerCase()==arr[i].value.toLowerCase()) // Tests to see if input matches part number and highlights input to match
{
var val = arr[i].value;
var st = val.toLowerCase().indexOf( this.sInp.toLowerCase());
var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);
var span = _b.DOM.cE("span", {}, output, true);
if (arr[i].value != "")
{
var br = _b.DOM.cE("br", {}); // Inserts line break
span.appendChild(br);
var small = _b.DOM.cE("small", {}, arr[i].info); // Produces second line (info or value)
span.appendChild(small);
}
var a = _b.DOM.cE("a", { href:"#" });
var tl = _b.DOM.cE("span", {className:"tl"}, " ");
var tr = _b.DOM.cE("span", {className:"tr"}, " ");
a.appendChild(tl);
a.appendChild(tr);
a.appendChild(span);
a.name = i+1;
// Modified to submit on click
a.onclick = function () { pointer.setHighlightedValue();
var formName = (pointer.oP.whereSubmit);
if (formName != null) {
var form = document.getElementById(formName);
form.submit();
}
return false; };
// End submit modification
a.onmouseover = function () { pointer.setHighlight(this.name); };
var li = _b.DOM.cE( "li", {}, a );
ul.appendChild( li );
}
else // Tests if input matches any part of description and highlights input to match
{
var val = arr[i].info;
var st = val.toLowerCase().indexOf( this.sInp.toLowerCase() );
var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);
var span = _b.DOM.cE("span", {}, output, true);
if (arr[i].info != "")
{
var br = _b.DOM.cE("br", {});
span.appendChild(br);
var small = _b.DOM.cE("small", {}, arr[i].value);
span.appendChild(small);
}
var a = _b.DOM.cE("a", { href:"#" });
var tl = _b.DOM.cE("span", {className:"tl"}, " ");
var tr = _b.DOM.cE("span", {className:"tr"}, " ");
a.appendChild(tl);
a.appendChild(tr);
a.appendChild(span);
a.name = i+1;
// Modified to submit on click
a.onclick = function () { pointer.setHighlightedValue();
var formName = (pointer.oP.whereSubmit);
if (formName != null) {
var form = document.getElementById(formName);
form.submit();
}
return false; };
// End submit modification
a.onmouseover = function () { pointer.setHighlight(this.name); };
var li = _b.DOM.cE( "li", {}, a );
ul.appendChild( li );
}
}
// End Input Highlight
My issue seems to be my conditional statement. Basically, the user's input must match the part number ("value") exactly for it to output the results is a specific format. I would like it to be able to have something similar to mySQL's wildcard ("%") it will search the entire part number ("value") and not just the beginning to find a match.
Sorry if this doesn't make much sense, I will attempt to clarify if needed, but again running on 0 sleep.
Thanks for any help.