gpt4 book ai didi

javascript - 使用 JavaScript 进行 GridView 计算

转载 作者:行者123 更新时间:2023-11-27 23:10:01 25 4
gpt4 key购买 nike

嗨,因为 GridView 将在浏览器中呈现为表格

如何在 asp.net grid-view 上应用下面的 JavaScript

HTML 表格演示:jsfiddle

Javascript代码

注意这里首先计算总计,然后将每行总计除以总计

<script>
$(document).ready(function () {
$(".txtMult input").keyup(multInputs);

function multInputs() {

var $mult = 0;
// calculate Grand total
$("tr.txtMult").each(function () {
// get the values from this row:
var $val1 = $('.val1', this).val();
var $val2 = $('.val2', this).val();
var $total = ($val1) * ($val2);
$mult += $total;
});

// for each row:
$("tr.txtMult").each(function () {
// get the values from this row:
var $val1 = $('.val1', this).val();
var $val2 = $('.val2', this).val();
var $total = ($val1) * ($val2);
var $Percentage = (($total / $mult)).toFixed(2);
$('.percentage', this).text($Percentage);
$('.multTotal', this).text($total);
});
$("#grandTotal").text(mult);
}
});
</script>

GridView 代码

<asp:GridView ID="testgrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtCalcQuantity" placeholder="Enter Quantity" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<asp:TextBox ID="txtCalcUnitprice" placeholder="Enter Unit Price" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Percentage">
<ItemTemplate>
<asp:Label ID="lblPercentage" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>

</asp:GridView>

非常感谢您的帮助

最佳答案

Grid View:

<asp:GridView ID="testgrid" runat="server" AutoGenerateColumns="False" OnRowDataBound="testgrid_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtCalcQuantity" CssClass="val1" placeholder="Enter Quantity" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<asp:TextBox ID="txtCalcUnitprice" CssClass="val2" placeholder="Enter Unit Price" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" CssClass="multTotal" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Percentage">
<ItemTemplate>
<asp:Label ID="lblPercentage" CssClass="percentage" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>

</asp:GridView>
<asp:Label ID="grandTotal" runat="server"></asp:Label>

GV backend:

protected void testgrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.CssClass = "txtMult";

}
}

Javascript :

<script>
$(document).ready(function () {
$(".txtMult input").keyup(multInputs);

function multInputs() {

var $mult = 0;
// calculate Grand total
$("tr.txtMult").each(function () {
// get the values from this row:
var $val1 = $('.val1', this).val();
var $val2 = $('.val2', this).val();
var $total = ($val1) * ($val2);
$mult += $total;
});

// for each row:
$("tr.txtMult").each(function () {
// get the values from this row:
var $val1 = $('.val1', this).val();
var $val2 = $('.val2', this).val();
var $total = ($val1) * ($val2);
var $Percentage = (($total / $mult)).toFixed(2);
$('.percentage', this).text($Percentage);
$('.multTotal', this).text($total);
});
$("#<%=grandTotal.ClientID %>").text($mult);
}
});

关于javascript - 使用 JavaScript 进行 GridView 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36273261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com