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

发布于 2024-11-02 04:27:18 字数 2983 浏览 1 评论 0原文

我的页面顶部“AZ”上有字母。当用户单击某个字母时,我会对 CFC 执行 AJAX 调用,CFC 根据所选字母执行缩略图目录查询并返回相应文件名的列表。

当列表返回到页面上的函数时,我使用 split() 对文件名执行多个函数以提取某些信息。例如,文件名的格式如下: lastname_firstnameIMG_1234_RGB.jpg

我的目标是提取 CFC 中的姓氏、名字和号码,以便我可以标记缩略图并创建指向的链接用于下载的“真实”文件,例如 lastname_firstnameIMG_1234_CMYK.tif 等...

最初,我构建了 AJAX 函数来使用 split() 处理返回的列表,但我'我在 IE 中遇到错误。为了缓解这个问题,我宁愿在 CFC 中执行字符串函数并返回准备就绪的数据。

在返回数据结构之前,如何在 CFC 中执行字符串函数?

编辑 此设置在 Safari 和 FF 中运行良好,但在 IE 中产生错误。我尝试修改 split() 函数并取得了一些成功,但是,当然,它在 FF 中无法正常工作。同样,我的目标是消除 javascript 字符串 mods 并在 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>

和我的 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  
});

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...):

$('.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  
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

止于盛夏 2024-11-09 04:27:18

对于 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);

以下是我在 ... 中执行此操作的方法代码>:

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);

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);
李白 2024-11-09 04:27:18

ColdFusion 部分可以使用一行代码来完成,

ListFirst(list [, delimiters, includeEmptyValues ])

因为 _ 在您的情况下实际上充当分隔符。

ListFirst 和 ListLast 是非常方便的函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文