开发者

Catching 'Last Record' in Coldfusion for IE javascript bug

I'm using ColdFusion to pull UK postcodes into an array for display on a Google Map. This happens dynamically from a SQL database, so the numbers can range from 1 to 100+

the script works great, however, in IE (groan) it decides to display one point way off line, over in California somewhere.

I fixed this issue in a previous webapp, this was due to the comma between each array item still being present at the end. Works fine in Firefox, Safari etc, but not IE.

But, that one was using a set 10 records, so was easy to fix.

I just need a little if statement to wrap around my comma to hide it when it hits the last record. I can't seem to get it right. Any tips/suggestions?

here is the line of code in question:

var address = [<cfloop query="getApplicant"><cfif getApplicant.dbHomePostCode GT ""><cfoutput>'#getApplicant.dbHomePostCode#',</cfoutput></cfif> </cfloop>];

Hopefully someone can help with this rather simple request. I'm开发者_JS百科 just having a bad day at the office!


var address = [#ListQualify(ValueList(getApplicant.dbHomePostCode), "'")#]

I notice a <cfif getApplicant.dbHomePostCode GT ""> in your code.

With ListQualify() the empty (NULL or empty string) post codes will not show up in the output, since ColdFusion list functions ignore empty list elements.


EDIT: The previous revision of this answer indicated that empty elements would show up in the result of ListQualify(). This is incorrect, but the first two comments refer to this initial revision.


My approach is a little different, you have to do less setup nor do you have to iterate; it's all done the the cfloop tag.

var address = [
<cfloop from="1" to="#getApplicant.recordcount#" index="i">
<cfif getApplicant.dbHomePostCode GT "">
  <cfoutput>
    '#getApplicant.dbHomePostCode[i]#'
    <cfif i lt getApplicant.recordcount>,</cfif>
  </cfoutput>
</cfif>
</cfloop>
];

Here it is without the breaks, you should be able to paste this into your page and it'll work.

var address = [<cfloop from="1" to="#getApplicant.recordcount#" index="i"><cfif getApplicant.dbHomePostCode GT ""><cfoutput>'#getApplicant.dbHomePostCode[i]#'<cfif i lt getApplicant.recordcount>,</cfif></cfoutput></cfif></cfloop>];


Try this

var address = [<cfset i=0><cfloop query="getApplicant"><cfset i=i+1><cfif getApplicant.dbHomePostCode GT ""><cfoutput>'#getApplicant.dbHomePostCode#'<cfif i LT getApplicant.RecordCount>,</cfif></cfoutput></cfif></cfloop>];

The above code uses an Integer(i) to store the position in the loop, and when it comes to output the comma checks to see if i is Less Than the size of the SQL result. This way it will only output a comma if it is not the last row of the result-set.


Using query.recordcount you can determine if you are at the last line of your query and adjust the output accordingly. Overall Tomalak and Ben Doom have excellent answers, but this one shows how you could have fixed the issue using your original line of thinking.

<cfset var address = "[">
<cfloop query="getApplicant">
    <!--- If not the last row, include the comma --->
    <cfif getApplicant.dbHomePostCode NEQ "" AND getApplicant.currentrow NEQ getApplicant.recordcount>
        <cfset address = address & getApplicant.dbHomePostCode & ",">

    <!--- If last row, omit the comma --->
    <cfelseif getApplicant.dbHomePostCode NEQ "">
        <cfset address = address & getApplicant.dbHomePostCode>
    </cfif>
</cfloop>
<cfset address = address & "];">

<!--- Now we output the string all at once --->
<cfoutput>#address#</cfoutput>


@Tomalok has a good answer. That's probably what I would use.

Before seeing his answer, here's what my answer probably would have been:

<cfset codelist = "">
<cfloop query="getApplicant">
    <cfif len(dbHomePostCode)>
        <cfset codelist = listappend("'#codelist#'", dbHomePostCode)>
    </cfif>
</cfloop>
<cfset address = "[#codelist#]">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜