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代碼!

下面是參考網上大神的合併單元格的代碼,爲尊重他們的原創,再次貼出他們的鏈接:


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