Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5928117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:15:21+00:00 2026-05-22T14:15:21+00:00

After using jqgrid’s setFooterData function to calculate the total amount like in this question

  • 0

After using jqgrid’s setFooterData function to calculate the total amount like in this question , this is my grid and functions:

<script type="text/javascript">

    function calculateTotal(grid_ , column_id_)
    {
        var _total_amount = 0;
        var i = getColumnIndexByName(grid_ , column_id_); 

        // TO ADD - calculate only if row "status" cell = "1:Confirmed"
        $("tbody > tr.jqgrow > td:nth-child("+(i+1)+")" , grid_[0]).each(function()
        {
            _total_amount += Number(getTextFromCell(this));
        });

        return _total_amount;
    }

    function getColumnIndexByName(grid_ , column_id_)
    {
        var cm = grid_.jqGrid('getGridParam','colModel');
        for (var i=0,l=cm.length; i<l; i++)
        {
            if (cm[i].name===column_id_)
            {
                return i; // return the index
            }
        }
        return -1;
    }

    function getTextFromCell(cellNode)
    {
        return cellNode.childNodes[0].nodeName === "INPUT"?
                  cellNode.childNodes[0].value:
                  cellNode.textContent || cellNode.innerText;
    }

    $(document).ready(function () {

        var grid = $("#list"),lastSel;

        grid.jqGrid({
            url:'url',
            datatype: "xml",
            loadonce:true ,
            async: false,
            colNames: ['Inv No', 'Name' , 'Amount' , 'Status'],
            colModel: [
                { name: 'id', index: 'id', width: 65, sorttype: 'int', hidden: true },
                { name: 'name', index: 'name', editable: true, width: 90, sortable: false },
                { name: 'amount', index: 'amount', editable: true, width: 70, formatter: 'number', align: 'right', sortable: false },
                {name:'status',index:'status', width:90, sorttype:"int" , editable:true,
                  edittype:"select", formatter:'select',
                  editoptions:
                  {
                     value:"1:Confirmed ;2:Open ; 3:Rejected" ,
                     dataInit: function(elem)
                     {
                        $(elem).width(90);
                     }
                  }
                },
            ],
            rowNum: 1000,
            pager: '#pager',
            viewrecords: true,
            sortorder: "desc",                
            height: "100%",                
            footerrow:true,
            xmlReader: {
                                          root:"rows",
                                          row:"row",
                                          repeatitems:false
                                       },
            shrinkToFit: false,
            beforeSelectRow: function(row_id_, e)
            {
            },
            onSelectRow: function(id)
            {
                 grid.jqGrid('saveRow' , lastSel , false, 'clientArray');
                 grid.editRow(id , false);
                 lastSel=id;
            },
            loadComplete:function()
            {
                grid.jqGrid('footerData' , 'set' , {name:'TOTAL:' , amount: calculateTotal(grid , 'amount')});
            }
        });            
    });    
</script>

My question Is how can I calculate the total amount sum depending on the value that is iniside the “status” combobox. I want to sum the amount only if the value iniside the “status” cell is equels to “Confirmed” (=1).

How can this be done?

Thank’s.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:15:22+00:00Added an answer on May 22, 2026 at 2:15 pm

    I made new demo where I used another way to enumerate the cells in the grid (see the answer).

    In the demo I modified the code of getTextFromCell and calculateTotal functions to the following:

    var getTextFromCell = function(cellNode) {
            if (cellNode.childNodes[0].nodeName.toUpperCase() === "SELECT") {
                var selOptions = $("option:selected", cellNode);
                if (selOptions.length>0) {
                    return selOptions.text();
                }
            }
            return cellNode.childNodes[0].nodeName.toUpperCase() === "INPUT"?
                   cellNode.childNodes[0].value:
                   cellNode.textContent || cellNode.innerText;
        },
        calculateTotal = function() {
            var totalAmount = 0,
                iAmount=getColumnIndexByName(grid,'amount'),
                iStatus=getColumnIndexByName(grid,'status');
            var i=0, rows = grid[0].rows, rowsCount = rows.length, row, status;
    
            for(;i<rowsCount;i++) {
                row = rows[i];
                if (row.className.indexOf('jqgrow') !== -1) {
                    status = getTextFromCell(row.cells[iStatus]);
                    if (status === "Confirmed") {
                        totalAmount += Number(getTextFromCell(row.cells[iAmount]));
                    }
                }
            }
    
            grid.jqGrid('footerData','set',{name:'TOTAL Confirmed',amount:totalAmount});
        };
    

    Now in the total lines will be displayed the sum of all values from the ‘Amount’ columns, but only the rows having “Confirmed” in the ‘Status’ column will be taken in the consideration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jqGrid with mvc 2 like this: jQuery(#extension_grid).jqGrid({ url: '/Extension/Report', datatype: json, direction:
I've a jqGrid v3.7.2, after hiding a column using .jqGrid('hideCol', infoName); and setting the
I'm having a problem with JQGrid using ASP.NET MVC. I'm trying to follow this
I am using jqgrid 3.8. I have a grid which is having some editable
In jqGrid, after a users chooses to remove a column(s), a grid's width becomes
I am using jqgrid with clientArray option for editing;My grid is using paging; Will
I need to set some event handlers for jqGrid's edit events after the grid
I am using jqgrid and the toolbar filter. After filtering I want to reload
I'm building a simple mvc3 app using jqgrid. My form currently calls a function
The expand/collapse button does not work properly with jqGrid when using the re-sizable grid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.