1.
$.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, 1) + ',';
}
}
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>"';//第0列显示为合计
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);
}
function sum(filed) {
var sumNum = 0;
var str = "";
for (var i = 0; i < rows.length; i++) {
var num = rows[i][filed];
sumNum += Number(num);
}
return '"' + filed + '":"' + sumNum.toFixed(2) + '"';
}
}
});
/* 商品列表 */
var posid = {$data.posid};
objs.datagrid = $("#datagrid").datagrid({
url : CONFIG.getGoodsListurl,
toolbar : '#toolbar',
fit : true,
border : false,
striped : true,
loadMsg : '正在加载数据,请稍后...',
rownumbers : true,
singleSelect:true,
pagination:false,
pageSize:20,
showFooter:true,
onClickRow: onClickRow,
columns : [CONFIG.COLUMNS],
queryParams: {
posid:posid
},
onLoadSuccess:function (data){
$('#datagrid').datagrid('statistics');
$(this).datagrid('unselectAll');
$(this).datagrid('selectRow',0);
}
});
CONFIG.COLUMNS = [{ field : 'code', title : '{:L("商品编号")}', width : 100, align : 'right' },{ field : 'mgname', title : '{:L("商品名称")}', width : 180 },{ field : 'specname', title : '{:L("规格")}', width : 150, },{ field : 'mgtname', title : '{:L("分类")}', width : 170, },{ field : 'dhprice', title : '{:L("单价")}', width : 100, align : 'right' },{ field : 'goodsno', title : '{:L("订货数量")}', width : 120, sum : true, align : 'right' },{ field : 'qgoodsno', title : '{:L("配送数量")}', width : 100, editor:{ type:'numberbox', options:{ max:999999.99, precision:2, min:0 } } },{ field : 'dhunit', title : '{:L("出入库单位")}', width : 120 },{ field : 'tprice', title : '{:L("小计")}', width : 120, sum : true, align : 'right' },{ field : 'remarks', title : '{:L("备注")}', width : 150, }];
/* 启用可编辑表格*/ var editIndex = undefined; function onClickRow(index){ if (editIndex == index)return false; if (endEditing()){ objs.datagrid.datagrid('selectRow', index).datagrid('beginEdit', index); editIndex = index; } else { objs.datagrid.datagrid('selectRow', editIndex); } } /* 完成可编辑*/ function endEditing(){ if(editIndex == undefined){return true;} if(objs.datagrid.datagrid('validateRow', editIndex)){ objs.datagrid.datagrid('endEdit', editIndex); editIndex = undefined; return true; }else { return false; } }