MecGrid 动态创建表格

MecGrid是一个很好表格工具,在flex中应用极其广泛,有时我们需要动态创建表格,就要构造其字符串,下边引用了一个我在项目中,使用到的函数,请看:

/**构建报表结构,主要是动态生成表格*/
private function  buildTable  ():void{
      var list:XML = new XML(<List></List>);
     var option:XML = new XML(<option></option>);
     var columns:XML = new XML(<columns></columns>);
     if (typeStr == "rbDayReport" || typeStr == "rbMonthReport") {
    //方式1,列数未知
    var colNum:int=payMethod.length; 
        option = new XML(<option cols={colNum+3} fixedrows="2" fixedcols="0" extendlastcol="T" merge_option="5" merge_option_fixedcolumn="1" merge_option_fixedrow="1" showspreadsheet="F" selectionmode="17" sortoption="0" treeview="T" treeColumn="0" textalignment="10" textalignment_fixed="10"/>);
        columns = new XML(<columns></columns>);
        columns.appendChild(new XML(<column  name="" datafield="spName" visible="T" editable="F" colindex="0" autowidth="T" width="110" datatype="5" textalign="5" textalign_fixed="5" formatstring="" merge="T" style="" enablefilter="T" header="测试名称;测试名称" />));
      columns.appendChild(new XML(<column  name="" datafield="serviceName" visible="T" editable="F" colindex="1" autowidth="T" width="110" datatype="5" textalign="5" textalign_fixed="5" formatstring="" merge="T" style="" enablefilter="T" header="测试类型;测试类型" />));
  for(var i:int=0;i<colNum;++i){
       var colHeader:String=payMethod[i].DATA;
       var colName:String="动态列;"+payMethod[i].LABEL;
      var column:XML = new XML();
      column=<column  name={colHeader} datafield={colHeader} visible="T" editable="F" colindex={i+2} autowidth="F" width="110" datatype="4" textalign="5" textalign_fixed="5" formatstring="#.##" merge="F" style="" enablefilter="T" header={colName}/>;
   columns.appendChild(column);
  }
  columns.appendChild(new XML(<column  name="" datafield="sum" visible="T" editable="F" colindex={colNum+2} autowidth="T" width="110" datatype="4" textalign="5" textalign_fixed="5" formatstring="#.##" merge="T" style="" enablefilter="T" header="小计;小计" />));
 } else {
   //方式二,列数已知
  option = new XML(<option cols="6" fixedrows="2" fixedcols="0" extendlastcol="T" merge_option="5" merge_option_fixedcolumn="1" merge_option_fixedrow="1" showspreadsheet="F" selectionmode="17" sortoption="0" treeview="T" treeColumn="0" textalignment="5" textalignment_fixed="5"/>);
  columns = new XML(<columns></columns>);
  columns.appendChild(new XML(<column  name="" datafield="TEST_DATE" visible="T" editable="F" colindex="0" autowidth="T" width="110" datatype="5" textalign="5" textalign_fixed="5" formatstring="" merge="T" style="" enablefilter="T" header="日期;日期" />));
  columns.appendChild(new XML(<column  name="" datafield="TEST_METHOD_NAME_1" visible="T" editable="F" colindex="1" autowidth="F" width="200" datatype="5" textalign="5" textalign_fixed="5" formatstring="#,###.##" merge="T" style="" enablefilter="T" header="测试方式;测试-1" />));
  columns.appendChild(new XML(<column  name="" datafield="TEST_METHOD_NAME_2" visible="T" editable="F" colindex="2" autowidth="F" width="200" datatype="5" textalign="5" textalign_fixed="5" formatstring="#,###.##" merge="T" style="" enablefilter="T" header="测试方式;测试-2" />));
  columns.appendChild(new XML(<column  name="" datafield="TEST_METHOD_NAME_3" visible="T" editable="F" colindex="3" autowidth="F" width="200" datatype="5" textalign="5" textalign_fixed="5" formatstring="#,###.##" merge="T" style="" enablefilter="T" header="测试方式;测试-3" />));
  columns.appendChild(new XML(<column  name="" datafield="TEST_METHOD_NAME_4" visible="T" editable="F" colindex="4" autowidth="F" width="200" datatype="5" textalign="5" textalign_fixed="5" formatstring="#,###.##" merge="T" style="" enablefilter="T" header="测试方式;测试-4" />));
  columns.appendChild(new XML(<column  name="" datafield="TEST_METHOD_NAME_5" visible="T" editable="F" colindex="5" autowidth="F" width="200" datatype="5" textalign="5" textalign_fixed="5" formatstring="#,###.##" merge="T" style="" enablefilter="T" header="测试方式;测试-5" />));
 }
 list.appendChild(option);
 list.appendChild(columns);
 //styles
 var styles:XML = new XML(<styles></styles>);
 styles.appendChild(new XML(<style name="spreadsheet" backcolor1="16317653" backcolor2="15066597" backalpha1="1" backalpha2="1" bordercolor="6710886" borderthickness="1" bt_top="1" bt_bottom="1" bt_left="1" bt_right="1" fontstyle="0" forecolor="0" fontsize="10"/>));
 styles.appendChild(new XML(<style name="subtotal" backcolor1="16317653" backcolor2="15066597" backalpha1="1" backalpha2="1" bordercolor="12566463" borderthickness="1" bt_top="1" bt_bottom="1" bt_left="1" bt_right="1" fontstyle="2" forecolor="0" fontsize="10"/>));
 styles.appendChild(new XML(<style name="normal" backcolor1="16777215" backcolor2="16777215" backalpha1="1" backalpha2="1" bordercolor="13290186" borderthickness="1" bt_top="1" bt_bottom="1" bt_left="1" bt_right="1" fontstyle="0" forecolor="0" fontsize="12"/>));
 styles.appendChild(new XML(<style name="fixedrow" backcolor1="3315442" backcolor2="16777215" backalpha1="1" backalpha2="1" bordercolor="13290186" borderthickness="1" bt_top="1" bt_bottom="1" bt_left="1" bt_right="1" fontstyle="1" forecolor="16777215" fontsize="12"/>));
 styles.appendChild(new XML(<style name="filter" backcolor1="16317653" backcolor2="15066597" backalpha1="1" backalpha2="1" bordercolor="12566463" borderthickness="1" bt_top="1" bt_bottom="1" bt_left="1" bt_right="1" fontstyle="2" forecolor="0" fontsize="10"/>));
 styles.appendChild(new XML(<style name="fixedcolumn" backcolor1="16777164" backcolor2="15066597" backalpha1="1" backalpha2="1" bordercolor="13290186" borderthickness="1" bt_top="1" bt_bottom="1" bt_left="1" bt_right="1" fontstyle="0" forecolor="0" fontsize="12"/>));
 list.appendChild(styles);
 //构造 ResourceXML
 mecGrid.ResourceXML=list;
}

你可能感兴趣的:(MecGrid 动态创建表格)