从CFC函数访问查询数据 [英] Accessing Query Data from CFC Function

查看:404
本文介绍了从CFC函数访问查询数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的反对:我试图根据从CFSelect框中的选择更改CFDIV的内容。

Here is what I am up against: I am trying to change the content of a CFDIV based on the selection from a CFSelect box.

为此,我已将CFDiv绑定到CFC,并且试图从我的查询中返回在该CFC中执行的两列; Alert_Status AND Alert_Priority。这些值将根据CFM页面中CFSelect框中的选择进行查询。 Company_Name是从CFSelect框中的选择传递给CFC的值。一旦CFC中的查询运行,我想在与选择框相同的CFM页面上的DIV中显示结果。

To do this I have bound the CFDiv to a CFC and I am trying to return two columns from my query that is executed in that CFC; Alert_Status AND Alert_Priority. These values will be queried based on a selection from the CFSelect box in my CFM page. Company_Name is the value passed to the CFC from the selection in the CFSelect box. Once the query in the CFC is run, I would like to display the results in a DIV on that same CFM page as the select box.

以下是CFC:

    <!---First Slect Box --->
<cffunction name="getData" access="remote" returntype="query">
    <cfoutput>
    <!--- Function to get data from datasource --->
    <cfquery name="data" datasource="#datasource#">
    select company_name, customer_id
    from customer_table
    where status <> '0'
    order by company_name
    </cfquery>
    </cfoutput>

    <!--- Return results --->
    <cfreturn data>
</cffunction>

<cffunction name="getDetail" access="remote" returnType="string">
    <cfargument name="company_name" type="any" required="true">

    <!--- localize function variables --->
    <cfset var dataDetail = "">
    <cfoutput>
    <cfquery name="dataDetail" datasource="#datasource#">
        SELECT tax_rate
        FROM   customer_table
        <!--- adjust cfsqltype if needed --->
        WHERE company_name = <cfqueryparam value="#ARGUMENTS.company_name#" cfsqltype="cf_sql_varchar">
    </cfquery>
    </cfoutput>
    <cfreturn dataDetail.tax_rate>
</cffunction>

<cffunction name="getAlerts" access="remote" returnType="query">
    <cfargument name="company_name" type="any" required="true">

    <!--- localize function variables --->
    <cfset var alertDetail = "">
    <cfoutput>
    <cfquery name="getID" datasource="#datasource#">
        select customer_id
        from customer_table
        where company_name = <cfqueryparam value="#ARGUMENTS.company_name#" cfsqltype="cf_sql_varchar">
    </cfquery> 
    <cfquery name="alertDetail" datasource="#datasource#">
        SELECT *
        FROM   customer_alerts
        <!--- adjust cfsqltype if needed --->
        WHERE customer_id = <cfqueryparam value="#getID.customer_id#" cfsqltype="cf_sql_varchar"> AND alert_status = 'on'
    </cfquery>
    </cfoutput>


    <cfreturn alertDetail>
</cffunction>

我尝试在查询中显示查询结果 AlertDetail

I am trying to display the query results for the query AlertDetail in a div on my main page.

以下是我的CFM页面中与此CFC相关的部分:

          <cfdiv name="test" id="test" type="text" bind="cfc:cfcs.taxdata.getAlerts({company_name})" bindonload="true" bindattribute="value" rows="2" cols="2" readonly="yes"></cfdiv>

任何帮助将非常感谢。谢谢。
-Brian

Any help would be greatly appreciated. Thanks. -Brian

推荐答案

让我们退一会儿。我看到你的目标,有时使用CFDiv只是复杂的东西。我只是使用AJAX。我不知道你的意思是什么两列,所以我发送给你的当前示例只显示一个函数被返回。添加第二个将很容易复制。

Let's step back for a moment. I see your goal and sometimes using CFDiv only complicates things. I would just use AJAX. I wasn't sure what you meant by "two columns" so the current example I am sending you only shows one function being returned. Adding the second will be pretty easy to replicate.

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
        // populate the dropdown
        $.ajax({
            type: "GET",
            url:"cfcs/taxdata.cfc?method=getData",
            dataType: "json",
            success: function (data) {
                $("#formSelect").html("");
                // may need tweeking depending on how you are outputting your query.
                $.each(data,function(i,obj)
                   {
                       var div_data="<option value="+obj+">"+obj+"</option>";
                       $(div_data).appendTo('#formSelect'); 
                   });  
            }
        });         
        // on change of dropdown value return the cfc data into the div
        $("#formSelect").change(function() {  
            var selectValue = $("#formSelect").val();
            $.ajax({
                type: "GET",
                url:"cfcs/taxdata.cfc?method=getAlerts",
                dataType: "json",
                data: { 
                    company_name: selectValue
                },
                success: function (data) {
                    $("#test").html(data);
                }
            });
        });
    </script>
</head>
<body>
    <form>
        <select id="formSelect">
        </select>
    </form>
    <div id="test"></div>
</body>

可以向< div>

<!-- Use multiple AJAX calls to each function. Have them return in separate divs within the main div. -->

<div id="test">
    <div id="query1"></div>
    <div id="query2"></div>
    <div id="query3"></div>
    <!-- You can also do paragraph tags. JQuery only looks for the ID or class -->
    <p id="query4"></p>
</div>

Option Two

<!-- 
    In your ajax instead of using $("#test").html(data); you can use appentTo() for each AJAX call
-->

success: function (data) {
    $(data).appendTo('#test');
}

这篇关于从CFC函数访问查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆