构建gridPanel动态列

1.定义全局变量

var cmItems = [];

var cmConfig = {};

2.具体代码思想

   从后台请求"列"数据以及store数组,代码如下

    gridTemp.removeAll();

    cmItems.clear();

    

    var ItemArr = [];

    var item = {};

    

    UploadstoreColumns.load({ params: {fileName: document.getElementById("txtFile").value} });

    for (var a = 0; a < UploadstoreColumns.getRange().length; a++) 

    {

        

        if(a==6)

        {

         STARTDATE=UploadstoreColumns.getRange()[a].get("COLUMNSCODE").replace("PSIDATE","");

        }

        

        item = { name:UploadstoreColumns.getRange()[a].get("COLUMNSCODE")};

        ItemArr.push(item); 

        var headName=UploadstoreColumns.getRange()[a].get("COLUMNSCODE").replace("PSIDATE","");

        switch(headName)

        {

         case "VOLUME":

           headName="物理体积 m3";

           break;

         case "TOTALP":

           headName="合计 P";

           break;

         case "TOTALS":

           headName="合计 S";

           break;

         case "TOTALI":

           headName="合计 I";

           break;

         case "TOTAL":

           headName="总体积";

           break;  

        }

        if(headName=="FLAGI")

        {

        cmConfig = { header:headName, align: 'center',sortable:true,hidden:true,dataIndex:UploadstoreColumns.getRange()[a].get("COLUMNSCODE"), width: 100}

        }  

        else

        {

        

                    

        cmConfig = { header:headName, align: 'center',sortable:true, dataIndex:UploadstoreColumns.getRange()[a].get("COLUMNSCODE"), width: 100,renderer:function(value){

        

        if(value=='0')

        {

         return "";

        }else

        {

        return value

        }

        

        }}

        

        }

        cmItems.push(cmConfig);

        

        

        if(headName!="FLAGI")

        {

        switch(headName)

        {

          case "DEPTCD":

          case "WAREHOUSECD":

          case "ITEMNO":

          case "ITEMNAME":

          case "物理体积 m3":

          case "合计 P":

          case "合计 S":

          case "合计 I":

          case "总体积":

          break;

          default:

            COLNAME+= strSplit+headName;

            break;   

        }                            

        

        }            

    }

    

///////////////////////////获得数据源

var Mystore = new Ext.data.Store({

    proxy: new Ext.data.HttpProxy({ url: 'WsECOWHS01.asmx/selectUploadData', method: "post", async: false}),

    reader: new Ext.data.XmlReader(

    {

        totalProperty: 'totalRecords',

        record: 'row'

    },ItemArr

    )

});



var Mycm = new Ext.grid.ColumnModel(cmItems);

 Mystore.load({ params: {fileName: document.getElementById("txtFile").value} });

 

 

var btnSave= new Ext.Button({

   text:'保存',

   iconCls: 'icon_save',

   handler:function()

   {

   



       var selFuns = [];

    Mystore.each(function(r){

      selFuns.push(r.data);

    });

    var pam = {

        list:selFuns,

        COLNAME:COLNAME,

        DEPTCD:MLCBMXX.getValue(),

        WAREHOUSECD:MLCGYS.getValue()  

    } 

    var result = AjaxRequestJson('WsECOWHS01.asmx/Save', pam);

    if (result.rtnValue.isError) {

        Ext.Msg.alert(" 提示消息", result.rtnValue.errorMessage);

    } else {

        Ext.Msg.alert(" 提示消息", "操作成功!");

    }                         

    

               

   



   }

}); 

var btnUpStep= new Ext.Button({

   text:'上一步',

   iconCls: 'icon_return',

   handler:function()

   {

    CardPanel.getLayout().setActiveItem(formPanel);

   }

});            

var MygridArray = new Ext.grid.GridPanel({

    store: Mystore,

    cm: Mycm,

    autoScroll: true,

    border: false,

    columnLines:true,

    viewConfig: {

    //forceFit:true,

    columnsText: '显示的列',

    scrollOffset: 20,

    sortAscText: '升序',

    sortDescText: '降序',

    getRowClass:function(record,rowIndex,rowParams,store)

    {             

        if(record.data.FLAGI == "Y")

        {

        return 'x-grid-back-Myellow';

        }

        

    }                 

    },

    bbar:[btnSave]//,btnUpStep

});  

gridTemp.insert(0, MygridArray);



MygridArray.doLayout();

gridTemp.doLayout();                

CardPanel.getLayout().setActiveItem(gridTemp);

 

你可能感兴趣的:(gridPanel)