Perform string functions on an AJAX query before returning the results from the CFC
My page has the alphabet across the top 'A-Z'. When a user clicks on a letter I perform an AJAX call to a CFC that performs a query of a directory of thumbnail images based on the letter selected and returns a list of the corresponding file names.
When the list is returned to my function on the page I'm using split()
to perform several functions on the file name to pull out certain information. For example, the file names are formatted thusly: lastname_firstnameIMG_1234_RGB.jpg
My goal is to extract the last name, the first name and the number in my CFC so that I can label the thumbnail image and create links to the 'real' files for downloading such as lastname_firstnameIMG_1234_CMYK.tif
etc...
Originally, I built my AJAX function to work with the returned list using split()
but I'm getting errors in IE. To mitigate the problem I would rather perform the string functions in my CFC and return the data ready to go.
How do I perform the string functions in the CFC before returning the data struct?
EDIT This set-up worked famously in Safari and FF but produced an error in IE. I tried modifying the split()
functions and had some success but then, of course, it didn't work properly in FF. Again, my goal is to eliminate the javascript string mods and do it in the CFC.
Here's my CFC:
<cffunction name="List" access="remote" output="no" returntype="struct">
<cfargument name="letter" required="yes" type="string">
<cfset local = StructNew()>
<cfset local.response = StructNew()>
<cfset local.response["error"] = "">
<cfset local.response["message"] = "">
<cfset local.data = #arguments.letter# & "*.jpg">
<cfdirectory
action="list"
directory="#thumbsdir#"
recurse="true"
listinfo="name"
name="qFile"
filter="#local.data#"
/>
<cfset local.response["message"] = #qFile#>
<cfreturn local.response>
</cffunction>
And my AJAX function (which is wrapped in a document.ready function...):
$('.lastn开发者_如何学Goame').click(function(e){
e.preventDefault();
$('#emptymessage').hide();
$('#searching').show();
$('#content').html("");
var alpha = $(this).attr('id');
$.getJSON("cfcengine.cfc?method=List&returnformat=json&queryFormat=column",
{letter:alpha},
function(res, code) {
var s = "";
if(res.message.ROWCOUNT > 0) {
$('#searching').hide();
for(var i=0; i<res.message.ROWCOUNT; i++) {
//This is all the stuff I want to get rid of...
var theFile = res.message.DATA.Name[i]
var theLastName = theFile.split(/_(.+)/)[0];
var theRest = theFile.split(/_(.+)/)[1];
var theFirstNameAll = theRest.split(/_(.+)/)[0];
var theFirstName = theFirstNameAll.split(/(?:IMG)/)[0];
var theImageAll = theRest.split(/_(.+)/)[1];
var theImage = theImageAll.split(/_(.+)/)[0];
var bw = theLastName + '_' + theFirstName + 'IMG_' + theImage + '_BW.jpg';
var rgb = theLastName + '_' + theFirstName + 'IMG_' + theImage + '_RGB.jpg';
//Right now I'm just returning the name to the page until debugging is complete...
s += '<p>' + res.message.DATA.Name[i] + '<\/p>';
}
} else {
var s = "Sorry, nothing matched your search.";
}
$("#content").html(s);
//End response
}
//End getJSON
);
//End plist click function
});
For JavaScript, I would do away with the regex literals and simplify the split()
:
// replace with res.message.DATA.Name[i]
var theFile = 'lastname_firstnameIMG_1234_RGB.jpg';
// ['lastname', 'firstnameIMG', '1234', RGB.jpg']
var pieces = theFile.split('_');
//lastname
var theLastName = pieces[0];
//firstname
var theFirstName = pieces[1].slice(0, pieces[1].length - 3);
//1234
var theImage = pieces[2];
var bw = theLastName + '_' + theFirstName + 'IMG_' + theImage + '_BW.jpg';
var rgb = theLastName + '_' + theFirstName + 'IMG_' + theImage + '_RGB.jpg';
// lastname_firstnameIMG_1234_BW.jpg'
alert(bw);
// lastname_firstnameIMG_1234_RGB.jpg'
alert(rgb);
Here's how I'd do it in <cfscript>...</cfscript>
:
theFile = 'lastname_firstnameIMG_1234_RGB.jpg';
// ['lastname', 'firstnameIMG', '1234', RGB.jpg']
pieces = theFile.split('_');
//lastname
theLastName = pieces[1];
//firstname
theFirstName = left(pieces[2], len(pieces[2]) - 3);
//1234
theImage = pieces[3];
bw = theLastName & '_' & theFirstName & 'IMG_' & theImage & '_BW.jpg';
rgb = theLastName & '_' & theFirstName & 'IMG_' & theImage & '_RGB.jpg';
// lastname_firstnameIMG_1234_BW.jpg'
writeOutPut(bw & "<br/>");
// lastname_firstnameIMG_1234_RGB.jpg'
writeOutPut(rgb);
The ColdFusion piece of this could be done in a single line of code using
ListFirst(list [, delimiters, includeEmptyValues ])
because the _ really acts as a delimiter in your case.
ListFirst and ListLast are very handy functions.
精彩评论