I added a new page today (Purpose of Blog) and while doing so realized that the Navigation is sorted using the Page Title by default.

Since my blog is using a webhost and not a local computer, I thought best thing for me to do is provide the admin an easy way to specify sort order for the navigation links. This is actually really easy to do, no complex code needed. I thought I would share it anyway.

First, the database has a tblBlogPages Table, that needs to be modified. Add a column called sortorder of type numeric/int (whatever your database takes for a number value). You can set the default of the value to 0.

Once we are done with that, we will change the "Pages" admin functions to display a sort order on the main screen and allow you to specify a sort order when you either add/edit the Page.

First changes are in the CFC that gets all the page information from the database. The file is page.cfc(found under org\camden\blog)

Page.cfc Functions: The following functions are modified:

getPage
- Add the sortorder field to the select and to the return struct.

view plain print about
1<cfquery name="q" datasource="#variables.dsn#" username="#variables.username#" password="#variables.password#">
2    select        id, blog, title, alias, body, sortorder
3    from        tblblogpages
4    where        id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.id#" maxlength="35">
5    and        blog = <cfqueryparam cfsqltype="cf_sql_varchar" value="#variables.blog#" maxlength="50">
6    </cfquery>
7
8    <cfif q.recordCount>
9        <cfset s.id = q.id>
10        <cfset s.blog = q.blog>
11        <cfset s.title = q.title>
12        <cfset s.alias = q.alias>
13        <cfset s.body = q.body>
14        <cfset s.sortorder = q.sortorder>
15    </cfif>

getPages
- Similarly add the sortorder field to the Select and also to the Order By. Your Order by should be

view plain print about
1order by sortorder, title asc

savePage
- This function is called to either add a new Page or update an existing Page. Very simply, add the sortorder column to arguments (I had it default to 0). Add it to the insert query and add it to the update query.

We are now done with the CFC changes. Now we need to change the two admin pages.

Under admin\ folder there is:

pages.cfm
- This is the Master i.e. Lists the current pages in the system. On this page you will find the following code:

view plain print about
1<cfmodule template="../tags/datatable.cfm" data="#pages#" editlink="page.cfm" label="Pages"
2    linkcol="title" defaultsort="title" defaultdir="asc">

3        <cfmodule template="../tags/datacol.cfm" colname="title" label="Title" />
4        <cfmodule template="../tags/datacol.cfm" colname="url" label="URL" format="url" />
5</cfmodule>

We change it to:

view plain print about
1<!--- We changed the default sort to sortorder so the datatable is sorted properly. kshah --->
2<cfmodule template="../tags/datatable.cfm" data="#pages#" editlink="page.cfm" label="Pages"
3 linkcol="title" defaultsort="sortorder" defaultdir="asc" showSort="Yes" allowUpdate="Yes">

4    <cfmodule template="../tags/datacol.cfm" colname="title" label="Title" />
5    <cfmodule template="../tags/datacol.cfm" colname="url" label="URL" format="url" />
6    <!--- Add the sortorder data column. no need to specify format. kshah --->
7    <cfmodule template="../tags/datacol.cfm" colname="sortorder" label="Sort Order"/>
8</cfmodule>

We changed the default sort on the datatable to use sortorder column. And we added the sortorder column as a datacolumn to the table.

page.cfm
- This is the Detail page which allows you to add/edit Pages. Very simple changes here. Near the top of the page you will see some tags. We need to add one for form.sortorder. Next, you will see the code that calls the savePage function of page CFC. We change it to pass the sortorder.

view plain print about
1<cfset application.page.savePage(url.id, left(form.title,255), left(form.alias,50), form.body, left(Val(form.sortorder),3))>
Then at the bottom of the page is the actual Form that shows up on the page. We need to add an extra row and add the sortorder field. I have my sortorder field set to a maxlength of 3.
view plain print about
1<!--- Add Sort Order Input Field. kshah. --->
2<tr>
3 <td align="right">sort order:</td>
4 <td>
5 <input type="text" name="sortorder" value="#form.sortOrder#" class="txtField" size="3" maxlength="3">
6 </td>
7</tr>

You can download the three changed files Here