Capture the "last record" in Coldfusion for IE javascript bug

I am using ColdFusion to pull UK postcodes into an array for display on a Google map. This comes dynamically from an SQL database, so numbers can range from 1 to 100 +

The script works fine, however in IE (moan) it decides to display one point from the line, somewhere in California.

I fixed this problem in a previous webapp, it was connected with a comma between each element of the array that is still present at the end. Works fine in Firefox, Safari, etc., but not in IE.

But that one used 10 records, so it was easy to fix.

I just need a little bit for the operator to wrap my comma to hide it when it gets to the last entry. I don't seem to understand. 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>]; 

Hope someone can help with this rather simple request. I just have a bad day at the office!

+4
source share
5 answers

My approach is slightly different, you need to make fewer settings, and you do not need to repeat; the cfloop tag did all this.

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

There are no breaks, you can paste this into your page and it will 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>]; 
+1
source
 var address = [#ListQualify(ValueList(getApplicant.dbHomePostCode), "'")#] 

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

With ListQualify() empty (NULL or empty strings) postal codes will not be displayed as ColdFusion list functions ignore empty list items.


EDIT: A previous revision of this answer indicated that empty elements would appear as a result of ListQualify() . This is incorrect, but the first two comments relate to this initial version.

+5
source

try it

 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 code above uses Integer (i) to store the position in the loop, and when it comes to outputting control data, to see if I am smaller than the size of the SQL result. Thus, it will only output a comma, if this is not the last line of the result set.

0
source

@ Tomalok has a good answer. It is possible that I will use.

Before you see his answer, here, probably, would be my answer:

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

Using query.recordcount, you can determine if you are on the last line of your query and adjust the output accordingly. In general, Tomalak and Ben Duma have excellent answers, but this shows how you could fix the problem 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> 
0
source

Source: https://habr.com/ru/post/1305651/


All Articles