将 Coldfusion 查询结果转换为格式化的 JavaScript 数组

发布于 2024-11-30 12:29:43 字数 777 浏览 1 评论 0原文

我正在尝试编写 ColdFusion 代码运行一个查询,然后将结果以某种格式放入数组中。

查询:

<cfquery name="get_cbox" datasource="ds" username="un" password="pw">
    SELECT CBOXADD, MFLPU, SATLPU, BOXTYPE
    FROM myTable
</cfquery>

和包含 1 个元素的数组如下所示:

<script type="text/javascript">
    var addresses = [{ name: "<table width=100% style='font-size:14px'><td rowspan='3'>OBS:1</td><tr><td>"#cboxadd#"</td><td align='right'>LPU M-F: "#mflpu#"</td></tr><tr><td>Barrigada Guam, 96910 ("#boxtype#")</td><td align='right'>LPU Sat: "#satlpu#"</td></tr></table>", to: #cboxadd#", Barrigada Guam, 96910" }];
</script>

如何实现此目的?

I am trying to write ColdFusion code runs a query then puts the results in an array in a certain format.

the query:

<cfquery name="get_cbox" datasource="ds" username="un" password="pw">
    SELECT CBOXADD, MFLPU, SATLPU, BOXTYPE
    FROM myTable
</cfquery>

and array with 1 element looks like:

<script type="text/javascript">
    var addresses = [{ name: "<table width=100% style='font-size:14px'><td rowspan='3'>OBS:1</td><tr><td>"#cboxadd#"</td><td align='right'>LPU M-F: "#mflpu#"</td></tr><tr><td>Barrigada Guam, 96910 ("#boxtype#")</td><td align='right'>LPU Sat: "#satlpu#"</td></tr></table>", to: #cboxadd#", Barrigada Guam, 96910" }];
</script>

How do I accomplish this?

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

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

发布评论

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

评论(3

梦境 2024-12-07 12:29:43

从我看来,你的对象看起来不对,但我不确定你在用它做什么,所以我不确定如何使它适合你,所以我只有一个较小的例子。

<cfquery name="get_cbox" datasource="ds" username="un" password="pw">
    SELECT CBOXADD, MFLPU, SATLPU, BOXTYPE
    FROM myTable
</cfquery>

<cfset arrString = "">
<cfloop from="1" to="get_cbox.recordcount" index="i">
    <cfset arrString = listAppend(arrString,"{'name':'#get_cbox.CBOXADD[i]#','type':'#get_cbox.BOXTYPE[i]#'}")>
</cfloop>
<cfoutput>
<script type="text/javascript">
    var addresses = [#arrString#];
</script>
</cfoutput>

这将构建一个对象数组,每个对象都有属性“name”和“type”,其值是从查询中填充的。

From what I see your object looks wrong, but I'm not sure what you are doing with it, so I'm unsure of how to make it right for you, so I just have a smaller example.

<cfquery name="get_cbox" datasource="ds" username="un" password="pw">
    SELECT CBOXADD, MFLPU, SATLPU, BOXTYPE
    FROM myTable
</cfquery>

<cfset arrString = "">
<cfloop from="1" to="get_cbox.recordcount" index="i">
    <cfset arrString = listAppend(arrString,"{'name':'#get_cbox.CBOXADD[i]#','type':'#get_cbox.BOXTYPE[i]#'}")>
</cfloop>
<cfoutput>
<script type="text/javascript">
    var addresses = [#arrString#];
</script>
</cfoutput>

This will build an array of objects, each object has the properties 'name' and 'type' with values populated from the query.

错々过的事 2024-12-07 12:29:43

您可以尝试 CF8 或 CF9 中的 serializeJSON 函数,它将获取任何 ColdFusion 对象并将其转换为可用的 JSON 字符串。然后可以在您的 JavaScript 函数中使用它。

You could try the serializeJSON function in CF8 or CF9, which will take any ColdFusion object and turn it into a usable JSON string. That can then be used in your JavaScript functions.

我还不会笑 2024-12-07 12:29:43

扩展 Dan 所说的内容,我会做这样的事情(假设您没有使用 CFC):

<cfquery name="get_cbox" datasource="ds" username="un" password="pw">
    SELECT CBOXADD, MFLPU, SATLPU, BOXTYPE
    FROM myTable
</cfquery>

<cfset myStruct = parseQuery(get_cbox)>

<cfset pageJSON = SearilizeJSON(myStruct)>

<cffunction name="parseQuery" output="struct">
    <cfargument name="query" type="query">
    <cfset var html = "">
    <cfset var retStruct = StructNew()> 

    <cfloop query="#arguments.query#">
        <cfset html = "<table width=100% style='font-size:14px'><td rowspan='3'>OBS:1</td><tr><td>"#cboxadd#"</td><td align='right'>LPU M-F: "#mflpu#"</td></tr><tr><td>Barrigada Guam, 96910 ("#boxtype#")</td><td align='right'>LPU Sat: "#satlpu#"</td></tr></table>">
        <cfset retStruct.name = htnml>
        <cfset retStruct.to = CBOXADD>
    </cfloop>

    <cfreturn returnStruct>
</cffunction>

由于我很长一段时间没有使用标签,代码中可能有一些语法错误,但您应该了解要点我想在这里完成的事情。首先,我们将创建一个函数来将查询的输出解析为结构,然后将该结构传递给 sealalizeJSON,这将为您提供一个格式良好的 JSON 对象(请注意,调用后键的大小写可能会有所不同) searilizeJSON)。那么您在页面上所要做的就是:

<script type="text/javascript">
    var addresses = #pageJSON#;

    for(var i=0;i<addresses.length;i++){
        addresses[i].html....
        addresses[i].name....
    }
</script>

Expanding on what Dan said I'd do something like this (assuming that you are not using CFCs):

<cfquery name="get_cbox" datasource="ds" username="un" password="pw">
    SELECT CBOXADD, MFLPU, SATLPU, BOXTYPE
    FROM myTable
</cfquery>

<cfset myStruct = parseQuery(get_cbox)>

<cfset pageJSON = SearilizeJSON(myStruct)>

<cffunction name="parseQuery" output="struct">
    <cfargument name="query" type="query">
    <cfset var html = "">
    <cfset var retStruct = StructNew()> 

    <cfloop query="#arguments.query#">
        <cfset html = "<table width=100% style='font-size:14px'><td rowspan='3'>OBS:1</td><tr><td>"#cboxadd#"</td><td align='right'>LPU M-F: "#mflpu#"</td></tr><tr><td>Barrigada Guam, 96910 ("#boxtype#")</td><td align='right'>LPU Sat: "#satlpu#"</td></tr></table>">
        <cfset retStruct.name = htnml>
        <cfset retStruct.to = CBOXADD>
    </cfloop>

    <cfreturn returnStruct>
</cffunction>

Since I haven't used tags in a long time the code may have a few syntax errors in it, but you should get the gist of what I'm trying to accomplish here. First we'll create a function to parse the output of your query into a structure, then we'll pass that structure to searalizeJSON, which will give you a nicely formatted JSON object (note that the case of the keys may be different after calling searilizeJSON). Then all you'd have to do on your page is:

<script type="text/javascript">
    var addresses = #pageJSON#;

    for(var i=0;i<addresses.length;i++){
        addresses[i].html....
        addresses[i].name....
    }
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文