skip to Main Content

While using check box section in asp.net ,grid view,with the following code.
not able to calculate the sum of the amount ,
eg,

  • if the grid row having the value with 1,001,870.00 then,getting the
    result as 1

  • if the grid row having the value with 976,000 then,
    getting the result as 976,000

am using the following script function,

function GetSelected() {
    debugger;
    
    var grid = document.getElementById("<%=grvGroup.ClientID%>");

    var checkBoxes = grid.getElementsByTagName("INPUT");
    var amountFC = 0;
    var amountVAT = 0;
    var amountSUMTOT = 0;
    var countSelect = 0;
    var totalValAmount = 0;
    
    if (grid.rows.length > 0) {

        for (var i = 1; i < grid.rows.length; i++) {
            var checkBoxes = grid.rows[i].getElementsByTagName('input');
            var checkedid = checkBoxes[0].getAttribute('id');
            var checkbox = document.getElementById(checkedid);

            if (checkbox.checked) {
                Amt = grid.rows[i].cells[5].innerText.replace(",", "");
                amountFC += parseFloat(Amt);
                Amt = grid.rows[i].cells[6].innerText.replace(",", "");
                amountVAT += parseFloat(Amt);
                
                countSelect++;
            }
        }
        jQuery('#<%=txtTotalAmount.ClientID %>').val(amountFC);
        jQuery('#<%=txtTotalVat.ClientID %>').val(amountVAT);
        var totalAmount = parseFloat(jQuery('#<%=txtTotalAmount.ClientID %>').val());
        var totalVat = parseFloat(jQuery('#<%=txtTotalVat.ClientID %>').val());
        var totalSum = totalAmount + totalVat;


        jQuery('#<%=txtPayableAmount.ClientID %>').val(totalSum);

    }
    if (grid.rows.length - 1 == countSelect) {
        document.getElementById("ContentPlaceHolder1_checkallselect").checked = true;
    }
    else {
        document.getElementById("ContentPlaceHolder1_checkallselect").checked = false;
    }
}

2

Answers


  1. In the documentation of JS function replace() we can read the following:

    A string pattern will only be replaced once. To perform a global
    search and replace, use a regular expression with the g flag, or use
    replaceAll() instead.

    These are the two options you have to remove all , from your number.

    ReplaceAll()

    grid.rows[i].cells[5].innerText.replaceAll(",", "");
    

    Using REGEX

    grid.rows[i].cells[5].innerText.replace(/,/g, "");
    

    Docs

    Replace

    Replace all

    Login or Signup to reply.
  2. Please follow DRY(Don’t Repeat Yourself) principle.

    Best to create a polyfill on window object that named i.e. parseFloatFromString().
    Put this code on load of your project.

    For your case:

    parseFloatFromString(grid.rows[i].cells[5].innerText);
    
    // polyfill parseFloatFromString() now available in window object as parseFloat()
    
    if(!('parseFloatFromString' in window)){
        Window.prototype.parseFloatFromString = function(numberInString){
            try {
                return +(numberInString.replaceAll(",", ""));
            } catch ( err ) { console.error(err) };
        }
    }
    
    console.log(parseFloatFromString('1,001,870.22'))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search