Skip to content
gregmoser edited this page Aug 4, 2011 · 25 revisions

This component is designed to extend the capabilities of ORM and allow for easily doing the five main query manipulations automatically by using naming conventions and either the form or url scope. These five tools are:

  • Filter
  • Range
  • Search (With Custom weights per column)
  • Ordering
  • Paging

##Easy Setup The only thing that you will need from the souce code is the SmartList.cfc, copy that file to any folder in your project. For the purposes of this demo lets just pretend that everything is in the root directory.

Next you create an instance of the smart list in your code and initiate it with the entityName of the object you would like a list of, as well as the scope where modification variables are expected, that scope should be passed as the 'rc' variable. For example:

<cfset SmartList = new SmartList(entityName="Product", data=url) />

Once the smart list is setup, all you need to do to use it is loop over the array provided by the getPageRecords() function. For example:

<cfoutput>
<table>
<cfloop array="#SmartList.getPageRecords()#" index="Product">
    <tr>
        <td>#Product.getProductName()#</td>
        <td>#Product.getProductPrice()#</td>
        <td>#Product.getBrand().getBrandName()#</td>
        <td>#Product.getProductYear()#</td>
    </tr>
</cfloop>
</table>
</cfoutput>

Thats IT!! Now all you need to do is pass it variables via form or url scope. See usage for examples.

##Usage

Filters: these can be executed by adding the following formated variable to your url of form scope: "F:propertyName=value". For any filter you can add multiple values by separating with a comma (,). In addition any sub properties of a sub entity can be used by seperating the sub entity with an underscore. Here are some url examples:

  • /index.cfm?F:productName=Cup
  • /index.cfm?F:productName=Cup,Book
  • /index.cfm?F:brand_brandName=Rolex

Ranges: these can be executed by adding the following formated variable to your url of form scope: "R:propertyName=lowerValue^upperValue". This is very similar to a form other than there will always be two values that must be numeric, and the property name is prefixed by an "R". Here are some url examples:

  • /index.cfm?R_productPrice=50,80
  • /index.cfm?R_productYear=2009,2010

Searching: this can be executed by adding the following formated variable to your url of form scope: "Keywords". The Smart List will automatically separate multiple words and individual search on each word in the phrase. In addition, to take advantage of this feature you will need to setup keyword weights directly after you initiate you Smart List. Again you can identify sub entities using the underscore. Here is an example of how to do that:

<cfset SmartList = new SmartList(entityName="Product", rc=url) />
<cfset SmartList.addKeywordProperty(propertyIdentifier="productName", weight=3) />
<cfset SmartList.addKeywordProperty(propertyIdentifier="productYear", weight=9) />
<cfset SmartList.addKeywordProperty(propertyIdentifier="productDescription", weight=1) />
<cfset SmartList.addKeywordProperty(propertyIdentifier="brand_brandName", weight=5) />

Ordering: this can be executed by adding the following formated variable to your url of form scope: "OrderBy=propertyName|A". In addition, multiple properties can be used for ordering in the order that they appear in the OrderBy value that is again delimited by a comma (,). Also, you can either sort ascending or descending by using the |A or |D after the propertyName. Here are some examples:

  • /index.cfm?OrderBy=productYear|A
  • /index.cfm?OrderBy=productYear|D
  • /index.cfm?OrderBy=productYear|A,productName|D

Paging: this can be handled a number of ways; setting the starting entity or setting the current page, and then also setting the number of entities per page. Here are some examples:

  • /index.cfm?P:start=11 Set the starting entity to 11
  • /index.cfm?P:show=20 Set the number of entities to be displayed to 20
  • /index.cfm?P:current=3 Set the starting/current page to 3
  • /index.cfm?P:current=3&P:show=20&
  • /index.cfm?P:start=21&P:show=20&

Advanced Usage

In addition to utilizing the getPageRecords(), you can also use the getRecords() function. This will return an array of all the records. This works well in conjunction with addSelect() function. Below is an example of generating a drop down list with this SmartList utility.

<cfset smartList = createObject("component", "SmartList").init(entityName="Art") />
<cfset smartList.addSelect("artid", "id") />
<cfset smartList.addSelect("artname", "name") />
<cfset smartList.addFilter("artist_firstname", "Aiden") />
<select>
	<cfloop array="#smartList.getRecords()#" index="Art" >
		<option value="#Art.ID#">#Art.Name#</option>
	</cfloop>
</select>

Clone this wiki locally