首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于另一个cfselect的cfselect中的数据过滤

基于另一个cfselect的cfselect中的数据过滤
EN

Stack Overflow用户
提问于 2018-09-21 14:19:03
回答 1查看 151关注 0票数 1

在我正在开发的ColdFusion应用程序中,用户将看到两个cfselect,一个包含区域列表,另一个包含中心列表。每个地区都有自己的中心。当用户单击第一个cfselect中的某个区域时,它应该使用一个中心列表填充第二个区域。

我的计划是在列表中有一个完整的中心列表,并在它们的相应区域被选中时使它们可见。有什么简单的方法吗?我对ColdFusion非常陌生,所以我在挣扎。下面是我所拥有的两个cfselect的代码:

代码语言:javascript
复制
<td>
        <cfset SelectionListWidthAndHeight =  "width:375px; height:" & min(130, ((REGIONS.RecordCount-1) * 13)) & "px;">
        <cfselect           name            = "Select_Region_ID"
                            query           = "REGIONS"
                            queryposition   = "below"
                            value           = "REGION_ID"
                            display         = "Region"
                            selected        = "0" 
                            size            = "10"
                            style           = #SelectionListWidthAndHeight#
                            required        = "yes"
                            message         = "You must specify a center."
                            onchange        =  "">
                            <option value="All">All regions and centers</option>



        </cfselect>                 
    </td>

<td>
        <cfset SelectionListWidthAndHeight =  "width:375px; height:" & min(130, ((CENTERS.RecordCount-1) * 13)) & "px;">
        <cfselect           name            = "Select_Center_ID"
                            query           = "CENTERS"
                            queryposition   = "below"
                            value           = "CENTER_ID"
                            display         = "Center"
                            selected        = "0" 
                            size            = "10"
                            style           = #SelectionListWidthAndHeight#
                            required        = "yes"
                            message         = "You must specify a center."

                            >
                            <option value="All">All centers in region</option>

        </cfselect>
    </td>

使用cfstoredprocs检索区域和中心列表:

代码语言:javascript
复制
<cfstoredproc procedure="spGetAllRegions" datasource="APD">
    <cfprocresult name="REGIONS" resultset="1">
</cfstoredproc>

<cfstoredproc procedure="spGetAllCenters" datasource="APD">
    <cfprocresult name="CENTERS" resultset="1">
</cfstoredproc>

到目前为止,CFC守则:

代码语言:javascript
复制
<cfcomponent output="false">
<cfset variables.dsn = "APD">

<cffunction name="getregions" access="remote" returntype="query">
    <cfset var getData = "">

    <cfquery name="getData" datasource="#variables.dsn#"> 
        SELECT DISTINCT REGION_ID FROM Regions
    </cfquery>

    <cfreturn getData />
</cffunction>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-21 17:00:51

(太长而不能发表评论)

实际上,它有两个部分:服务器端代码(CF)和客户端(Javascript/Ajax)。一个完整的例子对于单个S.O.线程来说有点长。如果是我,我会专注于第一次编写CF代码。让<cfcomponent>以您想要的格式返回您想要的数据。在它启动和工作之后,继续使用客户端代码。

就ColdFusion代码而言,您当前的CFC看起来不错。我要做的唯一改变就是让函数返回一个结构数组,而不是一个“查询”对象。CF在序列化查询对象时默认为摇摇欲坠的格式。更好的办法是建造自己的建筑,海事组织。

代码语言:javascript
复制
<cffunction name="getRegions" access="remote" returntype="array">

    <cfquery name="local.getData" datasource="#variables.dsn#"> 
        SELECT Region_Id, Region
        FROM   Regions
        ORDER BY Region
    </cfquery>

    <!--- convert each record into structure and append to array --->
    <cfset local.results = []>
    <cfloop query="local.getData">
       <cfset arrayAppend(local.results, {"value": region_id, "label": region})>
    </cfloop>

    <cfreturn local.results />
</cffunction>

要查看ajax调用将接收哪些数据,请在浏览器中加载数据并测试远程函数:

http://localhost/YourComponent.cfc?method=getRegions&returnformat=json

您可以创建一个类似的函数来返回与特定区域id关联的中心。唯一的区别是它需要一个区域id作为参数:

代码语言:javascript
复制
<cffunction name="getCenters" access="remote" returntype="array">
    <cfargument name="region_id" type="numeric" required="true">

    <cfquery name="local.getData" datasource="#variables.dsn#"> 
        SELECT Center_Id, Center
        FROM   Centers
        WHERE  Region_Id = <cfqueryparam value="#arguments.region_id#" cfsqltype="cf_sql_integer">
        ORDER BY Center
    </cfquery>
    <!--- convert each record into structure and append to array --->
    <cfset local.results = []>
    <cfloop query="local.getData">
        <cfset arrayAppend(local.results, {"value": center_id, "label": center})>
    </cfloop>

    <cfreturn local.results>
</cffunction>   

测试是类似的,您只需要提供一个区域id作为url参数:

http://localhost/YourComponent.cfc?method=getCenters&returnformat=json&region_id=123

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52445695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档