簡體   English   中英

如何使用javascript實現此計算

[英]How to achieve this calculation using javascript

首先,感謝您查看此問題。現在,讓我告訴您我想要的是什么

在此處輸入圖片說明

如您所見,我的表格中有兩列。現在,如果有人輸入30或300,則表格將如下所示

在此處輸入圖片說明

基本上,計算將像這樣。

現在,我正在使用javascript進行此計算,但我不知道如何獲得此結果。

這是我嘗試過的代碼。

//NOTE: This is an asp grid
        //enterdValue = the value I am putting.
        function FirstPriority(sender, eventArgs) {
            debugger;
            var loop = true;
            var loopcounter = 0;
            if (isNaN($find("<%= txt_payingAmt.ClientID %>").get_textBoxValue())) {
                eventArgs.set_cancel(true);
            }
            else {
                let enterdValue = parseFloat($find("<%= txt_payingAmt.ClientID %>").get_textBoxValue());
                var grid = $find("<%=rgv_INVList.ClientID%>");
                //loop through..
                for (var row = 0; row < grid.MasterTableView.get_dataItems().length ; row++) {
                    var remainingTot = 0;
                    loopcounter += 1;
                    var rowTotAmount = parseFloat(grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "GrandTotal").innerHTML);
                    var alreadyPaid = parseFloat(grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML);
                    if (loopcounter == 1) {
                        if ((alreadyPaid + enterdValue) <= rowTotAmount) {
                            grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML = alreadyPaid + enterdValue;
                            break;
                        }
                    }
                    else if (loopcounter > 1) {
                        if ((alreadyPaid + enterdValue) == rowTotAmount) {
                            grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML = rowTotAmount;
                            break;
                        }
                        else {
                            grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML = remainingTot;
                            break;
                        }
                    }
                    else {
                        if (loop) {
                            remainingTot = (alreadyPaid + enterdValue) - rowTotAmount;
                            grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML = rowTotAmount;
                            if (grid.MasterTableView.get_dataItems().length != loopcounter) {
                                if (remainingTot <= grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row + 1], "PayingAmount").innerHTML) {
                                    grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row + 1], "PayingAmount").innerHTML = alreadyPaid + enterdValue;
                                    loop = false;
                                }
                            }
                        }
                    }
                }
            }

讓我告訴你這些變量的作用

  • enterdValue =這將從用戶那里得到輸入。
  • 網格=呈現為表,並通過我需要循環。
  • rowTotAmount = TotalInvAmount,如網格所示。
  • 如網格中所示,hasPaid = paidAmount。

實現此目標所需的幫助:)謝謝與問候

我想出解決的方法:)因此,將其發布在這里。

function FirstPriority(args) {
                debugger;
                resetGrid();
                var paidAmount, totAmount, paidAmountHdn, payingAmount, remainingBalance = 0;
                payingAmount = parseFloat(args.value);
                var grid = $find("<%=rgv_INVList.ClientID%>");
                if (!isNaN(payingAmount)) {
                    remainingBalance = payingAmount;
                    for (var row = 0; row < grid.MasterTableView.get_dataItems().length ; row++) {
                        paidAmount = parseFloat(grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerText); //grid.MasterTableView.get_dataItems()[0]._element.cells[1].innerText
                        totAmount = parseFloat(grid.MasterTableView.get_dataItems()[row]._element.cells[4].innerText);
                        paidAmountHdn = parseFloat(grid.MasterTableView.get_dataItems()[row]._element.cells[7].innerText);
                        if (remainingBalance > 0) {
                            if (row == 0) {
                                if((paidAmount+payingAmount)>=totAmount)
                                {
                                    remainingBalance = (paidAmount + payingAmount) - totAmount;
                                    grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = totAmount;
                                }
                                else if ((paidAmount + payingAmount) <= totAmount) {
                                    remainingBalance = 0;
                                    grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = payingAmount+paidAmount;
                                    break;
                                }
                            }
                            else {
                                if (remainingBalance > totAmount) {
                                    grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = totAmount;
                                    remainingBalance = remainingBalance - totAmount;
                                }
                                else if(totAmount>=remainingBalance){
                                    grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML =  remainingBalance;
                                    remainingBalance = 0;
                                    break;
                                }
                            }
                        }
                    }
                }
                else {
                    for (var row = 0; row < grid.MasterTableView.get_dataItems().length ; row++) {
                        paidAmountHdn = parseFloat(grid.MasterTableView.get_dataItems()[row]._element.cells[7].innerText);
                        grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = paidAmountHdn;
                    }
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM