4 Replies Latest reply on Apr 19, 2007 12:02 PM by bigbrain28

    Help with UDF

    bigbrain28 Level 1
      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!
        • 1. Re: Help with UDF
          c_wigginton Level 1
          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>
          • 2. Re: Help with UDF
            MikerRoo Level 1
            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-789 14.aspx .

            • 3. Re: Help with UDF
              bigbrain28 Level 1
              Thank you, I will investigate and report back.
              • 4. Re: Help with UDF
                bigbrain28 Level 1
                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;