使用CFFORM更新阵列

使用CFFORM更新阵列

本文介绍了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>


解决方案

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更新阵列 - 更改值后提交)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:37