简体   繁体   中英

How to achieve this calculation using javascript

First of all thanks for viewing this question.Now,let me show you what I want

在此处输入图片说明

as you could see there are two columns in my table.. Now, if someone put 30 or 300 then the table will look like this

在此处输入图片说明

Basically,the calculation will go like this way.

Now, I am doing this calculation using javascript and I don't figure out how to get this.

Here, is my code that I have tried..

//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;
                                }
                            }
                        }
                    }
                }
            }

Let me tell you what these variables does,

  • enterdValue = This will get an input from user.
  • grid = rendered as table and to loop thru I need this.
  • rowTotAmount = TotalInvAmount as shown in grid.
  • alreadyPaid = paidAmount as shown in grid.

Help needed to achieve this :) Thanks & regards

I have figured out to solve it :) hence posting it here.

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;
                    }
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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