//对datagrid的参数进行扩展,增加了以下3个参数 $.extend($.fn.datagrid.defaults, { groupHeight: 25,//分组区域出现时的高度 expanderWidth: 30,//分组左侧的+-号的宽度 groupStyler: function(value,rows){return ''}//组的css样式 }); //定义全新的分组样式的grid视图,继承默认的视图 var groupview = $.extend({}, $.fn.datagrid.defaults.view, { //会执行2次,第一次是冻结列,第二次是指标列(非冻结) render: function(target, container, frozen){ var state = $.data(target, 'datagrid'); var table = []; this.currentIndex = 0; var opts = state.options; var groupFields = opts.groupField;//分组字段 if(typeof groupFields == 'string'){ groupFields = groupFields.split(','); } //每个汇总字段的计数 this.fieldCountMap = {}; for(var i=0;i'); for(var i=0; i0){ lastOrderIndex = groupOrder[i-1]; } if(lastOrderIndex.length == 0 || lastOrderIndex[0]!==groupOrder[i][0]){ //table.push(''); } table.push(this.renderGroup.call(this, target, groupOrder[i],nextOrderIndex, frozen)); if(nextOrderIndex.length==0 || nextOrderIndex[0]!==groupOrder[i][0]){ //table.push('
'); } } table.push(''); if(!frozen){ state.data.rows = this.newRows; } $(container).html(table.join('')); }, renderGroup: function(target, groupIndexArray,nextGroupIndexArray, frozen){ var state = $.data(target, 'datagrid'); var opts = state.options; var fields = $(target).datagrid('getColumnFields', frozen); var frozenFields = $(target).datagrid('getColumnFields', true); var hasFrozen = opts.frozenColumns && opts.frozenColumns.length; var groups = this.groups; if (frozen){ if (!(opts.rownumbers || hasFrozen)){ return ''; } } var groupFields = opts.groupField;//分组字段 if(typeof groupFields == 'string'){ groupFields = groupFields.split(','); } var table = []; var nestedObj = groups; var groupname = ''; var groupPos = -1; //获取当前小组的数据 for(var i=0;i=0;m--){ //倒数第二个开始 var isAddExtraRow = false; var curGroup = groupIndexArray[m]; if(nextGroupIndexArray.length == 0){ isAddExtraRow = true; }else{ var nextGroup = nextGroupIndexArray[m]; if(curGroup == nextGroup){ break; }else{ isAddExtraRow = true; } } //如果需要添加合计页 if(isAddExtraRow == true){ var jo = opts.sumFormatter.call(target, curGroup, nestedObj); var rowData = buildFrozenSumRow(nestedObj,m,curGroup,jo); var htmltr = renderRowExt(this,rowData,true); table.push(htmltr); this.currentIndex++; if (!(frozen && hasFrozen)){ this.newRows.push(rowData); } } } return table.join(''); /** * [buildFrozenSumRow 构建冻结区块的合计信息] * @param {[type]} rows [description] * @param {[type]} groupPos [description] * @param {[type]} title [description] * @return {[type]} [description] */ function buildFrozenSumRow(rows,groupPos,title,jo){ var keyField = ''; if(frozenFields.length>groupPos+1){ keyField = frozenFields[groupPos+1]; } if(!jo){ jo = {}; } if(rows.length>0){ jo = $.extend(jo,rows[0]); } if(keyField){ jo[keyField] = title+' 小计'; } if(frozenFields.length>groupPos+2){ for(var i=groupPos+2;i'); innerTable.push(that.renderRow.call(this, target, fields, frozen, that.currentIndex, rowData)); innerTable.push(''); return innerTable.join(''); } /* function getSumTitle(groupIndexArray,thisPath){ var title = ''; var index = -1; for(var i=0;i=0){ title = groupIndexArray[index]; } return title + '小计'; }*/ }, /* bindEvents: function(target){ var state = $.data(target, 'datagrid'); var dc = state.dc; var body = dc.body1.add(dc.body2); var clickHandler = ($.data(body[0],'events')||$._data(body[0],'events')).click[0].handler; body.unbind('click').bind('click', function(e){ var tt = $(e.target); var expander = tt.closest('span.datagrid-row-expander'); if (expander.length){ var gindex = expander.closest('div.datagrid-group').attr('group-index'); if (expander.hasClass('datagrid-row-collapse')){ $(target).datagrid('collapseGroup', gindex); } else { $(target).datagrid('expandGroup', gindex); } } else { clickHandler(e); } e.stopPropagation(); }); },*/ //渲染前的事件 onBeforeRender: function(target, rows){ var state = $.data(target, 'datagrid'); var opts = state.options; var groupFields = opts.groupField;//分组字段 if(typeof groupFields == 'string'){ groupFields = groupFields.split(','); } var groups = {};//分组信息 var groupOrder = [];//分组顺序 initCss(); groupRows(rows,groupFields); //state.data.rows = newRows; this.groups = groups; this.groupOrder = groupOrder; this.newRows = [];//新数据 /* var that = this; setTimeout(function(){ that.bindEvents(target); },0);*/ function groupRows(rows,groupFields){ var groupOrderStr = []; for(var i=0; i' + '.row-sum{/*background-color:#34d7d9 !important;*/ font-weight:700;}'+ '.datagrid-td-rownumber{font-weight:400;}'+ '' ); } } }, onAfterRender: function(target){ $.fn.datagrid.defaults.view.onAfterRender.call(this, target); //合并单元格; //$(target).datagrid('autoMergeCells', ['company','brand']); } });