简体   繁体   中英

How can I get a sum total from a field that is part of a dynamic row add?

I have a form, and the user can add as many rows as they want. One of the fields in the dynamic row is total cost. I am trying to figure out how to add the entries for this one particular field and display them on the following page.

I am coding in html 5, coldfusion and javascript.

Here is my script that generates the dynamic row:

    <script>
    $(document).ready(function(){
        var counter = 1;
        $("#addmrow").click(function(event){
            event.preventDefault();
            var newRow = 
                '<tr id="rtr_' + counter + '">' +
                    '<td><input type="text" id="Qty_' + counter + '" name="Qty_' + counter + '" size=3/></td>' +
                    '<td><input type="text" id="Sku_' + counter + '" name="Sku_' + counter + '" size=10/></td>' +
                    '<td><input type="text" id="Description_' + counter + '" name="Description_' + counter + '" /></td>' +
                    '<td><input type="text" id="Retail_' + counter + '" name="Retail_' + counter + '" size=10/></td>' +
                    '<td><input type="text" id="TransferNumber_' + counter + '" name="TransferNumber_' + counter + '" size=10/></td>' +
                    '<td><input type="text" id="TransferDate_' + counter + '" name="TransferDate_' + counter + '" size=16/></td>' +
                '</tr>';
            counter++;
            $('#mlist').append(newRow);
        });
        $("#delmrow").click(function(event){
            event.preventDefault();
            counter--;
            $("#" + 'Qty_' + counter).remove();
            $("#" + 'Sku_' + counter).remove();
            $("#" + 'Description_' + counter).remove();
            $("#" + 'Retail_' + counter).remove();
            $("#" + 'TransferNumber_' + counter).remove();
            $("#" + 'TransferDate_' + counter).remove();
            $("#" + 'rtr_' + counter).remove();
        });
    });
</script>

Here is my code displaying it on the next page, but I cant figure out how to add the Average Cost field so I get a sum total of that field:

                <cfloop index="i" list="#form.FieldNames#" delimiters=",">
                <cfif REFind("QTY_", i)>
                   <tr><td><cfoutput>#form[i]#</cfoutput></td>
                </cfif>
                <cfif REFind("SKU_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("DESCRIPTION_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("RETAIL_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("TRANSFERNUMBER_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("AVGCOST_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td></tr>
                </cfif> 
            </cfloop>

I hope I explained this correctly. Any help is much appreciated!

There are proably better ways to do this but given what you have this may be the simplest

            <cfset avgTotal = 0>           
            <cfloop index="i" list="#form.FieldNames#" delimiters=",">
                <cfif REFind("QTY_", i)>
                   <tr><td><cfoutput>#form[i]#</cfoutput></td>
                </cfif>
                <cfif REFind("SKU_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("DESCRIPTION_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("RETAIL_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("TRANSFERNUMBER_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("AVGCOST_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td></tr>
                    <cfset avgTotal = avgTotal+form[i]> 
                </cfif> 
            </cfloop>
            <cfoutput>#avgTotal#</cfoutput>

Is that what you are tying to do? Or have I completely missed the mark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM