• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help with UDF

Participant ,
Feb 06, 2007 Feb 06, 2007

Copy link to clipboard

Copied

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!
TOPICS
Advanced techniques

Views

303

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 06, 2007 Feb 06, 2007

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Feb 06, 2007 Feb 06, 2007

Copy link to clipboard

Copied

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 .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 13, 2007 Feb 13, 2007

Copy link to clipboard

Copied

Thank you, I will investigate and report back.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 19, 2007 Apr 19, 2007

Copy link to clipboard

Copied

LATEST
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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation