【发布时间】:2016-04-27 15:05:40
【问题描述】:
首先,我不仅对 SO 是全新的,而且对任何类型的网络编程也是全新的,所以完全是新手,感谢您的帮助和耐心!因此,以前在 HTML、ColdFusion、SQL、SQL Server 等方面的经验为零,所以我在过去 2 周中学到的任何东西。
我正在尝试在 ColdFusion 中创建 3 个动态相关下拉菜单(三项相关选择),最终会在另一个 .CFM 页面中打开地图。下拉列表由 SQL Server 中的一个表填充。 我已经做了很多研究(这里是关于 SO 和其他地方),我有一个主要工作的方法,但它有一个问题,我很想帮助弄清楚为什么一件事不起作用。我正在使用 .CFC 查询数据库以获取每个下拉菜单的信息,并且我试图使每个下拉菜单都依赖于前一个,然后在 .CFM 中我使用“绑定”。这是我的 CFC 和 CFM 代码:
CFC(命名为 GettingData):
<cfcomponent displayname="GetStuff" hint="Getting data on cruises from database">
<!---GET ARRAY OF TRIBUTARIES--->
<cffunction name="getData" access="remote" returntype="query" hint="Get data for first select">
<!---define variables--->
<cfset var data="tributary">
<!---Run the query for tributaries--->
<cfquery name="data" datasource="mydatasource">
SELECT distinct Tributary
FROM df_cruises
ORDER BY Tributary
</cfquery>
<!---and return it--->
<cfreturn data>
</cffunction>
<!---Get DATES by tributary--->
<cffunction name="getDates" access="remote" returntype="query"
hint="Get cruise dates by tributary for select dropdown">
<cfargument name="Tributary" type="any" required="no">
<!---Define variables--->
<cfset var data="CruiseDate">
<!---Run query to get Date Data--->
<cfquery name="data" datasource="mydatasource">
SELECT *, CruiseDate AS date <!---this gets date to display correctly in dropdown--->
FROM df_cruises
WHERE Tributary='#ARGUMENTS.Tributary#'<!---single quotes for SQL Server--->
ORDER BY CruiseDate desc
</cfquery>
<!---And return it--->
<cfreturn data>
</cffunction>
<!---getting the html file based on the cruisedate selection--->
<cffunction name="getmapname" access="remote" returntype="query"
hint="Get html file by tributary and cruisedate for dropdown select">
<cfargument name="Tributary" type="any" required="no">
<cfargument name="mapfile" type="any" required="no">
<!---Define variables--->
<cfset var data="">
<cfset var data="">
<!---Get file html Data--->
<cfquery name="data" datasource="mydatasource">
SELECT CruiseDate, Tributary, File_html
FROM df_cruises
WHERE CruiseDate='#ARGUMENTS.mapfile#'AND Tributary='#ARGUMENTS.Tributary#'AND File_html<>'notsampled'
</cfquery>
<!---And return it--->
<cfreturn data>
</cffunction>
</cfcomponent>
CFM(命名为 DropdownDisplay):
<cfparam name="url.File_html" default = '2'>
<cfform name="CruiseChoose" action="mapdisplayv3.cfm" method="post">
<table align="center" bgcolor="orange">
<tr>
<th colspan="2">
<font size="+1">First Select a Water Body, Then Choose Cruise Date</font>
</th>
</tr>
<tr>
<td>
<br/>
Tributary/Water Body:<br />
<cfselect name="Tributary"
bind="cfc:GettingData.getData()"
display="Tributary"
value="Tributary"
bindonload="true"
multiple="no"
size="6" />
</td>
<td>
<br />
Cruise Dates:<br />
<cfselect name="CruiseDate"
bind="cfc:GettingData.getDates({Tributary})"
bindonload="false"
type="link"
display="date"
value="CruiseDate"
multiple="no"
size="6"/>
</td>
<td>
<br>
Map Links:<br />
<cfselect name="link"
bind="cfc:GettingData.getmapname({CruiseDate},{Tributary})"
bindonload="false"
type="any"
display="File_html"
value="File_html"
multiple="no"
size="3"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<br />
</form>
<!--- submit button --->
<cfoutput> <input type="submit" name="submit" value="View Selected Maps" alt="View Map Selection Button"><br></cfoutput>
<!--- Reset button. --->
<input name="reset" type="reset" value="Clear Selections" alt="Clear Selections Button">
</td>
</tr>
</table>
</cfform>
</html>
The first dropdown is pulling a list of tributaries, and when a ‘Tributary’ is chosen the second dropdown correctly populates the list of ‘CruiseDates’ when they were sampled.第三个下拉列表应该填充表格中的“File_html”,该表格成为打开另一个 CFM 上数据图像映射的链接。 (这不是我的最终目标,这是我一步一步的实现。)
The issue I am having is that the third dropdown does not seem to be holding the dependency to the first dropdown selection (Tributary), so when a CruiseDate is selected where 2 tributaries happened to be sampled on the same date both of those File_html 名称显示在第三个下拉列表中(屏幕截图): 在 CFC 中,在“getmapname”函数中,我尝试使用 2 个参数(我在网上某处看到使用过),以便我可以重用该参数(以及 cfargument name="mapfile"),以便在最后一个查询中我可以执行以下操作:
<cfquery name="data" datasource="mydatasource">
SELECT CruiseDate, Tributary, File_html
FROM df_cruises
WHERE CruiseDate='#ARGUMENTS.mapfile#' AND Tributary='#ARGUMENTS.Tributary#'
AND File_html<>'notsampled'
</cfquery>
这不起作用。当我有 2 个参数时出现错误,它似乎只允许 1 个。
这是第三个下拉菜单的屏幕截图,显示虽然选择了“Back River”,但它正在为同一日期采样的 2 个支流提取 File_html:
我也开始研究一个 javascript 版本,但我也坚持这样做,因为它是如此接近,所以我继续研究这种方法。但是,如果无法使用这种方法进行三重相关选择,我将不得不继续!
任何帮助都会非常棒,谢谢! -B (抱歉,帖子太长了,只是想彻底)
【问题讨论】:
-
没有什么比从困难开始。你说
The issue I am having is that the third dropdown does not seem to be holding the dependency to the first dropdown selection。如果您有 3 个选择,那么第三个不应该依赖于第二个而不是第一个吗? -
我知道,对吧?是也不是,我想我希望第三个选择同时依赖于第一个和第二个。因此,您选择一个“支流”,然后选择“CruiseDate”,第三个下拉列表应填充指向该支流和 CruiseDate 数据的“File_html”链接,但现在它正在填充指向任何支流的链接在该日期采样。
-
您使用的确切代码和完整的错误消息是什么?您可以分配多个参数。但是,您需要在 cffunction 中定义这两个参数,并将预期数量的值传递给绑定。顺便说一句,一旦你让它工作,请确保使用 cfqueryparam 保护查询以防止 sql 注入。此外,虽然这是一次很好的学习体验……但众所周知,CF 的 ajax 功能有点过时且脆弱。所以你可能会考虑使用另一个库,比如 jQuery。一开始它有点复杂,但更健壮。
-
(编辑)谢谢,你能append that as an edit to the question?。很难在 cmets 中阅读那么多代码 :) 因为它是 ajax,所以首先要在浏览器的 javascript 控制台中查找错误,然后检查 CF 错误日志 {cf_root}\logs。
-
(编辑)看起来我们的帖子有冲突。仅仅添加一个参数是不够的:)。如果函数需要两个值……绑定必须为它提供两个值。所以就像我上面提到的,cfselect 绑定还必须将两个值传递给 cfc。尽管如此,请检查您的 javascript 控制台是否有错误。这是解决 JS 错误的最佳工具。