如何使用不同的列名称构建结构?

时间:2017-06-30 12:51:29

标签: coldfusion structure coldfusion-9 cfloop

我的cfquery中有两组不同的列。一组列保留给values,第二组保留给表单输入字段中的title。我在服务器端构建结构,我希望这两个在不同的列中。这是一个例子:

<cfquery name="UInfo" datasource="Test">
    SELECT TOP 1
        u_fname,
        u_lname,
        zu_fname,
        zu_lname
    FROM Users WITH (NOLOCK)
    WHERE u_urid = '0012341'
</cfquery>

<cfloop query="UInfo">
    <cfset qryRecs = StructNew()>
    <cfloop array="#UInfo.getColumnNames()#" index="columnName">
        <cfset qryRecs[columnName]['value'] = UInfo[columnName][CurrentRow]>
        <cfset qryRecs[columnName]['title'] = (structKeyExists(UInfo, "z"&columnName)? UInfo[columnName][CurrentRow] : "")>
    </cfloop>
</cfloop>

我上面的代码会产生这种输出:

U_FNAME     
struct
title   First Time
value   Mark
U_FLAST     
struct
title   New Customer
value   Miller
ZU_FNAME    
struct
title   First Time
value   [empty string]
ZU_LNAME    
struct
title   New Customer
value   [empty string] 

正如您在上面看到的,我的代码也为Z列生成了结构。我不需要为这些列提供单独的结构,它们只应在TITLE中显示。我的输出应该是这样的:

U_FNAME     
struct
    title   First Time
    value   Mark
U_FLAST     
struct
    title   New Customer
    value   Miller

我不确定如何获得所需的输出。如果有人可以帮助我,请知道。谢谢!

2 个答案:

答案 0 :(得分:1)

你有这个:

<cfset qryRecs = StructNew()>

在循环中。它应该在此之前:

<cfloop query="UInfo">

答案 1 :(得分:1)

看来你只需要在列名循环

中的columnName上使用if语句
class FbSendDialogComponent extends React.Component {

  componentDidMount() {
    const facebookScript = document.createElement('script')
    facebookScript.type = 'text/javascript'
    facebookScript.async = false
    facebookScript.innerHTML = `
    window.fbAsyncInit = () => {
      FB.init({
        appId: 'my app id',
        autoLogAppEvents: true,
        xfbml: true,
        version: 'v2.9'
      })
      FB.AppEvents.logPageView()
    }
    (function(d, s, id){
       var js, fjs = d.getElementsByTagName(s)[0];
       if (d.getElementById(id)) {return;}
       js = d.createElement(s); js.id = id;
       js.src = "//connect.facebook.net/en_US/sdk.js";
       fjs.parentNode.insertBefore(js, fjs);
     }(document, 'script', 'facebook-jssdk'));`
    this.node.appendChild(facebookScript)
  }

  openDialog = () => {
    FB.ui({
      method: 'send',
      link: 'https://www.google.com/'
    })
  }

  render() {
    return (
      <div ref={(ref) => (this.node = ref)}>
        <h4>You have no connections yet</h4>
        <a onClick={() => this.openDialog()}>Invite</a>
      </div>
    )
  }
}