根据下拉框过滤CFGRID

时间:2012-12-27 17:21:26

标签: coldfusion cfgrid

我是ColdFusion的新手,我正在努力弄清楚如何在MS Access中轻松完成一些简单的功能。

以下是我要做的事情:我希望根据下拉框对cfgrid过滤数据。然后根据另一个下拉框(如果需要)进一步过滤该数据。

非常感谢任何帮助。

示例:

    <html>

<!---Grid Source---> 
<cfquery name="getArtists" datasource="cfartgallery">
    SELECT A.*
    FROM ARTISTS A
    WHERE
        1=1
    <cfif structKeyExists(form,'dropdownbox1')>
      AND STATE like <cfqueryparam value="%#dropdownbox1#%" cfsqltype="CF_SQL_VARCHAR" maxlength="255">
      <cfif structKeyExists(form,'dropdownbox2')>
      AND LASTNAME like <cfqueryparam value="%#dropdownbox2#%" cfsqltype="CF_SQL_VARCHAR" maxlength="255">
      </cfif> <!--- DropdownBox 2 --->
    </cfif> <!--- DropdownBox 1 --->
    ORDER BY A.LASTNAME, A.FIRSTNAME
</cfquery>


<head>
</head>
<body style="padding:20px;">

<cfform>


<cfformgroup type="horizontal" style="backgroundAlpha:0;font-size:12;color:##000;">
    <cfformgroup type="vertical" width="100">

<!---Query for STATE--->
<cfquery name="getState" datasource="cfartgallery">
    SELECT A.STATE   
    FROM ARTISTS A
</cfquery>

<!---STATE Dropdown---> 
<cfselect name="dropdownbox1" query="getState" queryposition="below" 
    label="State:"  value="STATE" display="STATE"  
    required="no" 
    style="backgroundAlpha:0;" width="125">
    <option></option>
</cfselect>

    </cfformgroup>
    <cfformgroup type="vertical" width="100">

<!---Query for LastName--->
<cfquery name="getLast" datasource="cfartgallery">
    SELECT A.LASTNAME   
    FROM ARTISTS A
</cfquery>

<!---LastName Dropdown---> 
<cfselect name="dropdownbox2" query="getLast" queryposition="below" 
    label="Last Name:"  value="LASTNAME" display="LASTNAME"  
    required="no" 
    style="backgroundAlpha:0;" width="125">
    <option></option>
</cfselect>
    </cfformgroup>
    </cfformgroup>

     <cfformitem type="spacer" height="20"/>


<!---Grid---> 
        <cfgrid name="myGrid" query="getArtists" format="html">
            <cfgridcolumn name="ARTISTID" header="ARTISTID" width="75" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="FIRSTNAME" header="FIRSTNAME" width="100" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="LASTNAME" header="LASTNAME" width="75" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="ADDRESS" header="ADDRESS" width="100" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="CITY" header="CITY" width="125" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="STATE" header="STATE" width="125" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="POSTALCODE" header="POSTALCODE" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="EMAIL" header="EMAIL" width="125" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="PHONE" header="PHONE" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="FAX" header="FAX" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="THEPASSWORD" header="THEPASSWORD" headeralign="center" dataalign="center"/>
         </cfgrid>

    </cfform>

</body>
</html>

更新

代码Ben Koshy给我的工作就像一个魅力问题但是当我将表单更改为format =“flash”时它给了我一个错误“没有名称'submit'的方法”。我需要做什么才能解决这个问题?

由于 约翰尼

1 个答案:

答案 0 :(得分:1)

使用CFGRID所需的大部分工作都在为网格供电的CFQUERY端。在这里,我们只检查下拉框表单字段字段名称是否存在,然后编写一些基于该条件过滤的额外SQL。

<cfquery name="getPeople" datasource="myDSN">
  SELECT
    column1, column2
  FROM
    People
  WHERE
    1=1
    <cfif structKeyExists(form,'dropdownbox1')>
      AND column1 like <cfqueryparam value="%#dropdownbox1#%" cfsqltype="CF_SQL_VARCHAR" maxlength="255">
      <cfif structKeyExists(form,'dropdownbox2')>
      AND column1 like <cfqueryparam value="%#dropdownbox2#%" cfsqltype="CF_SQL_VARCHAR" maxlength="255">
      </cfif> <!--- DropdownBox 2 --->
    </cfif> <!--- DropdownBox 1 --->
</cfquery>

<cfgrid ... >

更新:以下是使用示例代码的示例。

<html>
<cfparam name="form.state" default="">
<cfparam name="form.lastname" default="">

<!---Grid Source---> 
<cfquery name="getArtists" datasource="cfartgallery">
    SELECT A.*
    FROM ARTISTS A
    WHERE
        1=1
    AND STATE like <cfqueryparam value="%#form.state#%" cfsqltype="CF_SQL_VARCHAR" maxlength="255">
    AND LASTNAME like <cfqueryparam value="%#form.lastname#%" cfsqltype="CF_SQL_VARCHAR" maxlength="255">
    ORDER BY A.LASTNAME, A.FIRSTNAME
</cfquery>


<head>
</head>
<body style="padding:20px;">

<cfform action="index.cfm" method="post">


<cfformgroup type="horizontal" style="backgroundAlpha:0;font-size:12;color:##000;">
    <cfformgroup type="vertical" width="100">

<!---Query for STATE--->
<cfquery name="getState" datasource="cfartgallery">
    SELECT A.STATE   
    FROM ARTISTS A
    WHERE
        1=1
        AND LASTNAME like <cfqueryparam value="%#form.lastname#%" cfsqltype="CF_SQL_VARCHAR" maxlength="255">
</cfquery>

<!---STATE Dropdown---> 
<cfselect name="state" query="getState" queryposition="below" 
    label="State:"
    value="STATE"
    display="STATE"  
    required="no"
    selected="#form.state#"
    onchange="submit();"
    style="backgroundAlpha:0;" width="125">
    <option></option>
</cfselect>


    </cfformgroup>
    <cfformgroup type="vertical" width="100">

<!---Query for LastName--->
<cfquery name="getLast" datasource="cfartgallery">
    SELECT A.LASTNAME   
    FROM ARTISTS A
    WHERE
        1=1
        AND STATE like <cfqueryparam value="%#form.state#%" cfsqltype="CF_SQL_VARCHAR" maxlength="255">
</cfquery>

<!---LastName Dropdown---> 
<cfselect name="lastname" query="getLast" queryposition="below" 
    label="Last Name:"  value="LASTNAME" display="LASTNAME"  
    selected="#form.lastname#"
    required="no" 
    onchange="submit();"
    style="backgroundAlpha:0;" width="125">
    <option></option>
</cfselect>

    <a href="index.cfm"><input type="button" value="Reset"></a>
    </cfformgroup>
    </cfformgroup>

     <cfformitem type="spacer" height="20"/>


<!---Grid---> 
        <cfgrid name="myGrid" query="getArtists" format="html">
            <cfgridcolumn name="ARTISTID" header="ARTISTID" width="75" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="FIRSTNAME" header="FIRSTNAME" width="100" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="LASTNAME" header="LASTNAME" width="75" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="ADDRESS" header="ADDRESS" width="100" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="CITY" header="CITY" width="125" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="STATE" header="STATE" width="125" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="POSTALCODE" header="POSTALCODE" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="EMAIL" header="EMAIL" width="125" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="PHONE" header="PHONE" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="FAX" header="FAX" headeralign="center" dataalign="center"/>
            <cfgridcolumn name="THEPASSWORD" header="THEPASSWORD" headeralign="center" dataalign="center"/>
         </cfgrid>

    </cfform>

</body>
</html>