分享

扩展GridPanel

 WindySky 2011-08-07
  1. /**  
  2.  
  3.  * 封装的grid  
  4.  
  5.  * 功能:分页后仍保持选中状态  
  6.  
  7.  * 约定:root为list, totalProperty为total, grid的第一列必须为id  
  8.  
  9.  */  
  10.   
  11. Ext.namespace('Ext.ux.grid');   
  12.   
  13.   
  14.   
  15. Ext.ux.grid.MyGrid = Ext.extend(Ext.grid.GridPanel,{   
  16.   
  17.        
  18.   
  19.     /*  
  20.  
  21.      * true to keep the records selected when you paging  
  22.  
  23.      * @default(false)  
  24.  
  25.      * @type: boolean  
  26.  
  27.      */  
  28.   
  29.     keepSelectedOnPaging: false,   
  30.   
  31.        
  32.   
  33.     /*  
  34.  
  35.      * the array to store the record id  
  36.  
  37.      * @type: array  
  38.  
  39.      */  
  40.   
  41.     recordIds:new Array(),   
  42.   
  43.        
  44.   
  45.     /*  
  46.  
  47.      * set your id Column Name  
  48.  
  49.      * @default : this.CM_JR_Record[0].dataIndex  
  50.  
  51.      */  
  52.   
  53.     idColName:'',   
  54.   
  55.        
  56.   
  57.     /*  
  58.  
  59.      * set this.store.load url;  
  60.  
  61.      * @type: string  
  62.  
  63.      */  
  64.   
  65.     url: '',   
  66.   
  67.        
  68.   
  69.     /*  
  70.  
  71.      * show the rowNumber or not  
  72.  
  73.      * @type: boolean  
  74.  
  75.      * @default: true  
  76.  
  77.      */  
  78.   
  79.     rowNumber : true,   
  80.   
  81.        
  82.   
  83.     /*  
  84.  
  85.      * set the grid sm,if checkBoxSelection=true,sm=CheckBoxSelectionModel  
  86.  
  87.      * else sm=RowSelectionModel,default to true;  
  88.  
  89.      * @type: boolean  
  90.  
  91.      */  
  92.   
  93.     checkBox: true,   
  94.   
  95.        
  96.   
  97.     /*  
  98.  
  99.      * set the grid cm array;  
  100.  
  101.      * set the JsonReader record;  
  102.  
  103.      *   
  104.  
  105.      * format: [{name:'',header:'',visiable:'',...another cm options},{}],  
  106.  
  107.      * @name=dataIndex  
  108.  
  109.      * @visiable: set this record to the cm(grid header) default(true)  
  110.  
  111.      * @type: array (records)  
  112.  
  113.      */    
  114.   
  115.     CM_JR_Record: null,   
  116.   
  117.        
  118.   
  119.     /*  
  120.  
  121.      * true to add a bottom paging bar  
  122.  
  123.      * @defalut: true  
  124.  
  125.      * @type: boolean  
  126.  
  127.      */  
  128.   
  129.     pagingBar: true,   
  130.   
  131.        
  132.   
  133.     /*  
  134.  
  135.      * config paging bar if pagingBar set true  
  136.  
  137.      * @type: object  
  138.  
  139.      * @default: {pageSize: 20,store: this.store,displayInfo: true,  
  140.  
  141.      *            displayMsg: '当前记录数: {0} - {1} 总记录数: {2}',  
  142.  
  143.      *            emptyMsg: '<b>0</b> 条记录'}  
  144.  
  145.      */  
  146.   
  147.     pagingConfig:{   
  148.   
  149.            pageSize: 15,   
  150.   
  151.        store: this.store,   
  152.   
  153.        displayInfo: true,   
  154.   
  155.        displayMsg: "显示第 {0} 条到 {1} 条记录,一共 {2} 条",   
  156.   
  157.        emptyMsg: '<b>0</b> 条记录',   
  158.   
  159.     },   
  160.   
  161.        
  162.   
  163.     viewConfig:{   
  164.   
  165.         forceFit: true  
  166.   
  167.     },   
  168.   
  169.        
  170.   
  171.     //private   
  172.   
  173.     initComponent: function(){   
  174.   
  175.         if(this.CM_JR_Record){   
  176.   
  177.             this.init_SM_CM_DS();   
  178.   
  179.         }   
  180.   
  181.         if(this.pagingBar){   
  182.   
  183.             this.init_PagingBar();   
  184.   
  185.         }   
  186.   
  187.         if(this.keepSelectedOnPaging){   
  188.   
  189.             this.init_OnPaging();   
  190.   
  191.         }   
  192.   
  193.         Ext.ux.grid.MyGrid.superclass.initComponent.call(this);   
  194.   
  195.     },   
  196.   
  197.        
  198.   
  199.     /*  
  200.  
  201.      * init the grid use the config options  
  202.  
  203.      * @return: void  
  204.  
  205.      * @params: none  
  206.  
  207.      */  
  208.   
  209.     init_SM_CM_DS: function(){   
  210.   
  211.         var gCm = new Array();   
  212.   
  213.         var gRecord = new Array();   
  214.   
  215.            
  216.   
  217.         if(this.rowNumber){   
  218.   
  219.             gCm[gCm.length]=new Ext.grid.RowNumberer();   
  220.   
  221.         }   
  222.   
  223.         if(this.checkBox){   
  224.   
  225.             var sm = new Ext.grid.CheckboxSelectionModel();   
  226.   
  227.             gCm[gCm.length] = sm;   
  228.   
  229.             this.selModel = sm;   
  230.   
  231.         }   
  232.   
  233.            
  234.   
  235.         for(var i=0;i<this.CM_JR_Record.length;i++)   
  236.   
  237.         {   
  238.   
  239.             var g = this.CM_JR_Record[i];   
  240.   
  241.             if(g.visiable || g.visiable=='undefined' || g.visiable==null){   
  242.   
  243.                 gCm[gCm.length] = g;   
  244.   
  245.             }   
  246.   
  247.                
  248.   
  249.             gRecord[gRecord.length]={   
  250.   
  251.                 name: g.dataIndex,   
  252.   
  253.                 type: g.type || 'string'    
  254.   
  255.             }   
  256.   
  257.         }   
  258.   
  259.        
  260.   
  261.         //create grid columnModel   
  262.   
  263.         this.cm = new Ext.grid.ColumnModel(gCm);   
  264.   
  265.         this.cm.defaultSortable = true;   
  266.   
  267.            
  268.   
  269.         //create a jsonStore   
  270.   
  271.         this.store = new Ext.data.Store({   
  272.   
  273.             proxy: new Ext.data.HttpProxy({   
  274.   
  275.                 url: this.url,   
  276.   
  277.                 method: 'post'  
  278.   
  279.             }),   
  280.   
  281.             reader:new Ext.data.JsonReader({   
  282.   
  283.                 totalProperty: 'total',   
  284.   
  285.                 root: 'list'  
  286.   
  287.             },   
  288.   
  289.             Ext.data.Record.create(gRecord)   
  290.   
  291.             )   
  292.   
  293.            
  294.   
  295.         });   
  296.   
  297.            
  298.   
  299.            
  300.   
  301.         this.pagingConfig.store = this.store;   
  302.   
  303.            
  304.   
  305.         if(this.pagingBar){   
  306.   
  307.             this.store.load({params:{start:0,limit:this.pagingConfig.pageSize}});   
  308.   
  309.         }else{   
  310.   
  311.             this.store.load();   
  312.   
  313.         }   
  314.   
  315.            
  316.   
  317.     },   
  318.   
  319.        
  320.   
  321.     /*  
  322.  
  323.      * 创建并初始化paging bar  
  324.  
  325.      */  
  326.   
  327.     init_PagingBar: function(){   
  328.   
  329.         var bbar = new Ext.PagingToolbar(this.pagingConfig);   
  330.   
  331.         this.bbar = bbar;   
  332.   
  333.     },   
  334.   
  335.        
  336.   
  337.     init_OnPaging: function(){   
  338.   
  339.            
  340.   
  341.         this.idColName = this.CM_JR_Record[0].dataIndex ;//默认第一列为ID列   
  342.   
  343.            
  344.   
  345.         this.selModel.on('rowdeselect',function(selMdl,rowIndex,rec ){   
  346.   
  347.            
  348.   
  349.                
  350.   
  351.                 for(var i=0;i<this.recordIds.length;i++)   
  352.   
  353.                 {   
  354.   
  355.                     if(rec.data[this.idColName] == this.recordIds[i]){   
  356.   
  357.                         this.recordIds.splice(i,1);                        
  358.   
  359.                         return;   
  360.   
  361.                     }   
  362.   
  363.                 }   
  364.   
  365.                
  366.   
  367.                 
  368.   
  369.        },this);   
  370.   
  371.           
  372.   
  373.        this.selModel.on('rowselect',function(selMdl,rowIndex,rec){   
  374.   
  375.             if(this.hasElement(this.recordIds)){   
  376.   
  377.                 for(var i=0;i<this.recordIds.length;i++){   
  378.   
  379.                     if(rec.data[this.idColName] == this.recordIds[i]){   
  380.   
  381.                         return;   
  382.   
  383.                     }   
  384.   
  385.                 }   
  386.   
  387.             }      
  388.   
  389.                
  390.   
  391.             this.recordIds.unshift(rec.data[this.idColName]);   
  392.   
  393.                
  394.   
  395.        },this);   
  396.   
  397.           
  398.   
  399.        this.store.on('load',function(st,recs){   
  400.   
  401.             if(this.hasElement(this.recordIds)){   
  402.   
  403.                 st.each(function(rec){   
  404.   
  405.                     Ext.each(this.recordIds,function(item,index,allItems){   
  406.   
  407.                         if(rec.data[this.idColName] == item){   
  408.   
  409.                             this.selModel.selectRecords([rec],true);                           
  410.   
  411.                             return false;   
  412.   
  413.                         }   
  414.   
  415.                     },this);   
  416.   
  417.                 },this);       
  418.   
  419.             }      
  420.   
  421.        },this);   
  422.   
  423.                 
  424.   
  425.     },   
  426.   
  427.        
  428.   
  429.     hasElement : function(recIds){   
  430.   
  431.         if(recIds.length > 0)   
  432.   
  433.             return true;   
  434.   
  435.         else  
  436.   
  437.             return false;   
  438.   
  439.     }   
  440.   
  441.        
  442.   
  443. }   
  444.   
  445. )<SPAN style="FONT-FAMILY: Arial, Verdana, Sans-Serif">   
  446.   
  447. //**************************这是个demo</SPAN>*****************   
  448. <SPAN style="FONT-FAMILY: Arial, Verdana, Sans-Serif"><PRE class=jscript name="code">var CM_JR_Record = [    
  449.   
  450.                {    
  451.   
  452.                   dataIndex:"id",    
  453.   
  454.                   visiable: false//不显示,反之为显示   
  455.   
  456.                },{    
  457.   
  458.                   dataIndex:"accid",   
  459.   
  460.                   header:"发布人ID",   
  461.   
  462.                   visiable: true  
  463.   
  464.                },{   
  465.   
  466.                     header: '标题',   
  467.   
  468.                     width: 80,   
  469.   
  470.                     dataIndex: 'bt',   
  471.   
  472.                     visiable: true  
  473.   
  474.                 }, {   
  475.   
  476.                     header: '发布时间',   
  477.   
  478.                     width: 80,   
  479.   
  480.                     dataIndex: 'fbsj',   
  481.   
  482.                     visiable: true  
  483.   
  484.                 }, {   
  485.   
  486.                     header: '发布人员',   
  487.   
  488.                     width: 80,   
  489.   
  490.                     dataIndex: 'fbry',   
  491.   
  492.                     visiable: true  
  493.   
  494.                 }, {   
  495.   
  496.                     header: '审核',   
  497.   
  498.                     width: 80,   
  499.   
  500.                     dataIndex: 'sh',   
  501.   
  502.                     visiable: true  
  503.   
  504.                 }, {   
  505.   
  506.                     header: '审核人员',   
  507.   
  508.                     width: 80,   
  509.   
  510.                     dataIndex: 'shry',   
  511.   
  512.                   visiable: true  
  513.   
  514.                 }, {   
  515.   
  516.                     header: '审核时间',   
  517.   
  518.                     width: 80,   
  519.   
  520.                     dataIndex: 'shsj',   
  521.   
  522.                     visiable: true  
  523.   
  524.                 }, {   
  525.   
  526.                     header: '点击数',   
  527.   
  528.                     width: 80,   
  529.   
  530.                     dataIndex: 'snum',   
  531.   
  532.                     visiable: true  
  533.   
  534.                 }];   
  535.   
  536.        
  537.   
  538.      var myGrid = new Ext.ux.grid.MyGrid({   
  539.   
  540.               url : '/ecommerce/findAllBulletin.action',          // the store load url (required)   
  541.   
  542.               CM_JR_Record: CM_JR_Record, //.....(required)   
  543.   
  544.               rowNumber:true,             //true to add a Ext.grid.RowNumberer,defalut(true)   
  545.   
  546.               checkBox:true,              //true to add a Ext.grid.CheckBoxSelectionModel,default(true)   
  547.   
  548.               pagingBar:true,             //true to add a Ext.PagingToolBar,default(true)   
  549.   
  550. //            pagingConfig:objcet,        //config pagingToolBar if pagingBar is true   
  551.   
  552.               keepSelectedOnPaging: true//true to FireEvent when you paging to keep the state of record selected   
  553.   
  554.               recordIds : new Array() ,   // store seleced ids when keepSelectedOnPaging is true   
  555.   
  556.               idColName :'id',       //the id column name   
  557.   
  558.               width : 700,    
  559.   
  560.               height: 600,    
  561.   
  562.               renderTo: 'editor-grid',   
  563.   
  564.               tbar: [{   
  565.   
  566.                   id : 'Add',   
  567.   
  568.                   text : ' 新建  ',   
  569.   
  570.                   tooltip : '新建一个表单',   
  571.   
  572.                   iconCls : 'add',   
  573.   
  574.                   pageSize: 15,   
  575.   
  576.                   handler : function(){   
  577.   
  578.                          ptb_bt1();   
  579.   
  580.                   }   
  581.   
  582.                 },{   
  583.   
  584.                     text: '删除所选',   
  585.   
  586.                     iconCls:'remove',   
  587.   
  588.                     tooltip : '删除所选数据',   
  589.   
  590.                     handler : function(){   
  591.   
  592.                                                //myGrid.recordIds是一个数组,里面存放选中的checkboxid   
  593.   
  594.                         if(myGrid.recordIds.length == 0){   
  595.   
  596.                             Ext.MessageBox.alert('提示','请选择一条记录!');   
  597.   
  598.                         }else{   
  599.   
  600.                             // 弹出对话框警告   
  601.   
  602.                             Ext.MessageBox.confirm('确认删除',    
  603.   
  604.                                 '你真的要删除所选用户吗?',    
  605.   
  606.                                 function(btn){   
  607.   
  608.                                  if(btn == 'yes') {// 选中了是按钮   
  609.   
  610.                                      // 调用 DWR, 执行结果成功时方删除所有数据   
  611.   
  612.                                      bulletinService.delBulletin(myGrid.recordIds.toString(), function() {   
  613.   
  614.                                             // 更新界面, 来真正删除数据   
  615.   
  616.                                             Ext.Msg.alert("成功","用户数据删除成功!");   
  617.   
  618.                                             myGrid.recordIds = new Array();   
  619.   
  620.                                             myGrid.store.load({params:{start:0,limit:15}});   
  621.   
  622.                                      });   
  623.   
  624.                                  }   
  625.   
  626.                                 }   
  627.   
  628.                             );   
  629.   
  630.                         }     
  631.   
  632.                 }   
  633.   
  634.         }]   
  635.   
  636.         });   
  637.   
  638.        
  639.   
  640.     myGrid.render();</PRE>   
  641. 下载地址如下<A>http://download.csdn.net/source/521174</A></SPAN>  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多