CFWheels

Plugins
What you've got loaded..

Simple Sorting 1.1

A plugin for Coldfusion on Wheels by Andy Bellenie

This plugin provides methods for sorting any model by an integer field.

Usage

Add simpleSorting([sortColumn,scope]) to the init of your model to enable the plugin.

  • sortColumm (string, default 'sortOrder') - the column used for sorting (integer)
  • scope (string, default '') - Limits all functions to the scope of the provided column(s) (see 'Scoping' below)
<cffunction name="init">
	<cfset simpleSorting(sortColumn="mySortColumn",scope="myScopeField1,myScopeField2")>
</cffunction>

Inserting a new model

If there is no sort position specified (or a sort position of zero) then the model will be added at the bottom of the sort table during inserts. If you wish to insert at a specific location, set the sort position into the model before calling create() or save(), e.g.

<cfset myModel = model("foo").create(title="bar")>               <!--- inserts at the bottom --->
<cfset myModel = model("foo").create(title="bar", sortOrder=0)>  <!--- inserts at the bottom --->
<cfset myModel = model("foo").create(title="bar", sortOrder=1)>  <!--- inserts at the top --->
<cfset myModel = model("foo").create(title="bar", sortOrder=3)>  <!--- inserts at position three --->

Moving a model

To move a model simply set a new sorting position and call update() or save(). If you specify a value of zero, it will move the model to the bottom of the table, e.g.

<cfset myModel.update()>             <!--- no move --->
<cfset myModel.update(sortOrder=0)>  <!--- moves to the bottom --->
<cfset myModel.update(sortOrder=3)>  <!--- moves to position 3 --->
<cfset myModel.update(sortOrder=1)>  <!--- moves to the top --->

Scoping

By adding one or more columns to the scope argument, you can have multiple independant sorts within a single table, e.g. - a comments table:

id postId sortOrder text
1 1 1 This is the FIRST comment for the FIRST post
1 1 2 This is the SECOND comment for the FIRST post
1 1 3 This is the THIRD comment for the FIRST post
1 2 1 This is the FIRST comment for the SECOND post
1 2 2 This is the SECOND comment for the SECOND post
1 2 3 This is the THIRD comment for the SECOND post

For this example, you would add the scope argument to the initial plugin call, i.e.

<cfset simpleSorting(scope="postId">

This effectively adds a "where postId = #this.postId#" to all sorting functions.

Support

I try to keep my plugins free from bugs and up to date with Wheels releases, but if you encounter a problem please log an issue using the tracker on github, where you can also browse my other plugins.
https://github.com/andybellenie