【问题标题】:Dynamic Dependent / Triple Related selects in ColdFusion, third not holding first selectColdFusion中的动态相关/三重相关选择,第三个不持有第一个选择
【发布时间】: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 错误的最佳工具。

标签: drop-down-menu coldfusion


【解决方案1】:

感谢大家的帮助和耐心,我得到了 2 个参数,第三个下拉列表现在只返回它应该返回的数据(即它正确绑定到第一个和第二个下拉列表中的选择)。谢谢! 这是有效的新代码:

氟氯化碳:

<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" >
      <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 
         FROM df_cruises
         WHERE Tributary=
         <cfqueryparam cfsqltype='cf_sql_varchar' value='#ARGUMENTS.Tributary#'>
         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" >
      <cfargument name="mapfile" type="any" required="no" default="">
      <cfargument name="Tributary" type="any" required="no" default="">
      <!---Define variables--->
      <cfset var data="">
      <!---Get file html Data--->
      <cfquery name="data" datasource="mydatasource">
         SELECT CruiseDate, Tributary, File_html
         FROM df_cruises
         WHERE CruiseDate=<cfqueryparam cfsqltype='cf_sql_date' value='#ARGUMENTS.mapfile#'> AND Tributary=<cfqueryparam cfsqltype='cf_sql_varchar' value='#ARGUMENTS.Tributary#'> AND File_html<><cfqueryparam cfsqltype="cf_sql_varchar" value='notsampled'>
      </cfquery>
      <!---And return it--->
      <cfreturn data>
   </cffunction>
</cfcomponent>

CFM:

<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>

【讨论】:

  • (编辑)干得好。您的意思是只过滤CruiseDate='#ARGUMENTS.mapfile#' 上的第三个查询吗?旁注,不要忘记在 all 查询参数(sql 注入保护)上使用 cfqueryparam,并在有机会时看看 jQuery。它比 IMO 中的 CF UI 功能要脆弱得多 :)
  • 因为我不知道你过滤查询是什么意思,可能不是!到目前为止,我对这一切的了解真的很有限。我确实对 jQuery 进行了尝试,我将不得不回去更努力地尝试,因为这听起来是一个更好的方法。感谢 cfqueryparam 的提醒,我会在最终确定之前将它们添加到其他任何地方。
  • 过滤,即限制查询返回哪些记录的WHERE 子句条件 :) 由于该函数接受两个参数,我认为(可能是错误的)您希望查询 only i> 返回与提供的 CruiseDate 和 Tributary 值匹配的记录。在这种情况下,两个参数都需要包含在WHERE 子句中。忽略 cfqueryparam 和 psuedo-sql,类似于 WHERE CruiseDate = '#arguments.nameOfFirstArgument#' 和 Tributary='#arguments.nameOfOtherArgument#'。
  • 哦,是的!我有一个粘贴错误!我会纠正它,我确实有两个论点!不错的收获:)
  • 可能是的。尽管您需要修改查询以使用 WHERE Column IN (...) 而不是 equals(仅限单一相等性测试)。
【解决方案2】:

从第二个选择的查询开始。

 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

我将假设表 df_cruises 有一个名为 CruiseID 的数字 PK。将查询更改为:

 SELECT cruiseId
, CruiseDate AS date <!---this gets date to display correctly in dropdown--->
 FROM df_cruises
 WHERE Tributary= cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.Tributary#">
ORDER BY CruiseDate desc

您对第三次选择的绑定将如下所示:

bind="cfc:UsingBook.getmapname({cruiseId})"

您的最终查询是这样的:

SELECT CruiseDate, Tributary, File_html
FROM df_cruises
WHERE cruiseID = cfqueryparam cfsqltype="cf_sql_integer"
value="#ARGUMENTS.cruiseID#>
AND File_html<>'notsampled'

注意变量的查询参数的使用。

【讨论】:

  • 感谢您的帮助。首先我意识到我在代码中引用了错误的 CFC,所以我更正了(所以你上面的代码粘贴应该说“GettingData”而不是“UsingBook”)。是的,你是对的,有一个名为“ID”的PK,所以我按照你的指示使用“ID”,你有“cruiseID”。我收到 2 个绑定错误:error:bind: Bind failed for select box CruiseDate, columns matching the value or display attributes specified on the cfselect tag are not present in the query 和 error:bind: Bind failed, element not found: ID
  • 我通过在第二个查询中添加 * 来消除第一个错误(幸运的是表很小),并且第二个下拉菜单正在工作。但我仍然收到error:bind: Bind failed, element not found: ID 并且第三个下拉列表没有填充。
  • 嗯,你现在有正确的方法。你只需要处理细节。为了提供帮助,您可能希望仅在使用 javascript 尝试之前使用 Coldfusion 代码测试您的 cfc 函数。这样,如果出现问题,您至少会知道功能正常。
  • 感谢您的帮助,我会继续努力的!我已经走到这一步了!
猜你喜欢
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多