easyui的datagrid生成合并行,合计计算价格

easyui生成合并行,合计计算价格

小弟最近遇到easyui的datagrid合并行,并且计算价格小计和区域小计,百度了代码,也看不懂,百度了很多,都没找到方法,最后请大神帮忙了一下,然后终于弄出来了!!!
望大家喜欢,勿喷,本人也是刚毕业的小菜鸟,谢谢!!!



下面是html+js代码!!!
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Basic DataGrid - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="jquery-easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="jquery-easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="jquery-easyui/jquery.select.css">
    <script type="text/javascript" src="jquery-easyui/jquery.min.js"></script>
    <script type="text/javascript" src="jquery-easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="extEasyUI.js"></script>
    <style>
    </style>
    <script>

  var sortFlag = false;
	
  $(function(){

  	//加载数据
  	$("#datagrid").datagrid({
  		url : 'data.json',
  		border:false,
  		singleSelect : true,
  		rownumbers : true,
  		fit:true,
  		toolbar:"#toolbar",
  		columns:[[    
  	        {field:'divr_id',hidden:true,title:'序号',width:100},
  	        {field:'divr_user',title:'分区/分项',width:100},
  	        {field:'resource_name',title:'项目名称',width:100},
  	        {field:'divr_price',title:'单价',width:100},
  	        {field:'resource_unit',title:'数量单位',width:100},
  	        {field:'divr_num',title:'数量',width:100},
  	        {field:'divr_unit',title:'周期单位',width:100},
  	        {field:'divr_cycle',title:'周期',width:100},
  	        {field:'sum',title:'金额',width:100,
  	        	formatter: function (value, row, index) {
                    if (!value && (row.divr_price != "" && row.divr_num != "" && row.divr_cycle != "")) {
                        return row.divr_price * row.divr_num * row.divr_cycle;
                    }else if(!value && row.divr_price != ""){
                        return row.divr_price;
                    }else {
                        return value;
                    }
                }
			},
  	        {field:'divr_bak',title:'备注',width:100}
  	    ]],
        onSortColumn:function(sort, order){
            sortFlag = true;
            if("userName"==sort){
                $(this).datagrid("autoMergeCells",[sort]);
            }else{
                $(this).datagrid("autoMergeCells");
            }
        },
        onLoadSuccess: function(data){
            if(!sortFlag) $(this).datagrid("autoMergeCells",['divr_user']);<span style="white-space:pre">	</span>//这里填入需要合并的行  例如<pre name="code" class="html">['divr_user','<span style="font-family: Arial, Helvetica, sans-serif;">resource_name</span><span style="font-family: Arial, Helvetica, sans-serif;">']</span>

var rows = $('#datagrid').datagrid('getRows'); var currArea = ''; var total = 0; var len = rows.length - 1; var objectArray = []; var indexs = 0; //记录加了多少次小计 var sumAll = 0; $.each(rows, function (i, item) {//循环所有行记录 var sum = 0; //计算合计的sum if (item.divr_price != "" && item.divr_num != "" && item.divr_cycle != "") { sum = item.divr_price * item.divr_num * item.divr_cycle; } else { sum = item.divr_price; } if (!currArea) {//判断是还是是当前区域id,如果不是赋值当前记录的area_id给变量 currArea = item.area_id; } if (currArea == item.area_id) {//如果是当前的区域id,计数合计 total += sum; sumAll += sum; } else {//不是当前的,添加datagrid的一条行数据 objectArray.push({ index: i, row: { divr_user: '', divr_cycle:'<span class="subtotal">小计</span>', sum: '<span class="subtotal">' + total.toFixed(2) + '¥</span>' // 四舍五入为指定小数位数的数字 } }); currArea = item.area_id; total = sum; indexs += 1; } if (i == len) {//最后一行 objectArray.push({ index: i, row: { divr_user: '', divr_cycle:'<span class="subtotal">小计</span>', sum: '<span class="subtotal">' + total.toFixed(2) + '¥</span>' } }); } }); var num = 0; $.each(objectArray, function (i, item) {//循环所有待添加的插入行记录,对应插入位置是原始行数据对应记录的最后一行 if (i < objectArray.length-1) {//判断是否是最后一行 item.index = item.index + num; $('#datagrid').datagrid('insertRow', { index: item.index, // 索引从0开始 row: item.row }); num++; } else { $('#datagrid').datagrid('appendRow', item.row ); $('#datagrid').datagrid('appendRow', { divr_user:'<span class="subtotal">合计</span>', sum: '<span class="subtotal">' + sumAll.toFixed(2) + '¥</span>' } ); } }); } }); }); //合并单元格 $.extend($.fn.datagrid.methods, { autoMergeCells : function (jq, fields) { return jq.each(function () { var target = $(this); if (!fields) { fields = target.datagrid("getColumnFields"); } var rows = target.datagrid("getRows"); var i = 0, j = 0, temp = {}; for (i; i < rows.length; i++) { var row = rows[i]; j = 0; for (j; j < fields.length; j++) { var field = fields[j]; var tf = temp[field]; if (!tf) { tf = temp[field] = {}; tf[row[field]] = [i]; } else { var tfv = tf[row[field]]; if (tfv) { tfv.push(i); } else { tfv = tf[row[field]] = [i]; } } } } $.each(temp, function (field, colunm) { $.each(colunm, function () { var group = this; if (group.length > 1) { var before, after, megerIndex = group[0]; for (var i = 0; i < group.length; i++) { before = group[i]; after = group[i + 1]; if (after && (after - before) == 1) { continue; } var rowspan = before - megerIndex + 1; if (rowspan > 1) { target.datagrid('mergeCells', { index : megerIndex, field : field, rowspan : rowspan }); } if (after && (after - before) != 1) { megerIndex = after; } } } }); }); }); } }); </script></head><body><table id="datagrid"/></body></html><script></script>




下面的是中中间的数据!
[{"area_id":1,"divr_addate":null,"divr_bak":null,"divr_cycle":1,"divr_id":0,"divr_num":1,"divr_price":2.0,"divr_unit":null,"divr_update":null,"divr_user":"导示系统","project_id":0,"resource_id":0,"resource_image":"D:\\upload\\file\\sequipment\\201606291919531001.jpg","resource_name":"话筒","resource_unit":"1","type_no":null},
{"area_id":1,"divr_addate":null,"divr_bak":"?\u0085¨?¤?\/????\u008E\u0092","divr_cycle":10,"divr_id":0,"divr_num":0,"divr_price":0.0,"divr_unit":null,"divr_update":null,"divr_user":"导示系统","project_id":0,"resource_id":0,"resource_image":"d:\/upload\/file\/program\/20160715182159200101.jpg","resource_name":"段宇","resource_unit":"???","type_no":null},
{"area_id":1,"divr_addate":null,"divr_bak":null,"divr_cycle":1399,"divr_id":0,"divr_num":1,"divr_price":15.0,"divr_unit":null,"divr_update":null,"divr_user":"导示系统","project_id":0,"resource_id":0,"resource_image":"d:\/upload\/file\/handmade\/20160716165542700101.jpg","resource_name":"影像马克杯","resource_unit":"场\/份","type_no":null},
{"area_id":2,"divr_addate":null,"divr_bak":null,"divr_cycle":1,"divr_id":0,"divr_num":1,"divr_price":150.0,"divr_unit":null,"divr_update":null,"divr_user":"主入口区","project_id":0,"resource_id":0,"resource_image":"d:\/upload\/file\/exhibit\/20160716145653500101.jpg","resource_name":"胡桃夹子(小)","resource_unit":"个","type_no":null},
{"area_id":2,"divr_addate":null,"divr_bak":null,"divr_cycle":10,"divr_id":0,"divr_num":10,"divr_price":699.0,"divr_unit":null,"divr_update":null,"divr_user":"主入口区","project_id":0,"resource_id":0,"resource_image":"D:\\upload\\file\\exhibit\\20160712135923500101.png","resource_name":"法式西点","resource_unit":"份","type_no":null},
{"area_id":3,"divr_addate":null,"divr_bak":"全天\/彩排\/距离:1000KM","divr_cycle":1,"divr_id":0,"divr_num":0,"divr_price":1600.0,"divr_unit":null,"divr_update":null,"divr_user":"舞台区","project_id":0,"resource_id":0,"resource_image":"d:\/upload\/file\/program\/20160715182159200101.jpg","resource_name":"段宇","resource_unit":"人","type_no":null},
{"area_id":3,"divr_addate":null,"divr_bak":null,"divr_cycle":1,"divr_id":0,"divr_num":1,"divr_price":1.0,"divr_unit":null,"divr_update":null,"divr_user":"舞台区","project_id":0,"resource_id":0,"resource_image":"D:\\upload\\file\\sequipment\\201606291919531001.jpg","resource_name":"话筒(多色变色)","resource_unit":"1","type_no":null}]

需要的可以复制下去试试!!!

下面是代码,直接下载,就可以运行,看效果!!!
如有不同,请复制上方的html页面的内容进行替换js代码!

下面是参考网上大神的合并单元格的代码,为尊重他们的原创,再次贴出他们的链接:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章