ColdFusion结构嵌套元素?

时间:2017-06-29 17:14:33

标签: coldfusion structure coldfusion-9

我想在结构中设置嵌套元素。以下是我当前代码的示例:

_graphem

上面的代码使用一些逻辑来操纵数据。这是我转储fncResults后输出的示例:

<cfset fnResults = StructNew()>
<cfset dateList = "HD_DATE1,HD_DATE2,HD_DATE3,HD_DATE4" />
<cfset servicesEquipment = {
    1="Strongly Agree",
    2="Agree",
    3="Don't Know",
    4="Disagree",
    5="Strongly Disagree"
}>
<cfset isActive = {
    1="Yes ",
    0="No "
}>

<cfquery name="UserInfo" datasource="TestDB">
    SELECT TOP 1
        hd_yn1,
        hd_active,
        hd_date1,
        hd_status,
        hd_age1,
        hd_date2,
        hd_age2,
        hd_date3,
        hd_date4,
        hd_age3,
        hd_deg1,
        hd_deg2,
        hd_toner,
        hd_cfgr,
        hd_deg4,
        hd_deg5,
        hd_tonel,
        hd_cfgl,
        hd_hri,
        hd_comment,
        hd_rear,
        hd_lear,
        hd_tosound,
        LTRIM(RTRIM(si_last)) + ', ' + LTRIM(RTRIM(si_first)) AS hd_staff,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_YN1' AND tm_code = hd_yn1) AS zhd_yn1,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_STATUS' AND tm_code = hd_status) AS zhd_status,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_AGE' AND tm_code = hd_age1) AS zhd_age1,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_AGE' AND tm_code = hd_age2) AS zhd_age2,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_AGE' AND tm_code = hd_age3) AS zhd_age3,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_IND' AND tm_code = hd_hri) AS zhd_hri,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_LOSS' AND tm_code = hd_deg1) AS zhd_deg1,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_LOSS' AND tm_code = hd_deg4) AS zhd_deg4,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_TYPE' AND tm_code = hd_deg2) AS zhd_deg2,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HEAR_TYPE' AND tm_code = hd_deg5) AS zhd_deg5,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_TONE' AND tm_code = hd_toner) AS zhd_toner,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_TONE' AND tm_code = hd_tonel) AS zhd_tonel,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_CFG' AND tm_code = hd_cfgr) AS zhd_cfgr,
        (SELECT TOP 1 tm_name FROM hmMaster WHERE tm_tblid = 'HD_CFG' AND tm_code = hd_cfgl) AS zhd_cfgl
    FROM userRec WITH (NOLOCK)
        LEFT OUTER JOIN staffInfo
            ON si_staff = hd_staff
    WHERE hd_userid = '10051989'
</cfquery>

<cfset fnResults.recordcount = UserInfo.recordcount>

<cfif UserInfo.recordcount EQ 0>
    <cfset fnResults.message = "No records were found.">
<cfelse>
    <cfloop query="UserInfo">
        <cfset qryRecs = StructNew()>
        <cfloop array="#UserInfo.getColumnList()#" index="columnName">
            <cfif listContains(dateList, columnName, ",")>
                <cfset qryRecs[columnName] = URLEncodedFormat(Trim(DateFormat(UserInfo[columnName][CurrentRow],'mm/dd/yyyy')))>
            <cfelseif columnName EQ 'hd_active'>
                <cfset qryRecs[columnName] = URLEncodedFormat((structKeyExists(isActive, LossInfo[columnName][CurrentRow]))? isActive[UserInfo[columnName][CurrentRow]]:"No ")>
            <cfelseif columnName EQ 'hd_tosound'>
                <cfset qryRecs[columnName] = URLEncodedFormat((structKeyExists(servicesEquipment, UserInfo[columnName][CurrentRow]))? servicesEquipment[UserInfo[columnName][CurrentRow]]:"")>
            <cfelse>
                <cfset qryRecs[columnName] = URLEncodedFormat(Trim(UserInfo[columnName][CurrentRow]))>
            </cfif>
        </cfloop>
    </cfloop>

    <cfset fnResults.data = qryRecs>
</cfif>

<cfdump var="#fnResults#">

在前端,我必须设置所有字母&#39; z&#39;在&#39; hs&#39;前面是我的title属性数据。因此我想组织我的结构:

DATA    
struct
HD_ACTIVE   No%20
HD_AGE1     [empty string]
HD_AGE2     [empty string]
HD_AGE3     36
HD_CFGL     MMO
HD_CFGR     MMO
HD_COMMENT  Test
HD_DATE1    09%2F22%2F1993
HD_DATE2    [empty string]
HD_DATE3    [empty string]
HD_DATE4    [empty string]
HD_DEG1     II
HD_DEG2     MM
HD_DEG4     MM
HD_DEG5     UU
HD_HRI  NN
HD_LEAR     [empty string]
HD_REAR     [empty string]
HD_STAFF    [empty string]
HD_STATUS   PESS
HD_TONEL    ALL
HD_TONER    ALL
HD_TOSOUND  [empty string]
HD_YN1  Y
ZHD_AGE1    [empty string]
ZHD_AGE2    [empty string]
ZHD_AGE3    36
ZHD_CFGL    MIDDLE
ZHD_CFGR    Mild
ZHD_DEG1    Mild
ZHD_DEG2    Unknown
ZHD_DEG4    Mild
ZHD_DEG5    Unknown
ZHD_HRI     None
ZHD_STATUS  Maybe
ZHD_TONEL   All
ZHD_TONER   All
ZHD_YN1     Did Not
RECORDCOUNT     1 

作为旁注,我试图在我的结构中设置嵌套变量。这样的事情:DATA struct HD_ACTIVE Value: No%20 Title: This is test HD_AGE3 Value: 36 Title: Years HD_COMMENT Value: Test Title: Test 这会引发错误:

<cfset qryRecs[columnName].value =UserInfo[columnName][CurrentRow]>

我不确定我是否还需要一个结构来实现这一目标。另外,我不确定组织这种结构的最佳方法是什么。如果您有任何建议或示例,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:1)

此代码段不是一个完整的答案,但可以使用Value / Title键帮助您使用嵌套结构。

<!--- in order to nest structs, the 'nesting' parent structs must be created before assigning values to them --->
<cfif NOT structKeyExists(qryRecs, "columnName")>
    <cfset qryRecs[columnName] = structNew()>
</cfif>
<!--- qryRecs[columnName] now exists as a struct so 'Value', 'Title', etc. keys can be added to it --->
<cfset qryRecs[columnName].value = UserInfo[columnName][CurrentRow]>