Skip to main content
Inspiring
February 6, 2007
Question

Help with UDF

  • February 6, 2007
  • 2 replies
  • 392 views
Howdy Folks,

I have a search page that returns a list of matches based on the user input. I thought it would be slick to show the user's search parameter in RED, inline in the returned results. I got this to work, but had issues with capitalization (inside the string, as opposed to only when the string began with the searched term. Also the code to do this was silly long and confusing. I wanted to create a UDF that would do the job without the massive switching in my page to page code. So far I am converting the returned results as such;
(see below)
And since there are several potential fields searched on (and returned) I have to adjust each seperately.
I would love to just have #red(str)# do the job in the back ground, but when I tried to create a UDF I just got the value reoutput with not style adjustment. Also I'm not sure how i could incorporate a "placement" checker to UCASE only the first letter when the search string IS the beginning of the result.

Thanks for any insight!
This topic has been closed for replies.

2 replies

Inspiring
February 13, 2007
Thank you, I will investigate and report back.
Inspiring
April 19, 2007
I have succeeded in creating a *nearly* perfect UDF for this task. In my case #busName# is a value returned from the first query (for a list of businesses) and #memberTrim# is the (trimmed) user provided search term. This UDF reformats the memberTrim to start with a Capital Letter, and appear Red.

I feel like this UDF could be trimmed down - It does work as-is, but I think I may have redundant or useless code in it. If anyone discovers a cleaner approach, please advise.

The resultant output NBN (New Business Name) is interjected into the #busName# via the UDF wrap;
Participating Frequently
February 6, 2007
One method would be to perform client side highlighting (without the upper case bit).

The below works on Internet Explorer and FireFox. (reference http://www.javascriptkit.com/javatutors/dynamiccontent4.shtml )

With this method, you will have to tokenize the search terms and trap out invalid regex patterns, such as stripping out undesirables from the search criteria and then creating the regEx pattern by using each word separated by the OR pipe ("|")


<cfset searchTerm = "test|foo|bar">
<style>
.highlite{
color:#3300FF;
background:#FFCC33;
}
</style>
<cfoutput>
<script language="javascript">
function colorTerm(){
var re = /(#searchTerm#)/gi;
document.body.innerHTML = document.body.innerHTML.replace(re, "<span class='highlite'>$1</span>");
}
</script>
</cfoutput>
<html>
<body onload="colorTerm();">
this is a bunch of text. and will test the highlighting of the text. FOO should be as well as bar and foobar.
</body>
</html>
February 7, 2007
The javascript's a pretty good way to go.

An all CF approach is here: http://www.sagewire.org/advanced-cfml-techniques/Need-help-with-advanced-search-please-78914.aspx .