CFWheels Search

So today I thought I would share with you something that became very cool when I implemented the CFWheels framework in my application. Well, I was pleased with it anyway; as follows I am going to describe how I implemented a search function into my CRM across 6 tables!

Controller

In the controller is a fairly basic setup, it goes as follows (wordpress colour coding just does not want to work on cfscripts so this is the best I can be bothered to do):

<cfscript>
	if(NOT isDefined("params.search")){//check there is a search string, if not go back with error
		flashInsert(error="No Search String");
		redirectTo(back=true);
	}
	stringOrig = params.search.string;//Store the original search string, I use it to repopulate the input field

	params.search.string = "'%" & LCASE(params.search.string) & "%'";//this is the MSSQL query string you need, used in with the LIKE operator the '%' act as wild cards - so we look for anything that contains our search string

	if(isDefined("params.search.includes.notes") AND params.search.includes.notes EQ "on")//repeat this section for each table you want to search, this example only searches the notes table, the includes struct is a set of checkboxes for "advanced" search filtering, they all default to "on" i.e. search all
		searchNotes = model("note").findAll(
			where="note LIKE #params.search.string#",
			returnAs="query");
	else searchNotes = QueryNew("");//if we are not to search this table populate the variable with a blank query to avoid errors in the view
</cfscript>

Basically it’s just query stuff, as it should be.

View

The view I had more fun building (I am omitting some javascript and jquery fancy stuff). Here’s the main view file for the above query.

<cfoutput>
	#includePartial(
		partial="searchSection",
		searchResult=searchNotes,
		name="Notes",
		colList=[{title:"Label",display="label"},{title:"Note",display:"fullLeftStrip(note, 130)"},{title:"Title",display="date"}]
	)#
</cfoutput>

Now the magic is in the partial (as you can guess I used the same partial for ALL of my search results queries – one for each query dynamically picking which columns to show). As Follows:

<!---
Params
searchResult - search query to display
name - title of the section
colList = [{title:"",display=""}] - defines the headers and what is displayed
urlformat = {controller, action, keycolumn} - details for the link url (optional)
--->
<div class="contentcontainer">
	<div class="headings">
		<cfoutput><!--- The following is just styling, basically each table outputs in a table with a title and a 'result' count --->
			<h2 class="left">#arguments.name#</h2>
			<p class="notifycount<cfif arguments.searchResult.recordCount> green</cfif>"><a href="" title="" class="notifypop">#arguments.searchResult.recordcount#</a></p>
		</div>
		<div class="panes #arguments.name#">
		</cfoutput>
		<div class="contentbox">
			<table width="100%">
				<thead>
					<tr><!--- output all the columns we want to display as passed to the partial --->
						<cfloop array="#arguments.colList#" index="column">
							<cfoutput><th>#column.title#</th></cfoutput>
						</cfloop>
					</tr>
				</thead>
				<tbody>
					<cfoutput query="arguments.searchResult"><!--- Here we output the actual data --->
						<tr class="editRow" <cfif isDefined("arguments.urlformat") AND isStruct(arguments.urlformat)>href="#URLFor(controller=arguments.urlformat.controller, action=arguments.urlformat.action, key="#Evaluate(arguments.urlformat.keycolumn)#")#"</cfif>><!--- based on what the urlformat we are passed, make the row a 'link' - this requires a javascript onclick to work thou; $("tr[href]").click(function(){window.location = $(this).attr("href")}) --->
							<cfloop array="#arguments.colList#" index="column"><!--- again only show the columns we want to display --->
								<td>#Evaluate(column.display)#</td>
							</cfloop>
						</tr>
					</cfoutput>
				</tbody>
			</table>
		</div>
	</div>
</div>

Note the use of Evaluate() in displaying the column meaning that when you define the display in the includePartial() you can put in any coldFusion you want to. Apart from some styling, jquery effects and the layout that is about it. Before CFWheels this was 2 very large cfm files with a LOT of repeated code, being able to do it this simply makes me a very happy man!

Edit: Got around to adding comments to the code.

About Simeon Cheeseman

I enjoy a wide variety of computer and board games, have a BSc in Computer Science and have played percussion for 18 years.

Posted on February 16, 2012, in CFWheels, ColdFusion and tagged , , , . Bookmark the permalink. 2 Comments.

  1. I wish you had commented on the codes so we can know what each line does exactly. Thanks all the same.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: