ColdFusion的(使用CFFORM更新阵列 - 更改值后提交) [英] Coldfusion (Update Array using CFForm - Change values after Submit)

查看:181
本文介绍了ColdFusion的(使用CFFORM更新阵列 - 更改值后提交)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的形式。值从数据库加载到一个数组,然后使用cfinput和cfselect标记,以允许现场编辑显示整个表。所以,我希望能够改变任何领域的向上和向下的显示,然后点击提交,并显示更改的字段但没有什么是永远改变了表。该表只是恢复到原来的和更改标签dissappear。我不想让用户使用单独一个单独的形式更新每个字段。我想有一个提交整个页面更新。

任何建议:

 <! -  ****装载数组从数据库中************************ ********** --->    < CFSET AssignArray = ArrayNew(2)>    < CFSET我= 1>
    < CFOUTPUT查询=getAssignments>
        &所述; CFSET AssignArray [Ⅰ] [1] =#getAssignments.Assignment#>
        &所述; CFSET AssignArray [Ⅰ] [2] =#getAssignments.Baylor#>
        < CFSET I = I + 1>
    < / CFOUTPUT><! - ****用表中的值来改变FORM ********************************* * --->    <表格边框=0CELLSPACING =0>
        <标题>更新分配与LT; /字幕>    < CFFORM NAME =FORMDATA>
    <表>
      < TR><第i#< /第i个百分位>分配和LT; /第i个百分位>名称< /第i< / TR>
      < CFLOOP从=1=#getAssignments.RecordCount#指数=我>
         &所述; TR>
            < TD类=centercell><&CFOUTPUT GT;#I#< / CFOUTPUT>< / TD>
            < TD>< cfinput类=assignSize类型=文本名称=分配
                    MAXLENGTH =70
                   值=#AssignArray [I] [1]#>&下; / TD>
            < TD>< cfselect类=assignFontNAME =姓名查询=getNames
                    显示=名称值=贝勒选择=#TRIM(AssignArray [I] [2])#>
                     &所述; CFIF AssignArray [Ⅰ] [2] NEQ>
                         <期权价值=>未分配< /选项>
                     <&CFELSE GT;
                         <期权价值=选择=选择>未分配< /选项>
                     < / CFIF>
                 < / cfselect>
            < / TD>
        < / TR>
      < / CFLOOP>
    < /表>      < cfinput类=btnStyle类型=提交名称=提交值=更新>
    < / CFFORM>    <!与改变的值--- **** DUMP FORM *********************************** * --->    < CFIF IsDefined(form.Assignment)>
        < CFIF IsDefined(form.submit)>          <表>
          < TR><第i#< /第i个百分位>分配和LT; /第i个百分位>名称< /第i< / TR>
          < CFLOOP从=1=#getAssignments.RecordCount#指数=我>
             &所述; TR>
                 < TD类=centercell><&CFOUTPUT GT;#I#< / CFOUTPUT>< / TD>
                 &所述; TD>&下; C​​FOUTPUT> #AssignArray [Ⅰ] [1]#&下; / CFOUTPUT>&下; / TD>
                 &所述; TD>&下; C​​FOUTPUT> #AssignArray [Ⅰ] [2]#&下; / CFOUTPUT>&下; / TD>
              < / TR>
          < / CFLOOP>
          < /表>
        < / CFIF>
    < / CFIF>


解决方案

  

......而这就是为什么我用数组,因为我料想他会要求和更新(SQL更新)为每一行数据
  这是不同的/独一无二的。我希望通过使用击败系统
  阵列cfselect等。要做到这一点一下子,然后发送
  对数据库的改变阵列回


它不工作的方式。在CF阵列重建每次页面加载,并请求之间不会持续。它们只存在,而你正在生成<形式为GT; 。一旦<形式为GT; 发送回客户端/浏览器,您从服务器断开连接和阵列都不见了。要访问的新的的用户输入值,您必须处理提交的表单字段(不是数组)。

在你的情况,你应该使用唯一的字段名。所以,你可以跟踪每个行或领域的套。做到这一点的方法之一是计数器变量追加到字段名。行存放在一个隐藏字段中的数字(主查询循环外)。

 <表格名称=FORMDATA方法=邮报行动=debug.cfm>
     < CFOUTPUT查询=getAssignments>
        排=#currentRow#        分配
        <输入类型=文本名称=#UniqueRecordID_#currentRow值=#getAssignments.UniqueRecordID#>
        <输入类型=文本名称=#Assignment_#currentRow值=#getAssignments.Assignment#>
        < ---注意:这个字段应该有一个更具描述性的名字--->
        <选择名称=#名_#currentRow>
            <期权价值=>未分配< /选项>
            < CFLOOP查询=getNames>
                <期权价值=#TheValueCol#< CFIF getNames.TheValueCol EQ getAssignments.Baylor>选择=真正的< / CFIF>>
                    #TheDisplayCol#
                < /选项>
            < / CFLOOP>
        < /选择>
        < BR>
    < / CFOUTPUT>    <!---专卖店总数行--->
    <&CFOUTPUT GT;
       <输入类型=隐藏的名字=totalRowsVALUE =#getAssignments.recordCount#>
    < / CFOUTPUT>
    <输入类型=提交名称=提交值=更新>
< /表及GT;

在提交表单

后,使用form.totalRows循环,并提取值。在循环中,做任何你需要的值(存储在数据库中,显示器等)做

 < CFIF structKeyExists(FORM,提交)>
    < cfparam NAME =form.totalRows默认值=0>    &所述; CFLOOP从=1=#form.totalRows#索引=variables.row>
        <!---让每组值---制造>
            < CFSET variables.ID = FORM [UniqueRecordID _&放大器; variables.row>
        < CFSET variables.assignment =表格[转让_&放大器; variables.row>
        < CFSET variables.name =表格[名称_&放大器; variables.row>        <!---显示屏发生变化值(将它们插入到一个数据库,等等。)--->
        <&CFOUTPUT GT;
           行[#variables.row#]
               ID =#variables.id#
               分配=#variables.assignment#
               名称=##variables.name LT&; BR>
        < / CFOUTPUT>
    < / CFLOOP>
< / CFIF>

I have a simple form. Values are loaded from a database into an array and then the entire table is displayed using cfinput and cfselect tags to allow field editing. So, I was hoping to be able to change any fields up and down the table that is displayed and then click SUBMIT and display the changed fields but nothing is ever changed. The table just reverts back to the original and the changes to the tags dissappear. I don't want to have the user update each field seperately using a seperate form. I would like to have the entire page update in one submission.

Any suggestions:

<!--- **** LOAD ARRAY FROM DATABASE  ********************************** --->

    <cfset AssignArray = ArrayNew(2)>

    <cfset i=1>
    <cfoutput query="getAssignments">
        <cfset AssignArray[i][1]="#getAssignments.Assignment#">
        <cfset AssignArray[i][2]="#getAssignments.Baylor#">
        <cfset i = i + 1>
    </cfoutput>

<!--- **** FORM WITH TABLE OF VALUES TO CHANGE  ********************************** --->

    <table border="0" cellspacing="0">
        <caption>Update Assignments</caption>

    <cfform name="formData">
    <table>
      <tr><th>#</th><th>Assignment</th><th>Name</th></tr>
      <cfloop from="1" to= "#getAssignments.RecordCount#" index="i">
         <tr>
            <td class="centercell"><cfoutput>#i#</cfoutput></td>
            <td><cfinput class="assignSize" type="text" name="Assignment"
                    maxlength="70" 
                   value="#AssignArray[i][1]#"></td>
            <td><cfselect class="assignFont" name="Name" query="getNames" 
                    display="Name" value="Baylor" selected="#TRIM(AssignArray[i][2])#">
                     <cfif AssignArray[i][2] neq "">
                         <option value="">Not Assigned</option>
                     <cfelse>
                         <option value="" selected="selected" >Not Assigned</option>
                     </cfif>
                 </cfselect>
            </td>
        </tr>
      </cfloop>
    </table>    

      <cfinput class="btnStyle" type="submit" name="submit" value="Update">
    </cfform>

    <!--- ****DUMP FORM WITH CHANGED VALUES  ************************************ --->

    <cfif IsDefined ("form.Assignment")>
        <cfif IsDefined ("form.submit")>

          <table>
          <tr><th>#</th><th>Assignment</th><th>Name</th></tr>
          <cfloop from="1" to= "#getAssignments.RecordCount#" index="i">
             <tr>
                 <td class="centercell"><cfoutput>#i#</cfoutput></td>
                 <td><cfoutput>#AssignArray[i][1]#</cfoutput></td>
                 <td><cfoutput>#AssignArray[i][2]#</cfoutput></td>
              </tr>
          </cfloop>
          </table>    
        </cfif>
    </cfif>

解决方案

...and thats why I used the array because I figured the would require and update (sql update) for each row for data that is different/unique. I was hoping to beat the system by using an array with cfselect, etc.. to make it happen all at once and then send the changes in the array back to the database

It does not work that way. The CF arrays are rebuilt every time the page loads, and do not persist between requests. They exist only while you are generating the <form>. Once the <form> is sent back to the client/browser, you are disconnected from the server and the arrays are gone. To access the new values entered by the user, you must process the submitted FORM fields (not the array).

In your case, you should use unique field names. So you can keep track of each "row" or "set" of fields. One way to do that is append a counter variable to the field names. Store the number of rows in a hidden field (outside the main query loop).

<form name="formData" method="post" action="debug.cfm">
     <cfoutput query="getAssignments">
        row = #currentRow#

        Assignment 
        <input type="text" name="UniqueRecordID_#currentRow#" value="#getAssignments.UniqueRecordID#">
        <input type="text" name="Assignment_#currentRow#" value="#getAssignments.Assignment#">
        <!--- note: this field should have a more descriptive name --->
        <select name="Name_#currentRow#">
            <option value="">Not Assigned</option>
            <cfloop query="getNames">
                <option value="#TheValueCol#" <cfif getNames.TheValueCol eq getAssignments.Baylor>selected="true"</cfif>>
                    #TheDisplayCol#
                </option>
            </cfloop>
        </select>
        <br>
    </cfoutput>

    <!--- store total number of rows --->
    <cfoutput>
       <input type="hidden" name="totalRows" value="#getAssignments.recordCount#">
    </cfoutput>
    <input type="submit" name="submit" value="Update">
</form> 

After the form is submitted, use form.totalRows to loop and extract the values. Inside the loop, do whatever you need to do with the values (store in database, display, etc...)

<cfif structKeyExists(FORM, "submit")>
    <cfparam name="form.totalRows" default="0">

    <cfloop from="1" to="#form.totalRows#" index="variables.row">
        <!--- get each set of values --->
            <cfset variables.ID = FORM["UniqueRecordID_"& variables.row]>
        <cfset variables.assignment = FORM["assignment_"& variables.row]>
        <cfset variables.name = FORM["Name_"& variables.row]>

        <!--- display changed values (insert them into a db, etc..) --->
        <cfoutput>
           row [#variables.row#] 
               id = #variables.id# 
               assignment = #variables.assignment# 
               name = #variables.name#<br>
        </cfoutput>
    </cfloop>
</cfif>

这篇关于ColdFusion的(使用CFFORM更新阵列 - 更改值后提交)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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