|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Sort #FORM# by value?
Author: Morgan Senkal
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163869
It's working perfect Charlie, thanks. One small change I made on the second line
of the function:
Changed: var stForm = f;
Changed To: var stForm = Duplicate(f);
I wanted to leave the original #form# intact for further processing. On testing
the code I found that leaving out the Duplicate() function did not break the
reference.
----- Excess quoted text cut - see Original Post for more -----
Author: Charlie Griefer
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163867
Morgan:
This seems to do it:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<cfscript>
function sortForm(f) {
var sortArray = arrayNew(2);
var stForm = f;
var keys = structKeyArray(stForm);
var sortedKeys = "";
for (i=1; i LTE #arrayLen(keys)#; i=i+1) {
if (NOT isNumeric(stForm[keys[i]])) {
structDelete(stForm, keys[i]);
}
}
sortedKeys = structSort(stForm, 'numeric');
for (i=1; i LTE #arrayLen(sortedKeys)#; i=i+1) {
sortArray[arrayLen(sortArray)+1][1] = sortedKeys[i];
sortArray[arrayLen(sortArray)][2] = stForm[sortedKeys[i]];
}
return sortArray;
}
</cfscript>
</head>
<body>
<cfparam name="form.field1" default="foo">
<cfparam name="form.field2" default="bar">
<cfparam name="form.field3" default="12">
<cfparam name="form.field4" default="foobar">
<cfparam name="form.field5" default="4">
<cfparam name="form.field6" default="barfoo">
<cfparam name="form.field7" default="8">
<cfdump var="#form#" label="this is the original form">
<cfset formSorted = sortForm(form)>
<cfdump var="#formSorted#" label="this should be sorted">
</body>
</html>
let me know if that works out.
Charlie
Author: Morgan Senkal
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163866
Ah, thanks for clearing that up. I was testing kArray[i] instead
of stForm[kArray[i]]. As for the keys, as I mentioned above there should be no
chance of having duplicate numeric values returned, so I can just match up the
values to their keys in the original form now that they are in order.
----- Excess quoted text cut - see Original Post for more -----
Author: Charlie Griefer
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163863
if you want it to insert only numeric values...the UDF should look like:
<cfscript>
function sortForm(stForm) {
var formValueArray = arrayNew(1);
var kArray = structKeyArray(stForm);
for (i=1; i LTE arrayLen(kArray); i=i+1) {
if (isNumeric(stForm[kArray[i]])) {
formValueArray[arrayLen(formValueArray)+1] = stForm[kArray[i]];
}
}
temp = arraySort(formValueArray, 'numeric');
return formValueArray;
}
</cfscript>
this does NOT yet retain the form field name. still not sure how you intend
to handle that.
----- Excess quoted text cut - see Original Post for more -----
the cfdump I'm getting back for the FORM element:
Author: Morgan Senkal
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163862
Doesn't matter, as long as I can access the name in relation to the value. On
second thought, I think the way I coded the form, I don't believe that it is
possible for there to be repeat values. (all the form values in question are
from checkboxes, so I can control the values sent). So actually the code you
started will work, as long as it only includes those values that are numeric (and
not empty), so I can do a numeric sort.
----- Excess quoted text cut - see Original Post for more -----
Author: Charlie Griefer
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163860
when you say you need to retain the original key name...how so? a 2d array?
a structure?
Charlie
> I suppose I should have given more information, sorry :-( But to clarify:
I only want to pull out FORM values which are numeric and order those. I
also need to somehow retain the key name associated to each value. It's a
good start tho thanks much Charlie! Perhaps you and I can fiddle with it
and see where we get...
----- Excess quoted text cut - see Original Post for more -----
Author: Morgan Senkal
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163857
Ok, for starters I tried adding a simple if statement like so:
if (isNumeric(kArray[1]))
{
formValueArray[arrayLen(formValueArray)+1] = stForm[kArray[i]];
}
But apparently it doesn't recognize any of the values as numbers. This is the
cfdump I'm getting back for the FORM element:
ACTIONDATE1
ACTIONDATE2
ACTIONITEMDESCRIPTION
ACTIVITYTYPE
ACTIVITYTYPEORDER 5
BUSINESSUNIT
BUSINESSUNITORDER 3
DATE1 5/13/2004
DATE2 5/20/2004
FIELD1 foo
FIELD2 bar
FIELD3 foobar
FIELD4 barfoo
FIELDNAMES
ISSUEDESCRIPTIONORDER,ISSUEDESCRIPTION,ISSUEOUTCOME,ACTIONITEMDESCRIPTION,GENTEXT,ACTIVITYTYPEORDER,ACTIVITYTYPE,DATE1,DATE2,TRIBEORDER,TRIBE,BUSINESSUNITORDER,BUSINESSUNIT,PARTICIPANT,ISSUECATEGORY,PRIORITY,PROJECTORDER,PROJECT,ISSUESTATUSORDER,ISSUESTATUS,ACTIONDATE1,ACTIONDATE2,QUERYNUMBER,QUERYTITLE,QUERY0
GENTEXT
ISSUECATEGORY
ISSUEDESCRIPTION
ISSUEDESCRIPTIONORDER 9
ISSUEOUTCOME
ISSUESTATUS
ISSUESTATUSORDER 10
PARTICIPANT
PRIORITY
PROJECT
PROJECTORDER 4
QUERY0 Run Query
QUERYNUMBER 0
QUERYTITLE Custom Query
TRIBE
TRIBEORDER 1
So there are indeed numbers in there, but the question of whether they are
'numeric' objects is apparently no. Not sure the best way to proceed at this
point...
----- Excess quoted text cut - see Original Post for more -----
Author: Morgan Senkal
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163855
I suppose I should have given more information, sorry :-( But to clarify: I
only want to pull out FORM values which are numeric and order those. I also need
to somehow retain the key name associated to each value. It's a good start tho
thanks much Charlie! Perhaps you and I can fiddle with it and see where we
get...
----- Excess quoted text cut - see Original Post for more -----
Author: Charlie Griefer
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163745
Hi Morgan:
Might be a better/more efficient way, but threw together a quick UDF
that seems to work:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<cfscript>
function sortForm(stForm) {
var formValueArray = arrayNew(1);
var kArray = structKeyArray(stForm);
for (i=1; i LTE arrayLen(kArray); i=i+1) {
formValueArray[arrayLen(formValueArray)+1] = stForm[kArray[i]];
}
temp = arraySort(formValueArray, 'text');
return formValueArray;
}
</cfscript>
</head>
<body>
<cfparam name="form.field1" default="foo">
<cfparam name="form.field2" default="bar">
<cfparam name="form.field3" default="foobar">
<cfparam name="form.field4" default="barfoo">
<cfdump var="#form#">
<cfset formValueArray = sortForm(form)>
<cfdump var="#formValueArray#">
</body>
</html>
Hope that helps.
Any ideas? You would think that StructSort would be the answer but
nope.
TIA!
-Morgan
Author: Morgan Senkal
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32626#163708
Any ideas? You would think that StructSort would be the answer but nope.
TIA!
-Morgan
|
May 24, 2012
|
Latest Fusion Authority Articles
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||