jqgrid calculate total column cells amount depending on other column cell values

前端 未结 1 1511
死守一世寂寞
死守一世寂寞 2020-12-18 16:56

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



        
相关标签:
1条回答
  • 2020-12-18 17:22

    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 讨论(0)
提交回复
热议问题