简体   繁体   English

从CFC返回结果之前,对AJAX查询执行字符串函数

[英]Perform string functions on an AJAX query before returning the results from the CFC

My page has the alphabet across the top 'A-Z'. 我的页面的顶部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. 当用户单击字母时,我将对CFC进行AJAX调用,该CFC将根据所选字母对缩略图目录进行查询,并返回相应文件名的列表。

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. 当列表返回页面上的函数时,我正在使用split()对文件名执行几个函数以提取某些信息。 For example, the file names are formatted thusly: lastname_firstnameIMG_1234_RGB.jpg 例如,文件名的格式如下: 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... 我的目标是提取我的CFC中的姓,名和号码,以便我可以标记缩略图图像并创建指向要下载的``真实''文件的链接,例如lastname_firstnameIMG_1234_CMYK.tif等。

Originally, I built my AJAX function to work with the returned list using split() but I'm getting errors in IE. 最初,我使用split()构建了AJAX函数以使用返回的列表,但是IE中出现错误。 To mitigate the problem I would rather perform the string functions in my CFC and return the data ready to go. 为了缓解此问题,我宁愿在CFC中执行字符串函数并返回准备就绪的数据。

How do I perform the string functions in the CFC before returning the data struct? 返回数据结构之前,如何在CFC中执行字符串函数?

EDIT This set-up worked famously in Safari and FF but produced an error in IE. 编辑此设置在Safari和FF中享有盛誉,但在IE中产生了错误。 I tried modifying the split() functions and had some success but then, of course, it didn't work properly in FF. 我尝试修改split()函数,并取得了一些成功,但是,当然,它在FF中无法正常工作。 Again, my goal is to eliminate the javascript string mods and do it in the CFC. 同样,我的目标是消除JavaScript字符串mod,并在CFC中进行操作。

Here's my CFC: 这是我的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...): 和我的AJAX函数(包装在document.ready函数中...):

$('.lastname').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() : 对于JavaScript,我将放弃正则表达式文字并简化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> : 这是我在<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 可以使用以下代码在一行代码中完成ColdFusion

ListFirst(list [, delimiters, includeEmptyValues ])

because the _ really acts as a delimiter in your case. 因为_实际上充当您的情况的分隔符。

ListFirst and ListLast are very handy functions. ListFirst和ListLast是非常方便的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM