Mamine
asked on
Routine to total-up user-changed datagrid column in asp.net
I have an asp.net datagrid with a variable/unknown number of rows. All the columns except one are read-only. The exception is a columnn where users can amend the figures. I want to have a client-side routine which re-calculates the totals but I don't have a clue about javascript! Can anyone help ?
If u could post the datagrid it would be easier for us.
Thanks
Thanks
ASKER
Many thanks dakyd this looks really great! A couple of questions to help my understanding ...
1. What does the [0] refer to in here: getElementsByTagName("inpu t")[0]
2. If I want to read a value in another column that is readonly (eg to calculate horizontal subtotals as well, multiplying the above column by a quantity column), how do I identify the read-only/quantity col ? asp.net throws out these other columns without any tag name except the <td></td> its in so I can't use getElementsByTagName. Is there another way of identifying cells ?
1. What does the [0] refer to in here: getElementsByTagName("inpu
2. If I want to read a value in another column that is readonly (eg to calculate horizontal subtotals as well, multiplying the above column by a quantity column), how do I identify the read-only/quantity col ? asp.net throws out these other columns without any tag name except the <td></td> its in so I can't use getElementsByTagName. Is there another way of identifying cells ?
ASKER
Here is a sample datagrid:
<table class="Grid" cellspacing="0" align="Left" rules="rows" border="1" id="ucContent_DbGrid_Ingre dients" style="width:100%;border-c ollapse:co llapse;">
<caption align="Top">Items</caption >
<tr class="GridHdr">
<td align="Left">Item</td>
<td align="Center">Quantity</t d>
<td align="Right" style="width:6em;">Cost1</ td>
<td align="Right" style="width:6em;">Cost2</ td>
<td align="Right">Your cost</td>
<td align="Right" style="width:6em;">CalcCos t</td>
</tr>
<tr class="GridItem">
<td align="Left"><a href="ItemDetails.aspx?id= 1392">Item 1</a></td>
<td nowrap="nowrap" align="Center">1</td>
<td nowrap="nowrap" align="Right">10.000</td>
<td nowrap="nowrap" align="Right">0.052</td>
<td nowrap="nowrap" align="Right"><input type="text" maxlength="7" size="7" tabindex="2" style="TEXT-ALIGN: right" onblur="recalc"></td>
<td nowrap="nowrap" align="Right">0.052</td>
</tr>
<tr class="GridItem">
<td align="Left"><a href="ItemDetails.aspx?id= 2918">Item 2</a></td>
<td nowrap="nowrap" align="Center">1</td>
<td nowrap="nowrap" align="Right">900.000</td> <td nowrap="nowrap" align="Right"> </td>
<td nowrap="nowrap" align="Right"><input type="text" maxlength="7" size="7" tabindex="2" style="TEXT-ALIGN: right" onblur="recalc"></td>
<td nowrap="nowrap" align="Right">900.000</td>
</tr>
<tr class="GridItem">
<td align="Left"><a href="ItemDetails.aspx?id= 3074">Item 3</a></td>
<td nowrap="nowrap" align="Center">3</td>
<td nowrap="nowrap" align="Right">100.000</td>
<td nowrap="nowrap" align="Right"><input type="text" maxlength="7" size="7" tabindex="2" style="TEXT-ALIGN: right" onblur="recalc"></td>
<td nowrap="nowrap" align="Right">300.000</td>
</tr>
<tr class="GridFtr" align="Left" valign="Top">
<td align="Left" valign="Top"> </td>
<td nowrap="nowrap" valign="Top"> </td>
<td> </td>
<td align="Right" valign="Top"> </td>
<td align="Right" valign="Top"> </td>
<td align="Right" valign="Top">Totals</td>
<td nowrap="nowrap" align="Right" valign="Top">1,200.052</td >
</tr>
</table>
This should come out as:
Item Quantity Cost1 Cost2 Your Cost CalcCost
Item1 1 10 0.052 0.052
Item2 1 900 900
Item3 3 100 300
Totals 1200.052
Your Cost is the column that the user can change. CalcCost needs to be a column of subtotals with a calculated grand total
<table class="Grid" cellspacing="0" align="Left" rules="rows" border="1" id="ucContent_DbGrid_Ingre
<caption align="Top">Items</caption
<tr class="GridHdr">
<td align="Left">Item</td>
<td align="Center">Quantity</t
<td align="Right" style="width:6em;">Cost1</
<td align="Right" style="width:6em;">Cost2</
<td align="Right">Your cost</td>
<td align="Right" style="width:6em;">CalcCos
</tr>
<tr class="GridItem">
<td align="Left"><a href="ItemDetails.aspx?id=
<td nowrap="nowrap" align="Center">1</td>
<td nowrap="nowrap" align="Right">10.000</td>
<td nowrap="nowrap" align="Right">0.052</td>
<td nowrap="nowrap" align="Right"><input type="text" maxlength="7" size="7" tabindex="2" style="TEXT-ALIGN: right" onblur="recalc"></td>
<td nowrap="nowrap" align="Right">0.052</td>
</tr>
<tr class="GridItem">
<td align="Left"><a href="ItemDetails.aspx?id=
<td nowrap="nowrap" align="Center">1</td>
<td nowrap="nowrap" align="Right">900.000</td>
<td nowrap="nowrap" align="Right"><input type="text" maxlength="7" size="7" tabindex="2" style="TEXT-ALIGN: right" onblur="recalc"></td>
<td nowrap="nowrap" align="Right">900.000</td>
</tr>
<tr class="GridItem">
<td align="Left"><a href="ItemDetails.aspx?id=
<td nowrap="nowrap" align="Center">3</td>
<td nowrap="nowrap" align="Right">100.000</td>
<td nowrap="nowrap" align="Right"><input type="text" maxlength="7" size="7" tabindex="2" style="TEXT-ALIGN: right" onblur="recalc"></td>
<td nowrap="nowrap" align="Right">300.000</td>
</tr>
<tr class="GridFtr" align="Left" valign="Top">
<td align="Left" valign="Top"> </td>
<td nowrap="nowrap" valign="Top"> </td>
<td> </td>
<td align="Right" valign="Top"> </td>
<td align="Right" valign="Top"> </td>
<td align="Right" valign="Top">Totals</td>
<td nowrap="nowrap" align="Right" valign="Top">1,200.052</td
</tr>
</table>
This should come out as:
Item Quantity Cost1 Cost2 Your Cost CalcCost
Item1 1 10 0.052 0.052
Item2 1 900 900
Item3 3 100 300
Totals 1200.052
Your Cost is the column that the user can change. CalcCost needs to be a column of subtotals with a calculated grand total
ASKER
The asp.net datagrid looks like this before it is rendered:
<asp:datagrid id="DbGrid_Ingredients" runat="server" width="100%" gridlines="Horizontal" selectedindex="0" autogeneratecolumns="False " horizontalalign="Left" caption="Ingredients" captionalign="Top" pagesize="5" cssclass="Grid" showfooter="True" itemstyle-cssclass="GridIt em" headerstyle-cssclass="Grid Hdr" pagerstyle-cssclass="GridF tr">
<footerstyle horizontalalign="Left" cssclass="GridHdr" verticalalign="Top"></foot erstyle>
<itemstyle cssclass="GridItem"></item style>
<headerstyle cssclass="GridFtr"></heade rstyle>
<columns>
<asp:hyperlinkcolumn datanavigateurlfield="Item ID" datanavigateurlformatstrin g="ItemDet ails.aspx? id={0}"
datatextfield="Item" headertext="Item">
<headerstyle horizontalalign="Left"></h eaderstyle >
<itemstyle horizontalalign="Left"></i temstyle>
<footerstyle horizontalalign="Left" verticalalign="Top"></foot erstyle>
</asp:hyperlinkcolumn>
<asp:boundcolumn datafield="Quantity" readonly="True" headertext="Quantity<BR >needed ">
<headerstyle horizontalalign="Center">< /headersty le>
<itemstyle wrap="False" horizontalalign="Center">< /itemstyle >
<footerstyle wrap="False" verticalalign="Top"></foot erstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="Cost1" readonly="True" headertext="Bazaar<BR&g t;Cost" dataformatstring="{0:N3}">
<headerstyle horizontalalign="Right" width="6em"></headerstyle>
<itemstyle wrap="False" horizontalalign="Right"></ itemstyle>
<footerstyle horizontalalign="Right" verticalalign="Top"></foot erstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="Cost2" readonly="True" headertext="NPC<BR>C ost" dataformatstring="{0:N3}">
<headerstyle horizontalalign="Right" width="6em"></headerstyle>
<itemstyle wrap="False" horizontalalign="Right"></ itemstyle>
<footerstyle horizontalalign="Right" verticalalign="Top"></foot erstyle>
</asp:boundcolumn>
<asp:templatecolumn headertext="Your cost" footertext="Totals">
<headerstyle horizontalalign="Right"></ headerstyl e>
<itemstyle wrap="False" horizontalalign="Right"></ itemstyle>
<itemtemplate>
<input type="text" maxlength="7" size="7" tabindex="2" style="TEXT-ALIGN: right" onblur="recalc">
</itemtemplate>
<footerstyle horizontalalign="Right" verticalalign="Top"></foot erstyle>
</asp:templatecolumn>
<asp:boundcolumn readonly="True" headertext="CalcCost" dataformatstring="{0:N3}">
<headerstyle horizontalalign="Right" width="6em"></headerstyle>
<itemstyle wrap="False" horizontalalign="Right"></ itemstyle>
<footerstyle wrap="False" horizontalalign="Right" verticalalign="Top"></foot erstyle>
</asp:boundcolumn>
</columns>
<pagerstyle cssclass="GridFtr"></pager style>
</asp:datagrid>
<asp:datagrid id="DbGrid_Ingredients" runat="server" width="100%" gridlines="Horizontal" selectedindex="0" autogeneratecolumns="False
<footerstyle horizontalalign="Left" cssclass="GridHdr" verticalalign="Top"></foot
<itemstyle cssclass="GridItem"></item
<headerstyle cssclass="GridFtr"></heade
<columns>
<asp:hyperlinkcolumn datanavigateurlfield="Item
datatextfield="Item" headertext="Item">
<headerstyle horizontalalign="Left"></h
<itemstyle horizontalalign="Left"></i
<footerstyle horizontalalign="Left" verticalalign="Top"></foot
</asp:hyperlinkcolumn>
<asp:boundcolumn datafield="Quantity" readonly="True" headertext="Quantity<BR
<headerstyle horizontalalign="Center"><
<itemstyle wrap="False" horizontalalign="Center"><
<footerstyle wrap="False" verticalalign="Top"></foot
</asp:boundcolumn>
<asp:boundcolumn datafield="Cost1" readonly="True" headertext="Bazaar<BR&g
<headerstyle horizontalalign="Right" width="6em"></headerstyle>
<itemstyle wrap="False" horizontalalign="Right"></
<footerstyle horizontalalign="Right" verticalalign="Top"></foot
</asp:boundcolumn>
<asp:boundcolumn datafield="Cost2" readonly="True" headertext="NPC<BR>C
<headerstyle horizontalalign="Right" width="6em"></headerstyle>
<itemstyle wrap="False" horizontalalign="Right"></
<footerstyle horizontalalign="Right" verticalalign="Top"></foot
</asp:boundcolumn>
<asp:templatecolumn headertext="Your cost" footertext="Totals">
<headerstyle horizontalalign="Right"></
<itemstyle wrap="False" horizontalalign="Right"></
<itemtemplate>
<input type="text" maxlength="7" size="7" tabindex="2" style="TEXT-ALIGN: right" onblur="recalc">
</itemtemplate>
<footerstyle horizontalalign="Right" verticalalign="Top"></foot
</asp:templatecolumn>
<asp:boundcolumn readonly="True" headertext="CalcCost" dataformatstring="{0:N3}">
<headerstyle horizontalalign="Right" width="6em"></headerstyle>
<itemstyle wrap="False" horizontalalign="Right"></
<footerstyle wrap="False" horizontalalign="Right" verticalalign="Top"></foot
</asp:boundcolumn>
</columns>
<pagerstyle cssclass="GridFtr"></pager
</asp:datagrid>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is absolutely wonderful! ... except I still have a small problem ... the parseFloat(currInp.value); ALWAYS comes out as NaN ... if I look at the html source, the input value='1234.567' (or whatever) rather than value=1234.567, so I'm guessing it can't convert from a string to a float automatically ?
ASKER
.... or is it cos its single quotes rather than double quotes ? (the values read from the non-input table cells come out fine)
ASKER
... scrap that ... my boo-boo ... dropped the .value off the end - duh!
ASKER
Works like a dream now!!!
Great, glad to hear you got what you wanted. Thanks for the points.
<html>
<head>
<script type="text/javascript">
// stores which column has the values to be totalled
// note, first colum = 0, second = 1, third = 2, ...
column = 1;
window.onload = init;
function init()
{
tbl = document.getElementById("t
for (var i = 1, n = tbl.rows.length - 1; i < n; i ++)
{
var currCell = tbl.rows[i].cells[column];
currCell.getElementsByTagN
}
}
function updateTotal()
{
var totalAmt = 0;
for (var i = 1, n = tbl.rows.length - 1; i < n; i ++)
{
var currCell = tbl.rows[i].cells[column];
var currInp = currCell.getElementsByTagN
if (currInp.value != "")
totalAmt += parseFloat(currInp.value);
}
document.getElementById("t
}
</script>
</head>
<body>
<table id="theDataGrid">
<tr>
<th>Product</th>
<th>Cost</th>
</tr>
<tr>
<td>Prod 1</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Prod 2</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Prod 3</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Prod 4</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Total</td>
<td id="total"></td>
</tr>
</table>
</body>
</html>