jqgrid 根据其他列单元格值计算总列单元格数量
使用jqgrid的setFooterData函数计算总金额后,如这个问题 ,这是我的网格和函数:
<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) 时,我才想对金额求和。
这怎么能做到呢?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我制作了 新演示,其中我使用另一种方式枚举单元格网格(参见答案)。
在演示中,我将
getTextFromCell
和calculateTotal
函数的代码修改为以下内容:现在,总计行中将显示“金额”列中所有值的总和,但只有“状态”列中具有“已确认”的行才会被考虑。
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
andcalculateTotal
functions to the following: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.