我是较新版本的 ColdFusion 的新手,并且在将简单数据绑定(bind)到 cfselect 时遇到问题。我已经尽力彻底研究它,甚至回到了教科书,基本上在测试文件中复制了代码示例,但我仍然遇到相同的错误。
我正在尝试构建一种常见情况,其中有 2 个 cfselect,而第二个依赖于第一个,但此时,我什至无法让第一个工作。返回的错误是:
“选择框 Species_id 绑定(bind)失败,绑定(bind)值不是二维数组或有效的序列化查询”
预先感谢您的帮助。这是代码:
<cfcomponent>
<cffunction name="getSpecies" access="remote" returnType="array">
<cfset var rsData = "">
<cfset var myReturn=ArrayNew(2)>
<cfset var i=0>
<cfstoredproc datasource="#application.dsn#" procedure="up_get_Species">
<cfprocresult name="DataResults">
</cfstoredproc>
<cfloop query="DataResults">
<cfset myReturn[rsData.currentRow][1]=rsData.Species_id>
<cfset myReturn[rsData.currentRow][2]=rsData.Species>
</cfloop>
<cfreturn myReturn>
</cffunction>
</cfcomponent>
<html>
<head>
<title>CFSelect Example</title>
</head>
<body>
<h1>Sandbox for getting cfselect bind working</h1>
<cfform name="form1">
Wood Type
<br>
<cfselect name="Species_id" bind="office.cfc:data.getspecies()"
bindOnLoad = "true" />
</cfform>
</body>
</html>
最佳答案
您的绑定(bind)语法似乎已关闭。绑定(bind)表达式应以绑定(bind)的type:
开头(cfc、url、javascript)。由于您要绑定(bind)到组件,因此必须在其前面加上 "cfc:"
,即
bind="cfc:path.to.yourComponentName.yourFunctionName()"
也就是说,CF 的更高版本支持绑定(bind)到查询,这简化了绑定(bind)。只需将函数 returnType
更改为 query
即可。
<cffunction name="getSpecies" access="remote" returnType="query">
<!--- Be sure to Local scope all variables, including query names --->
<cfstoredproc datasource="#application.dsn#" procedure="up_get_Species">
<cfprocresult name="Local.DataResults">
</cfstoredproc>
<cfreturn Local.DataResults >
</cffunction>
然后在选择列表中指定 display
和 value
属性。
<cfselect name="service"
bind="cfc:office.cfc:data.getSpecies()"
display="Species"
value="Species_id" ...>
关于binding - cfselect 未绑定(bind)到 cfc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15955121/