1.this
$.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; } }