I need to calculate the total of an invoice. This invoice is created with a form, amount, quantity and tax fields, the sum of the fields is made with a bind in cfinput.
I can not make the sum of all rows, the total.
I tried some operations, but not arriving at the solution
This is an example code:
<cfform action="" method="post">
<cfloop from="1" to="3" index="i">
Q.ta <cfinput type="text" name="quantita#i#" value="0">
+
Importo <cfinput type="text" name="importo#i#" value="0">
+
Tax <cfinput type="text" name="iva#i#" value="0">
=
Totale <cfinput type="text" name="totale#i#" value="0" bind="cfc:somma.getSomma({quantita#i#},{importo#i#},{iva#i#})">
<br /><br />
</cfloop>
CFC:
<cfcomponent>
<cffunction name="getSomma" access="remote" returntype="string">
<cfargument name="quantita" default="0">
<cfargument name="importo" default="0">
<cfargument name="iva" default="0">
<cfset totaleSomma=#evaluate((importo*quantita)*(1+iva))#>
<cfreturn totaleSomma>
</cffunction>
</cfcomponent>
I think you will need to create a Javascript function if you want to loop through ALL these form feilds and get a "grand total". My suggestion would be to abandon cfform and use jQuery to create an editable grid.
ok i found the solution, i use cfdiv for the grand total:
<cfparam name="var_tot" default="0">
<cfloop from="1" to="3" index="i">
<cfparam name="totale#i#" default="0">
<cfset var_tot = listappend(var_tot, "{totale"&#i#&"}")>
</cfloop>
<cfform action="" method="post">
<table>
<cfloop from="1" to="3" index="i">
<tr>
<td>Q.ta</td><td><cfinput type="text" name="quantita#i#" value="0"></td>
<td>Importo</td><td><cfinput type="text" name="importo#i#" value="0"> </td>
<td>Tax</td><td><cfinput type="text" name="iva#i#" value="0"> </td>
<td>Totale</td><td class="price"><cfinput type="text" name="totale#i#" value="0" bind="cfc:somma.getSomma({quantita#i#},{importo#i#},{iva#i#})" ></td>
</tr>
</cfloop>
</table>
</cfform>
<cfdiv bind="url:divtot.cfm?InputText=#var_tot#" id="checktot">
divtot.cfm
<cfparam name="tot" default="0">
<cfset listval=url.InputText>
<cfloop index="i" list="#listval#" delimiters=",">
<cfset tot=tot+i>
</cfloop>
TOTALE: <cfoutput>#tot#</cfoutput>
thank you everybody
Related
How to save image path or name to database and copy the image in a folder. I have searched many pages on the internet but no luck. Please can anyone show my how to create the controller?
My View
<form action="Create" method="post">
<table>
<tr>
<td>
<input type="date" name="InquirDate" id="InquirDate" placeholder="Date" />
</td>
<td>
<input type="text" id="signby" name="signby" placeholder="signby" />
</td>
<td>
<input type="text" id="description" name="description" placeholder="Description" />
</td>
<td>
<input type="text" id="parentInqNo" name="parentInqNo" placeholder="Parent Inquiry number">
</td>
<td>
<select id="status" name="status">
<option>Draft</option>
<option>Published</option>
</select>
</td>
<td>
<input type="text" id="number" name="number" placeholder="number" />
</td>
</tr>
<tr>
<td>
<input type="text" name="subject" id="subject" placeholder="Subject" />
</td>
<td>
<input type="text" id="empid" name="empid" placeholder="empid" />
</td>
<td>
<input type="file" name="Attachment" id="Attachment" />
</td>
<td>
<input type="text" name="InqiurURL" id="InqiurURL" placeholder="InqiurURL" />
</td>
<td>
<input type="text" name="EnterDate" id="EnterDate" placeholder="EnterDate" />
</td>
</tr>
</table>
<input type="submit" value="Save">
</form>
Try following this code in your controller
public ActionResult Save(FormCollection fc, HttpPostedFileBase Attachment)//Instead of Attachment better to use "file"
{
if (Attachment!= null)
{
Database1Entities db = new Database1Entities();
string ImageName = System.IO.Path.GetFileName(Attachment.FileName);
string physicalPath =Server.MapPath("~/images/"+ ImageName);
// save image in folder
Attachment.SaveAs(physicalPath);
//save new record in database
tblA newRecord = new tblA();
newRecord.empid= fc.empid;
newRecord.subject= fc.subject;
//....Add all field which need to save in db
newRecord.Attachment= ImageName;
db.tblA.Add(newRecord);
db.SaveChanges();
}
return View();
}
I have inputtext filed in my gsp llike this :
<tr class="context">
<td width="5%" ><a class="addButton" href="#" style="display:none;" >+</a></td>
<td width="60%"><input type="text" name="iwd0_description" value="" id="iwd0_description" /></td>
<td width="10%"><input type="text" name="iwd0_tax" value="" id="iwd0_tax" /></td>
<td width="10%"><input type="text" name="iwd0_discount" value="" id="iwd0_discount" /></td>
<td width="10%"><input type="null" name="iwd0_total" value="0" required="" id="iwd0_total" /></td>
<td width="5%" ><a class="deleteButton" href="#" style="display:none;" >-</a></td>
</tr>
<tr class="context">
<td width="5%" ><a class="addButton" href="#" style="display:none;" >+</a></td>
<td width="60%"><input type="text" name="iwd1_description" value="" id="iwd1_description" /></td>
<td width="10%"><input type="text" name="iwd1_tax" value="" id="iwd1_tax" /></td>
<td width="10%"><input type="text" name="iwd1_discount" value="" id="iwd1_discount" /></td>
<td width="10%"><input type="null" name="iwd1_total" value="0" required="" id="iwd1_total" /></td>
<td width="5%" ><a class="deleteButton" href="#" style="display:none;" >-</a></td>
</tr>
How can I access to input value in my controller?
Form values are sent over HTTP using request parameters (for GET requests), with the input name attribute used to set the parameter "key". So your HTTP request will have the following params: ?iwd0_tax=userInput1&iwd0_discount=userInput2 etc.
Grails makes request parameters available by the params var in controllers:
def iwd0_tax = params.iwd0_tax
Grails can also populate a bean/class from the request params automatically. The bean is called a command object. See details in the Grails docs.
def i = 0
while (params."iwd${i}_tax") {
println 'tax'+"${i}" + params."iwd${i}_tax"
i++
}
This is my form in jsp
<form action="submit">
<tr class="row" id="item1">
<td><input type="text" name="type" class="type" style="width: 50px"/></td>
<td><input type="text" name="color" class="color" style="width: 50px"/></td>
<td><input type="text" name="qty" class="qty" style="width: 50px"/></td>
<td><input type="text" name="unitprice" class="unitPrice" style="width: 50px"/></td>
</tr>
...
<tr class="row" id="itemN">
<td><input type="text" name="type" class="type" style="width: 50px"/></td>
<td><input type="text" name="color" class="color" style="width: 50px"/></td>
<td><input type="text" name="qty" class="qty" style="width: 50px"/></td>
<td><input type="text" name="unitprice" class="unitPrice" style="width: 50px"/></td>
</tr>
</form>
I want to fetch data which may contain 1 to N items and each item contais type color qty and unitprice. I want to store all these data in ArrayList variable.
How to do this?
What you need is Tabular Input
I have this view in which I need to display twice the same item. So I pass a list of my model and this list contains twice the same item.
Here's how I display them:
<p>
#using (Html.BeginForm("Confirm", "Inventory"))
{
<table>
<tr>
<th>Item Name</th>
(...)
</tr>
#for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>#Html.DisplayFor(x => x[i].OtrObj.ObjName)</td>
(...)
#Html.HiddenFor(x => x[i].OtrObjID)
</tr>
}
</table>
<div class="float-right">
<input type="submit" value="Ready!"/>
</div>
}
</p>
I'm sure that it occurs in the view because just when I send the view each items in my list carries the right "ObjID".
But when the page is rendered, the result is like this:
item 1:
<input data-val="true" data-val-number="The field OtrObjID must be a number."
data-val-required="The OtrObjID field is required."
name="[0].OtrObjID" type="hidden" value="1" />
item 2:
<input data-val="true" data-val-number="The field OtrObjID must be a number."
data-val-required="The OtrObjID field is required."
name="[1].OtrObjID" type="hidden" value="2" />
Why? I really don't get it. Can anyone help me out on this?
Here is the HTML output for those items:
<p>
<form action="/Inventory/Confirm" method="post">
<table>
<tr>
<th>Item Name</th>
...
</tr>
<tr>
<td>Item one</td>
<td><input checked="checked" class="state1" name="[0].Destination" style="width: 50px" type="radio" value="State 1" /></td>
<td><input class="state2" name="[0].Destination" style="width: 50px" type="radio" value="State 2" /></td>
<td>
<input class="field1" data-val="true" data-val-number="The field m_Field1 must be a number." data-val-required="The m_Field1 field is required." name="[0].m_Field1" style="width:200px" type="text" value="0,99" />
<span class="field-validation-valid" data-valmsg-for="[0].m_Field1" data-valmsg-replace="false">Field 1 must be a number.</span>
</td>
<td>
<input class="field2" data-val="true" data-val-number="The field m_Field2 must be a number." data-val-required="The m_Field2 field is required." name="[0].m_Field2" style="width:200px" type="text" value="1,00" />
<span class="field-validation-valid" data-valmsg-for="[0].m_Field2" data-valmsg-replace="false">Field 2 must be a number.</span>
</td>
<td>
<input class="field3" data-val="true" data-val-number="The field m_Field3 must be a number." data-val-required="The m_Field3 field is required." name="[0].m_Field3" style="width:200px" type="text" value="1,00" />
<span class="field-validation-valid" data-valmsg-for="[0].m_Field3" data-valmsg-replace="false">Field 3 must be a number.</span>
</td>
<td>See other information</td>
<input data-val="true" data-val-number="The field OtrObjID must be a number." data-val-required="The OtrObjID field is required." name="[0].OtrObjID" type="hidden" value="1" />
<input data-val="true" data-val-number="The field ObjID must be a number." data-val-required="The ObjID field is required." name="[0].ObjID" type="hidden" value="1" />
<input name="[0].m_Field1" type="hidden" value="0,99" />
<input name="[0].m_Field2" type="hidden" value="1,00" />
<input name="[0].m_Field3" type="hidden" value="1,00" />
</tr>
<tr>
<td>Item 2</td>
<td><input checked="checked" class="state1" name="[1].Destination" style="width: 50px" type="radio" value="State 1" /></td>
<td><input class="state2" name="[1].Destination" style="width: 50px" type="radio" value="State 2" /></td>
<td>
<input class="field1" data-val="true" data-val-number="The field m_Field1 must be a number." data-val-required="The m_Field1 field is required." name="[1].m_Field1" style="width:200px" type="text" value="0,99" />
<span class="field-validation-valid" data-valmsg-for="[1].m_Field1" data-valmsg-replace="false">Field 1 must be a number.</span>
</td>
<td>
<input class="field2" data-val="true" data-val-number="The field m_Field2 must be a number." data-val-required="The m_Field2 field is required." name="[1].m_Field2" style="width:200px" type="text" value="1,00" />
<span class="field-validation-valid" data-valmsg-for="[1].m_Field2" data-valmsg-replace="false">Field2 must be a number.</span>
</td>
<td>
<input class="field3" data-val="true" data-val-number="The field m_Field3 must be a number." data-val-required="The m_Field3 field is required." name="[1].m_Field3" style="width:200px" type="text" value="1,00" />
<span class="field-validation-valid" data-valmsg-for="[1].m_Field3" data-valmsg-replace="false">Field 3 must be a number.</span>
</td>
<td>See other information</td>
<input data-val="true" data-val-number="The field OtrObjID must be a number." data-val-required="The OtrObjID field is required." name="[1].OtrObjID" type="hidden" value="2" />
<input data-val="true" data-val-number="The field ObjID must be a number." data-val-required="The ObjID field is required." name="[1].ObjID" type="hidden" value="2" />
<input name="[1].m_Field1" type="hidden" value="0,99" />
<input name="[1].m_Field2" type="hidden" value="1,00" />
<input name="[1].m_Field3" type="hidden" value="1,00" />
</tr>
</table>
<div class="float-right">
<input type="submit" value="Ready!"/>
</div>
</form>
</p>
The "problem" is that you have #model IEnumerable<Model> that'll force the [#] prefix.
One possible solution would be to wrap your Model inside a ViewModel.
public class ViewModel
{
public IEnumerable<Model> Models { get; set; }
}
And then use #model ViewModel
I'd also like to suggest the use of #foreach as it's cleaner.
#foreach (var item in Model.Models) {
<tr>
<td>
#Html.DisplayFor(x => item.ObjName)
(...)
#Html.HiddenFor(x => item.OtrObjID)
</td>
</tr>
}
Note: I put everything inside the <td> as it's not valid HTML to have anything other than <td> or <th> inside the <tr> tag. http://www.w3schools.com/tags/tag_tr.asp
You can use simple html hidden field control
<input type="hidden" value="x[i].OtrObjID" name="OtrObjID" />
I need to get in an action method the values from the selected checkboxes on a form
how would you do that ? (or probably get them all and see who was selected)
public ActionResult (int[] ids)
...
<input type="checkbox" value = "1" />
<input type="checkbox" value = "2" />
<input type="checkbox" value = "3" />
<input type="checkbox" value = "4" />
<%=Html.Submit(); %>
You could try giving the checkboxes a name (ids):
<input type="checkbox" name="ids" value="1" />
<input type="checkbox" name="ids" value="2" />
<input type="checkbox" name="ids" value="3" />
<input type="checkbox" name="ids" value="4" />
Should work with:
public ActionResult Index(int[] ids) { ... }