最近项目上使用了AdvancedDataGrid的合并表头,但是目前还没对应的导出方法,所以抽空写了一个,大家试试,如果有问题尽管提出来,谢谢!
/**
* @author:Yang Hao
* @desc :合并表头调用此方法
* dg :需要导出的AdvancedDataGrid的id
*/
public static function getMergerHeaders(dg:AdvancedDataGrid):ArrayCollection{
var mergerHeaders:ArrayCollection = new ArrayCollection();
var array:Array = new Array();
headerHandler(dg.groupedColumns, array, -1);
var maxRows:int = getMaxRows(array);
mergerHeaderHandler(dg.groupedColumns,0,0,0,0,maxRows,mergerHeaders);
return mergerHeaders;
}
/**
* @author:Yang Hao
* @desc :获取表头树的每个分支的层数(合并单元格的表头相当于一个树状结构)
*/
public static function headerHandler(groupedColumns:Array, array:Array, maxRows:int):void{
maxRows++;
/**
* groupedColumns.length=0:情况①:该 AdvancedDataGrid 不包含 AdvancedDataGridColumnGroup
* 情况②:该递归中 AdvancedDataGridColumnGroup 不含有 children
* groupedColumns.length>0:该递归中 AdvancedDataGridColumnGroup 含有 children
*/
if(groupedColumns != null && groupedColumns.length>0){
/** 遍历所有的group,获取每个分支的层数 */
for(var i: int=0; i<groupedColumns.length; i++){
/**
* 如果对象是 AdvancedDataGridColumnGroup:则说明该对象可以获取children,也表示不是最后一层
* 如果对象是 AdvancedDataGridColumn :表示此单元格已是最后一层,也就是叶子节点
*/
if(groupedColumns[i] as AdvancedDataGridColumnGroup){
var adg:AdvancedDataGridColumnGroup = groupedColumns[i] as AdvancedDataGridColumnGroup;
var children:Array = adg.children as Array;
headerHandler(children, array, maxRows);
}else if(groupedColumns[i] as AdvancedDataGridColumn){
array.push(maxRows);
}
}
}else{
array.push(maxRows);
}
}
/**
* @author:Yang Hao
* @desc :获取数组中的最大值,也就是获取合并表头中的最大层数
*/
public static function getMaxRows(array:Array):int{
var max:int = 0;
for(var i:int=0; i<array.length; i++){
if(max<array[i]){
max = array[i];
}
}
return max;
}
/**
* @author:Yang Hao
* @desc :获取导出的列
*/
public static function getExportCols(dg:AdvancedDataGrid):Array{
var cols:Array = new Array();
for(var i:Number=0; i<dg.columns.length; i++){
if(i!=0){
var column:AdvancedDataGridColumn = dg.columns[i] as AdvancedDataGridColumn;
cols.push(column.dataField);
}
}
return cols;
}
/**
* @author:Yang Hao
* @desc :根据每个 group中是否含有 children,来构造 MergerHeaderVO 对象集合
* startCols:开始行
* endCols :结束行
* startRows:开始列
* endRows :结束列
* maxRows :最大行
* results :结果集
**/
public static function mergerHeaderHandler(groupedColumns:Array, startCols:int, endCols:int, startRows:int, endRows:int, maxRows:int, results:ArrayCollection):void{
/**
* groupedColumns.length>0:该递归中 AdvancedDataGridColumnGroup 含有 children
*/
if(groupedColumns != null && groupedColumns.length>0){
/** 遍历所有的group,构造 /** 遍历所有的group,获取每个分支的层数 */
for(var i:int; i<groupedColumns.length; i++){
if(groupedColumns[i] as AdvancedDataGridColumnGroup){
var adg:AdvancedDataGridColumnGroup = groupedColumns[i] as AdvancedDataGridColumnGroup;
var children:Array = adg.children as Array;
/**
* 该递归中 AdvancedDataGridColumnGroup 不含有 children,则是最后一层,则该单元格的结束行数为最大行数
*/
if(children == null || children.length ==0){
var mergerHeader:MergerHeaderVO = new MergerHeaderVO();
mergerHeader.startCol=startCols;
mergerHeader.overCol=endCols;
mergerHeader.startRow=startRows;
mergerHeader.overRow=maxRows;
mergerHeader.value =adg.headerText;
results.addItem(mergerHeader);
} else {
/** 调用递归方法,则说明会进入下一层 ,则时候的开始行数和结束行数都应该变成当前的最后行数+1 */
mergerHeaderHandler(children, startCols, endCols,endRows+1, endRows+1, maxRows,results);
var mergerHeader:MergerHeaderVO = new MergerHeaderVO();
mergerHeader.startCol=startCols;
/**当单元格的children中的最后一个的结束列数也就是当前单元格的结束列数,同时也是下一个group的开始列数*/
mergerHeader.overCol=results.getItemAt(results.length-1).overCol;
endCols = results.getItemAt(results.length-1).overCol;
mergerHeader.startRow=startRows;
mergerHeader.overRow=endRows;
mergerHeader.value =adg.headerText;
results.addItem(mergerHeader);
}
} else if(groupedColumns[i] as AdvancedDataGridColumn){
var adc:AdvancedDataGridColumn= groupedColumns[i] as AdvancedDataGridColumn;
var mergerHeader:MergerHeaderVO = new MergerHeaderVO();
mergerHeader.startCol=startCols;
mergerHeader.overCol=endCols;
mergerHeader.startRow=startRows;
mergerHeader.overRow=maxRows;
mergerHeader.value =adc.headerText;
results.addItem(mergerHeader);
}
/** 每循环一次,代表group横向移动,则下个group的开始列和结束列应该为当前group的结束列+1 */
startCols=endCols+1;
endCols=endCols+1;
}
}
}
MergerHeaderVO.as
package com.winning.bi.model.domain
{
[RemoteClass(alias="com.winning.bi.vo.export.MergerHeaderVO")]
[Bindable]
public class MergerHeaderVO
{
public function MergerHeaderVO(){}
/*开始列*/
public var startRow:int;
/*结束列*/
public var overRow:int;
/*开始行*/
public var startCol:int;
/*结束行*/
public var overCol:int;
/*值*/
public var value:String;
}
}
package com.winning.bi.vo.export;
public class MergerHeaderVO {
private int startRow;
private int overRow;
private int startCol;
private int overCol;
private String value;
/**
*
* @param startRow 开始行
* @param overRow 结束行
* @param startCol开始列
* @param overCol结束列
* @param value值
*/
public MergerHeaderVO(int startRow, int overRow, int startCol, int overCol,
String value) {
this.startRow = startRow;
this.overRow = overRow;
this.startCol = startCol;
this.overCol = overCol;
this.value = value;
}
public MergerHeaderVO() {
super();
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public int getOverRow() {
return overRow;
}
public void setOverRow(int overRow) {
this.overRow = overRow;
}
public int getStartCol() {
return startCol;
}
public void setStartCol(int startCol) {
this.startCol = startCol;
}
public int getOverCol() {
return overCol;
}
public void setOverCol(int overCol) {
this.overCol = overCol;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
需要源码:请添加332589762@qq.com