Stats
Version
1.1
Wheels Compatibility
1.1, 1.0
Downloads
152
Last Updated
November 7, 2010
About Plugins
Plugins allow you to extend or modify default Wheels application behavior. To use, you just drop the zip file into your plugins directory and reload your application.
Read Using and Creating Plugins for more information.
Simple Sorting
Provides methods for sorting any model by an integer field.
Author
Project Home
http://github.com/andybellenie/cfwheels-simple-sorting
Description
This plugin provides methods for sorting any model by an integer field.
Usage/Examples
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.
