Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigger and Sort and Toggles Between Sorts

Status
Not open for further replies.

scripter73

Programmer
Joined
Apr 18, 2001
Messages
421
Location
US
Hi,

I have a page that displays data from an array. The array has 5 fields:

Binder/Policy#
Date
Insured
Account#
Amount

The data is initially displayed sorted by Insured, then by Date. I call this one SORT 1.
My SORT 2 is displayed sorted by Date, then by Insured.


This is my goal. I want to display my SORT 1 data, and then have the
user click on something (preferably) the header, and then display the other sort (SORT 2), and be
able to switch back and forth between the two based on how the user determines the sort.

I'm having a hard time figuring out where to start. Here are a few of my issues:

1) I'd like to make the header look like "links", but not behave like links, because I
don't want to redirect the page. Therefore, my <a href=&quot;&quot; action=&quot;&quot;> will look sort of
funky.

2) I'll probably have to recall my current page with something like a (this) pointer.

If someone could point me in the right direction, I'd be eternally grateful.

To view my pages, goto the following:




Click on the Sweep Bank Reconciliation link to get to:




Thanks,
scripter73




Change Your Thinking, Change Your Life.
 
Well, making your headers clickable without making them behave like links is simply a matter of assigning the right style attributes to them. You'd just need to assign an onClick event handler to either the <A> or the <TD> containing the <A>. That would be up to you.

There are a few ways you could handle this.
1. Make all the headers a link that just reloads the page with the sort option. You could make the default sort = date then if the user clicks on the &quot;name&quot; header, they're taken right back to the same page, but now sort = name. Very simple solution...calls for a lot of interface between client and server.

2. You could write a script that does the same thing without making trips to the server, buy creating a JavaScript array and sorting the array right on the client's machine, then rebuilding the table accordingly. This is no small task.

There are other ways, but hopefully this will spark your thinking. Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 
Here, scripter73!

thread232-194717 Calista :-X
Jedi Knight,
Champion of the Force
 
Thanks KevinFSI and Calista for your posts.

Calista, I have HUGE questions. I read the thread. Thanks for the wealth of info.

I have no problem admitting that I'm not up to date on the usage of cookies in Cold Fusion nor am I completely familiar with the <cfparam> tag.

Here's my current logic for my program.

HOMEPAGE.CFM calls SWEEP.CFM (has an include for ARRAY_SORT.CFM).

My ARRAY_SORT.CFM page uses an Allaire tag that sorts my array. It has all of the ascending, descending specifications in it, what columns to primary/secondary sort by, etc.

My question is: Can I still use the cookie method as you've indicated with my current sort routine? Or is this method in lieu of my routine?

I guess I just not seeing it, so I'll post what I currently have for my ARRAY_SORT.CFM template:


<html>
<head>
<title>KDL Custom Tag Usage</title>
<script language=&quot;javascript&quot;>
function caller_id(){
alert(&quot;<cfoutput>#cgi.http_referer#</cfoutput>&quot;);
}//function caller_id()
</script>
</head>

<body>

Original Array (Sorted By Lastname, then Date):<BR>
<table cellspacing=0 cellpadding=0 width=100% border=0>
<tr width=100% bgcolor=&quot;#6699cc&quot;>
<td width=10%><font size=3 color=&quot;black&quot;>Binder/Policy#</font></td>
<td width=20% align=&quot;center&quot;><font size=3 color=&quot;black&quot;>Date</font></td>
<td width=30% colspan=1 align=&quot;left&quot;><font size=3 color=&quot;black&quot;>Insured</font></td>
<td width=20% align=&quot;right&quot;><font size=3 color=&quot;black&quot;>Account#</font></td>
<td width=20% align=&quot;right&quot;><font size=3 color=&quot;black&quot;>Amount</font></td>
</tr>

<cfloop from=1 to=#ArrayLen(bankArray)# index=&quot;i&quot;>
<cfif (#i# mod 2) eq 0>
<tr bgcolor=&quot;silver&quot; width=100%>
<cfelse>
<tr bgcolor=&quot;ffffff&quot; width=100%>
</cfif>
<cfloop from=1 to=5 index=&quot;j&quot;>
<cfset #grand_total# = #grand_total# + #bankArray[5]#>
<cfif (#j# eq 5) or (#j# eq 4)>
<td align=&quot;right&quot; border=0 nowrap>
<cfelseif (#j# eq 2)>
<td align=&quot;center&quot; border=1>
<cfelse>
<td border=0>
</cfif>
<CFOUTPUT>#bankArray[j]#</CFOUTPUT>
</td>
</cfloop>
</tr>
</cfloop>
</table>
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr></tr>
<tr width=100% align=&quot;right&quot; bgcolor=&quot;#6699CC&quot;><td>Grand Total: <cfoutput>#grand_total#</cfoutput><td></td></tr>
</table>

<br><br><br>

<!--- Call the tag ---> (this is an Allaire custom tag to perform the sort)
<CF_MD_Array_Sort
Sort_Order= 2,3
Array_Name = #bankArray#
Asc_Desc = 'Asc'
Sort_Type = 'text'>


<!--- <BR>Sorted Array by columns 2 then 3 with CASE sensitivity:<BR> --->
Date-Sorted Array (Sorted By Date, then Lastname):<BR>
<table cellspacing=0 cellpadding=0 width=&quot;100%&quot; border=0>
<tr width=100% bgcolor=&quot;#6699cc&quot;>
<td width=10%><font size=3 color=&quot;black&quot;>Binder/Policy#</font></td>
<td width=20% align=&quot;center&quot;><font size=3 color=&quot;black&quot;>Date</font></td>
<td width=30% colspan=1 align=&quot;left&quot;><font size=3 color=&quot;black&quot;>Insured</font></td>
<td width=20% align=&quot;right&quot;><font size=3 color=&quot;black&quot;>Account#</font></td>
<td width=20% align=&quot;right&quot;><font size=3 color=&quot;black&quot;>Amount</font></td>
</tr>
<cfloop from=1 to=#ArrayLen(bankArray)# index=&quot;i&quot;>
<cfif (#i# mod 2) eq 0>
<tr bgcolor=&quot;silver&quot; width=100% border=0>
<cfelse>
<tr bgcolor=&quot;ffffff&quot; width=100% border=0>
</cfif>
<cfloop from=1 to=5 index=&quot;j&quot;>
<cfif (#j# eq 5)>
<cfset #grand_total# = #grand_total# + #tmparray[j]#>
</cfif>
<cfif (#j# eq 5) or (#j# eq 4)>
<td align=&quot;right&quot; border=1 nowrap>
<cfelseif (#j# eq 2)>
<td align=&quot;center&quot; border=1>
<cfelse>
<td border=1>
</cfif>
<cfif (#j# eq 5)>
<cfoutput>#NumberFormat(tmparray[j],&quot;999,999.99&quot;)#</cfoutput>
<cfelse>
<cfoutput>#tmparray[j]#</cfoutput>
</cfif>
</td>
</cfloop>
</tr>
</cfloop>
</table>
<cfset #grand_total# = #NumberFormat(grand_total,&quot;999,999.99&quot;)#>
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr></tr>
<tr width=100% align=&quot;right&quot; bgcolor=&quot;#6699CC&quot;><td>Grand Total: <cfoutput>#grand_total#</cfoutput><td></td></tr>
</table>


</body>
</html>


The above is what you're seeing on the page that I previously posted.

I'm really confused. Any light you can shed is appreciated greatly.

Thanks again,
scripter73
Change Your Thinking, Change Your Life.
 
Yes, you can definitely use this cookie method, and, you can use any sort routine you want. I wound up writing my own. All CFPARAM does, is create a variable. You can give it a default or not. It keeps you from erroring out if the variable is referenced, but was not yet given a value by a CFSET.

OK, this code goes somewhere near the top of your page that displays your info:

<CFPARAM name=&quot;cookie.SortOrder&quot; default=&quot;A&quot;>
<CFPARAM name=&quot;cookie.oSort&quot; default=&quot;1&quot;>
<CFPARAM name=&quot;newSort&quot; default=&quot;&quot;>
<CFPARAM name=&quot;origSort&quot; default=&quot;&quot;>

<CFIF IsDefined(&quot;URL.newsort&quot;)>
<CFSET newsort = URL.newsort>
</CFIF>
<CFIF IsDefined(&quot;URL.origsort&quot;)>
<CFSET origsort = URL.origsort>
</CFIF>

<CFIF len(origsort) gt 0>
<CFIF cookie.oSort is &quot;#newsort#&quot;>
<CFIF cookie.SortOrder is &quot;A&quot;>
<CFCOOKIE name=&quot;SortOrder&quot; value=&quot;D&quot; expires=&quot;NEVER&quot;>
<CFELSE>
<CFCOOKIE name=&quot;SortOrder&quot; value=&quot;A&quot; expires=&quot;NEVER&quot;>
</CFIF>
</CFIF>
<CFCOOKIE name=&quot;oSort&quot; value=&quot;#origsort#&quot; expires=&quot;NEVER&quot;>
</CFIF>

Now, this is my header for the table that displays the data:

<CFOUTPUT>
<DIV ALIGN=&quot;center&quot;>
<TABLE CLASS=&quot;Toolbar&quot;>
<TR>
<TD><A HREF=&quot;DiscussionIndex.cfm?Forum=#Forum#&origsort=1&newsort=1&quot;><SPAN CLASS=&quot;BigBlack&quot;>Thread</SPAN></A></TD>
<TD><A HREF=&quot;DiscussionIndex.cfm?Forum=#Forum#&origsort=2&newsort=2&quot;><SPAN CLASS=&quot;BigBlack&quot;>Messages</SPAN></A></TD>
<TD><A HREF=&quot;DiscussionIndex.cfm?Forum=#Forum#&origsort=3&newsort=3&quot;><SPAN CLASS=&quot;BigBlack&quot;>Last Post</SPAN></A></TD>
</TR>
</TABLE>
</DIV>
</CFOUTPUT>

Does this help? Calista :-X
Jedi Knight,
Champion of the Force
 
Hi scripter73,

I'm entering this discussion kinda toward the end, but I had a similar issue and chose a different approach that I thought I'd toss out there. I put the information into a form and made the header columns submit buttons with the value of the database field being passed. That way, the user doesn't get to see the link at all.

Using this, you could either continue as you are with CF doing the sorting or, as I did, just redo the request to the database. Though this is more load on the database, it can sometimes be faster than having the data reordered by Cold Fusion.

The form submits back to the same page but the value of the sort order is supplied by the form.

For example, the query looks a little like this:

<CFQUERY>
Select a,b,c from table1 where a='#Form.a#' Order By #Form.SortOrder#
</CFQUERY>
 
Thanks MarFisk, I'll interpret your way of doing it.

This is to Calista (or to whoever may know this answer).

Calista, I took your advice, and I'm ALMOST there.

The only problem is that I cannot get my arrays to toggle back and forth based on what the user selected. It just shows me the same sort no matter what.

My idea is that when the user clicks on the link, I can add a sorttype to the url and toggle between my displays based on the url variable.

Here's what I mean:


<html>
<head>
<title>Custom Tag Usage</title>
</head>

<body>

<CFPARAM Name=&quot;SESSION.SORT_TYPE&quot; Default=&quot;INSURED&quot;>
<CFSET SESSION.SORT_TYPE = &quot;INSURED&quot;>

<cfif IsDefined(&quot;url.sorttype&quot;) eq &quot;FALSE&quot;>
<cfset #url.sorttype# = &quot;insured&quot;>
<cfset #session.type# = #url.sorttype#>
</cfif>

<cfoutput></cfoutput>

<!--- Cookie Implementation. --->
<CFPARAM name=&quot;cookie.SortOrder&quot; default=&quot;A&quot;>
<CFPARAM name=&quot;cookie.oSort&quot; default=&quot;1&quot;>
<CFPARAM name=&quot;newSort&quot; default=&quot;&quot;>
<CFPARAM name=&quot;origSort&quot; default=&quot;&quot;>

<CFIF IsDefined(&quot;URL.newsort&quot;)>
<CFSET newsort = URL.newsort>
</CFIF>
<CFIF IsDefined(&quot;URL.origsort&quot;)>
<CFSET origsort = URL.origsort>
</CFIF>

<CFIF len(origsort) gt 0>
<CFIF cookie.oSort is &quot;#newsort#&quot;>
<CFIF cookie.SortOrder is &quot;A&quot;>
<CFCOOKIE name=&quot;SortOrder&quot; value=&quot;D&quot; expires=&quot;NEVER&quot;>
<CFELSE>
<CFCOOKIE name=&quot;SortOrder&quot; value=&quot;A&quot; expires=&quot;NEVER&quot;>
</CFIF>
</CFIF>
<CFCOOKIE name=&quot;oSort&quot; value=&quot;#origsort#&quot; expires=&quot;NEVER&quot;>
</CFIF>

<form name=&quot;sortform&quot; action=&quot;submit&quot; method=&quot;get&quot;>
<CFIF #url.sorttype# eq &quot;insured&quot;>
Original Array (Sorted By Lastname, then Date):<BR>
<table cellspacing=0 cellpadding=0 width=100% border=0>
<tr width=100% bgcolor=&quot;#ffffff&quot;>
<td width=10%><font size=3 color=&quot;black&quot;>Binder/Policy#</font></td>
<td width=20% align=&quot;center&quot;><font size=3 color=&quot;black&quot;><A HREF=&quot;sweep.cfm?Forum=#Forum#&origsort=2&newsort=2&sorttype=date&quot;>Date</A></font></td><!--- Here's where it should change. I added sorttype to the url. I can't get the url.sorttype to resolve to switch displays.--->
<td width=30% colspan=1 align=&quot;left&quot;><font size=3 color=&quot;black&quot;><A HREF=&quot;sweep.cfm?Forum=#Forum#&origsort=1&newsort=1&sorttype=insured&quot;>Insured</A></font></td>
<td width=20% align=&quot;right&quot;><font size=3 color=&quot;black&quot;>Account#</font></td>
<td width=20% align=&quot;right&quot;><font size=3 color=&quot;black&quot;>Amount</font></td>
</tr>

<cfloop from=1 to=#ArrayLen(bankArray)# index=&quot;i&quot;>
<cfif (#i# mod 2) eq 0>
<tr bgcolor=&quot;silver&quot; width=100%>
<cfelse>
<tr bgcolor=&quot;ffffff&quot; width=100%>
</cfif>
<cfloop from=1 to=5 index=&quot;j&quot;>
<cfset #grand_total# = #grand_total# + #bankArray[5]#>
<cfif (#j# eq 5) or (#j# eq 4)>
<td align=&quot;right&quot; border=0 nowrap>
<cfelseif (#j# eq 2)>
<td align=&quot;center&quot; border=1>
<cfelse>
<td border=0>
</cfif>
<CFOUTPUT>#bankArray[j]#</CFOUTPUT>
</td>
</cfloop>
</tr>
</cfloop>
</table>
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr></tr>
<tr width=100% align=&quot;right&quot; bgcolor=&quot;#6699CC&quot;><td>Grand Total: <cfoutput>#grand_total#</cfoutput><td></td></tr>
</table>

<br><br><br>
<CFELSEIF #url.sorttype# EQ &quot;date&quot;>

<!--- Call the tag --->
<CF_MD_Array_Sort
Sort_Order= 2,3
Array_Name = #bankArray#
Asc_Desc = 'Asc'
Sort_Type = 'text'>


<!--- <BR>Sorted Array by columns 2 then 3 with CASE sensitivity:<BR> --->
Date-Sorted Array (Sorted By Date, then Lastname):<BR>
<table cellspacing=0 cellpadding=0 width=&quot;100%&quot; border=0>
<tr width=100% bgcolor=&quot;#6699cc&quot;>
<td width=10%><font size=3 color=&quot;black&quot;>Binder/Policy#</font></td>
<td width=20% align=&quot;center&quot;><font size=3 color=&quot;black&quot;>Date</font></td>
<td width=30% colspan=1 align=&quot;left&quot;><font size=3 color=&quot;black&quot;>Insured</font></td>
<td width=20% align=&quot;right&quot;><font size=3 color=&quot;black&quot;>Account#</font></td>
<td width=20% align=&quot;right&quot;><font size=3 color=&quot;black&quot;>Amount</font></td>
</tr>
<cfloop from=1 to=#ArrayLen(bankArray)# index=&quot;i&quot;>
<cfif (#i# mod 2) eq 0>
<tr bgcolor=&quot;silver&quot; width=100% border=0>
<cfelse>
<tr bgcolor=&quot;ffffff&quot; width=100% border=0>
</cfif>
<cfloop from=1 to=5 index=&quot;j&quot;>
<cfif (#j# eq 5)>
<cfset #grand_total# = #grand_total# + #tmparray[j]#>
</cfif>
<cfif (#j# eq 5) or (#j# eq 4)>
<td align=&quot;right&quot; border=1 nowrap>
<cfelseif (#j# eq 2)>
<td align=&quot;center&quot; border=1>
<cfelse>
<td border=1>
</cfif>
<cfif (#j# eq 5)>
<cfoutput>#NumberFormat(tmparray[j],&quot;999,999.99&quot;)#</cfoutput>
<cfelse>
<cfoutput>#tmparray[j]#</cfoutput>
</cfif>
</td>
</cfloop>
</tr>
</cfloop>
</table>
<cfset #grand_total# = #NumberFormat(grand_total,&quot;999,999.99&quot;)#>
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr></tr>
<tr width=100% align=&quot;right&quot; bgcolor=&quot;#6699CC&quot;><td>Grand Total: <cfoutput>#grand_total#</cfoutput><td></td></tr>
</table>
</CFIF>
</form>
</body>
</html>



Thanks so much. Any help's appreciated.

scripter73
Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top