现在由于业务需求对grid进行列汇总,但是问题是gird有一列是默认数据。通过合计后该列始终显示在footer里。怎么都去除不掉。
下面是源码,图片是效果。
<script type="text/javascript">
$(function () {
CreateTable();
$('#tt').datagrid('statistics');//sum
});
function CreateTable() {
$('#tt').datagrid({
width: 799,
height: 250,
fitColumns: false,
nowrap: false,
collapsible: true,
collapsed: false,
singleSelect: true,
showFooter: true,
pagination: false,
columns: [[
{
field: 'ListNo', title: '序号', width: 40, editor: {
type: 'validatebox',
options: {
required: true
}
}
},
{
field: 'RoadName', title: '路名', width: 120,
editor: {
type: 'validatebox',
options: {
required: true
}
}
},
{
field: 'Start', title: '起点', width: 120, sortable: false,
editor: {
type: 'validatebox',
options: {
required: true
}
}
},
{
field: 'End', title: '终点', width: 120, sortable: false,
editor: {
type: 'validatebox',
options: {
required: true
}
}
},
{
field: 'Manhole', title: '窨井', width: 100, sortable: false,
sum:true,
editor: {//设置其为可编辑
type: 'numberbox',//设置编辑格式
options: {
required: true//设置编辑规则属性
}
}
},
{
field: 'Well', title: '边井', width: 100, sortable: false,
sum: true,
editor: {//设置其为可编辑
type: 'numberbox',//设置编辑格式
options: {
required: true//设置编辑规则属性
}
}
},
{
field: 'Shift', title: '作业班次', width: 100, sortable: false,
editor: {//设置其为可编辑
type: 'combobox',
options: {
valueField: 'id',
textField: 'name',
data: [{ id: '白班', name: '白班' },
{ id: '晚班', name: '晚班' }],
required: true
}
}
},
{
field: 'action', title: '操作', width: 90, align: 'center',
formatter: function (value, row, index) {
if (row.editing) {
var s = '<a href="#" onclick="saverow(' + index + ')">保存</a> ';
var c = '<a href="#" onclick="cancelrow(' + index + ')">取消</a>';
return s + c;
} else {
var e = '<a href="#" onclick="editrow(' + index + ')">编辑</a> ';
var d = '<a href="#" onclick="deleterow(' + index + ')">删除</a>';
return e + d;
}
},sum:true
}
]],
toolbar: [
"-",{
text: '增加计划',
iconCls: 'icon-add',
handler: addrow
}
/*, "-", {
text: '保存计划',
iconCls: 'icon-save',
handler: saveall
}, "-",{
text: '全部取消',
iconCls: 'icon-cancel',
handler: cancelall }*/
],
onBeforeEdit: function (index, row) {
row.editing = true;
$('#tt').datagrid('refreshRow', index);
editcount++;
},
onAfterEdit: function (index, row) {
row.editing = false;
$('#tt').datagrid('refreshRow', index);
editcount--;
},
onCancelEdit: function (index, row) {
row.editing = false;
$('#tt').datagrid('refreshRow', index);
editcount--;
}
});
}
var editcount = 0;
var lastIndex;
function editrow(index) {
$('#tt').datagrid('beginEdit', index);
}
function deleterow(index) {
$.messager.confirm('确认', '是否真的删除?', function (r) {
if (r) {
$('#tt').datagrid('deleteRow', index);
$('#tt').datagrid('statistics');
}
});
}
function saverow(index) {
$('#tt').datagrid('endEdit', index);
$('#tt').datagrid('statistics');
}
function cancelrow(index) {
$('#tt').datagrid('cancelEdit', index);
$('#tt').datagrid('statistics');
}
function addrow() {
$('#tt').datagrid('endEdit', lastIndex);
var CurIndex = $('#tt').datagrid('getRows').length + 1;
if (editcount > 0) {
$.messager.alert('警告', '当前还有' + editcount + '记录正在编辑,不能增加记录。');
return;
}
$('#tt').datagrid('appendRow', {
ListNo: CurIndex,
RoadName: '',
Start: '',
End: '',
Manhole: 0,
Well: 0,
Shift:'白班'
});
lastIndex = $('#tt').datagrid('getRows').length - 1;
$('#tt').datagrid('selectRow', lastIndex);
$('#tt').datagrid('beginEdit', lastIndex);
}
/*
function saveall() {
$('#tt').datagrid('acceptChanges');
}
function cancelall() {
$('#tt').datagrid('rejectChanges');
}*/
//datagrid合计扩展
$.extend($.fn.datagrid.methods, {
statistics: function (jq) {
var opt = $(jq).datagrid('options').columns;
var rows = $(jq).datagrid("getRows");
var footer = new Array();
footer['sum'] = "";
for (var i = 0; i < opt[0].length; i++) {
if (opt[0][i].sum) {
footer['sum'] = footer['sum'] + sum(opt[0][i].field) + ',';
}
}
var footerObj = new Array();
if (footer['sum'] != "") {
var tmp = '{' + footer['sum'].substring(0, footer['sum'].length - 1) + "}";
var obj = eval('(' + tmp + ')');
if (obj[opt[0][0].field] == undefined) {
footer['sum'] += '"' + opt[0][0].field + '":"<b>合计:</b>"';
obj = eval('({' + footer['sum'] + '})');
} else {
obj[opt[0][0].field] = "<b>合计:</b>" + obj[opt[0][0].field];
}
footerObj.push(obj);
}
if (footerObj.length > 0) {
$(jq).datagrid('reloadFooter', footerObj);
////test
//for (var i = 0, l = footerObj.length; i < l; i++) {
// for (var key in footerObj[i]) {
// alert(key + ':' + footerObj[i][key]);
//}
//}
}
function sum(filed) {
var sumNum = 0;
for (var i = 0; i < rows.length; i++) {
sumNum += Number(rows[i][filed]);
}
return '"' + filed + '":"' + sumNum.toFixed(0) + '"';
}
}
});
</script>
HTML代码
<table id="tt" style="margin-top:10px;" ></table>