jqgrid根据其他列单元格值计算总列单元格数量

时间:2011-05-15 11:33:14

标签: jqgrid

使用jqgrid的setFooterData函数来计算this question中的总量,这是我的网格和函数:

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

我的问题是如何根据“状态”组合框内的值计算总金额。我想仅在“状态”单元格中的值等于“已确认”(= 1)时才对该数量求和。

如何做到这一点?

感谢的。

1 个答案:

答案 0 :(得分:2)

我做了new demo,我用另一种方法来枚举网格中的单元格(参见the answer)。

在演示中,我将getTextFromCellcalculateTotal函数的代码修改为以下内容:

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

现在总行中将显示“金额”列中所有值的总和,但只会考虑“状态”列中“已确认”的行。