【问题标题】:Perform string functions on an AJAX query before returning the results from the CFC在从 CFC 返回结果之前对 AJAX 查询执行字符串函数
【发布时间】:2012-11-30 06:28:25
【问题描述】:

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

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

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

最初,我使用split() 构建了我的 AJAX 函数来处理返回的列表,但我在 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  
});

【问题讨论】:

  • 嗯...,-1?这是一个没有足够细节的畸形问题吗?只是好奇,所以我不会重复冒犯。
  • 不知道为什么-1,我已经标记了
  • 是的...+1..我很好奇为什么有人想对此投反对票...
  • @Richard,谢谢。我也不确定。我唯一能想到的就是看起来我正试图让某人为我编写代码。我不是,我只是不确定有什么好方法可以继续。我已经阅读了几篇关于split() 的帖子在 IE 中无法正常工作,所以我认为这可能与 CF 用户相关。

标签: javascript regex string coldfusion cfc


【解决方案1】:

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

这是我在&lt;cfscript&gt;...&lt;/cfscript&gt; 中的做法:

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

【讨论】:

  • 那么,您会选择 JavaScript 而不是 &lt;cfscript&gt;?我想我对使用 JavaScript 有点害羞,因为它在浏览器中的不规则性。
  • @Ofeargall:我只在 IE 9 中尝试过 JS,它运行良好。所以我会选择阻力最小的路径并从 JS 更改开始,看看效果如何。如果没有,请切换到 CF。
【解决方案2】:

其中的 ColdFusion 部分可以使用

在一行代码中完成
ListFirst(list [, delimiters, includeEmptyValues ])

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多