Last active
October 6, 2015 15:19
-
-
Save evagoras/3ba55d6089dd116325f1 to your computer and use it in GitHub Desktop.
Removes unwanted records from a ColdFusion Query object to help with pagination.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <cfscript> | |
| /** | |
| * @hint Used for pagination, removes unwanted rows from a query. | |
| * @qry The entire query object to be trimmed. | |
| * @skip How many records to remove from the start. | |
| * @take How many records to return after the @skip. -1 gets everything. | |
| */ | |
| query function RemoveRows | |
| ( | |
| required query qry, | |
| numeric skip = 0, | |
| numeric take = -1 | |
| ) | |
| { | |
| // Local the query object. | |
| var qResults = duplicate( arguments.qry ); | |
| // Get the starting point of returned results. | |
| var iStartRow = arguments.skip + 1; | |
| // Get the end row of the returned results. | |
| var iEndRow = arguments.qry.recordcount; | |
| if( arguments.take >= 0 ) { | |
| iEndRow = arguments.skip + arguments.take; | |
| } | |
| /* | |
| * Use a native Java method to remove records, | |
| * looping backwards to avoid indexing issues | |
| */ | |
| var row = qResults.recordcount; | |
| loop from=qResults.recordcount to=1 step=-1 index="row" { | |
| if( row > iEndRow || row < iStartRow ) { | |
| qResults.RemoveRows( | |
| javacast('int', row-1), | |
| javacast('int', 1) | |
| ); | |
| } | |
| } | |
| return qResults; | |
| } | |
| </cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment