简体   繁体   English

表中的计算字段仅针对第一行进行计算-遍历表中的行以计算列值

[英]Calculated Fields in table only calculating for first row - Looping through rows in table to calculate column values

I have a list of records in a table in my view that has a calculated column. 我在视图中的表中有一个记录列表,该表中有一个计算列。

记录清单

From the image, the column in question is the Mark-Up column, and its value is generated/calculated from Price, Casecost and Casesize. 在图像中,所讨论的列是“加价”列,其值是根据“价格”,“案例成本”和“案例大小”生成/计算的。 This column is only visual for the user and will never be saved in db/table. 该列仅对用户可见,并且永远不会保存在db / table中。 Value is calculated when page is loaded (next/previous for more than 10 rows). 加载页面时计算值(下一页/上一页超过10行)。 For each page on with the records its only calculating the value on the first row as seen in the picture. 对于带有记录的每一页,它仅计算第一行中的值,如图所示。

CSHTML I have 我有CSHTML

@Html.TextBox("q_markup", null, new { @class = "calc markupclass", @readonly = "readonly" })

Javascript Java脚本

function calculate() {

            //Fields that are used for calculations (declare variables)
            var casecost = parseFloat($('#item_q_casecost').val());
            var casesize = parseFloat($('#item_q_casesize').val());
            var price = parseFloat($('#item_q_sellprice').val());

            //calculations
            var unitcost = casecost / casesize;
            var markup = ((price - unitcost) / unitcost) * 100;

            //put calculated value into field
            $('#q_markup').val(markup.toFixed(2));           
    }

    $(document).ready(function () {
        calculate();
    });

What I have since tried is to have a loop in my calculate function in the following manner without any success 从那以后,我尝试以以下方式在我的calculate函数中循环,但没有成功

function calculate() {

//gather all markup fields on page
var rows = document.getElementsByClassName("markupclass");

//cycle through each rows and calculate
for (i = 0; i < rows.length; i++) {

    //do my calculations here
   }
}

How can I get this calculate that field for all rows? 我怎样才能计算出所有行的字段?

Not exactly sure, but you could try fixing your for loop 不确定,但是您可以尝试修复for循环

function calculate() {

//gather all markup fields on page
var rows = document.getElementsByClassName("markupclass");

//ADDED - middle conditional - cycle through each rows and calculate
for (i = 0; i < rows.length; i++) {

    //do my calculations here
   }
}

This is how I managed to solve this for anyone who comes across this issue 这就是我设法为遇到此问题的任何人解决的方法

First with the Table I have ( Note: class 'searchTable' ) 首先使用表( 注意:类“ searchTable”

<table id="ListTable" class="table searchTable">
    <tr>

Looping through the rows and getting the required values for each field 遍历行并获取每个字段的必需值

function calculate() {
        $('.searchTable tr').each(function () {

            //Fields that are used for calculations (declared variables)
            var casecost = $(this).find('#item_q_casecost').val();
            var casesize = $(this).find('#item_q_casesize').val();
            var price = $(this).find('#item_q_sellprice').val();
            var vat = $(this).find('#item_viewVat').val();

            //calculations - PREREQUISITES 
            var unitcost = casecost / casesize;
            var profit = (price - unitcost) - ((vat / 100) * price);

            //calculations - FIELD VALUES
            var markup = ((price - unitcost) / unitcost) * 100;
            var gprofit = (profit / price) * 100;

            //assign calculated values to table fields (columns)
            $(this).find('#q_markup').val(markup.toFixed(2));
            $(this).find('#q_grossprofit').val(gprofit.toFixed(2));

        });           
    }

GP and Mark-Up are the calculated field columns (in image) GP和标记是计算出的字段列(图像中)

最后结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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