`
牧羊人
  • 浏览: 215464 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

一个扩展的 GridPanel

阅读更多
分页后可保存checkbox的选择状态。
/**

 * 封装的grid

 * 功能:分页后仍保持选中状态

 * 约定:root为list, totalProperty为total, grid的第一列必须为id

 */

Ext.namespace('Ext.ux.grid');



Ext.ux.grid.MyGrid = Ext.extend(Ext.grid.GridPanel,{

	

	/*

	 * true to keep the records selected when you paging

	 * @default(false)

	 * @type: boolean

	 */

	keepSelectedOnPaging: false,

	

	/*

	 * the array to store the record id

	 * @type: array

	 */

	recordIds:new Array(),

	

	/*

	 * set your id Column Name

	 * @default : this.CM_JR_Record[0].dataIndex

	 */

	idColName:'',

	

	/*

	 * set this.store.load url;

	 * @type: string

	 */

	url: '',

	

	/*

	 * show the rowNumber or not

	 * @type: boolean

	 * @default: true

	 */

	rowNumber : true,

	

	/*

	 * set the grid sm,if checkBoxSelection=true,sm=CheckBoxSelectionModel

	 * else sm=RowSelectionModel,default to true;

	 * @type: boolean

	 */

	checkBox: true,

	

	/*

	 * set the grid cm array;

	 * set the JsonReader record;

	 * 

	 * format: [{name:'',header:'',visiable:'',...another cm options},{}],

	 * @name=dataIndex

	 * @visiable: set this record to the cm(grid header) default(true)

	 * @type: array (records)

	 */	

	CM_JR_Record: null,

	

	/*

	 * true to add a bottom paging bar

	 * @defalut: true

	 * @type: boolean

	 */

	pagingBar: true,

	

	/*

	 * config paging bar if pagingBar set true

	 * @type: object

	 * @default: {pageSize: 20,store: this.store,displayInfo: true,

	 *            displayMsg: '当前记录数: {0} - {1} 总记录数: {2}',

	 *            emptyMsg: '<b>0</b> 条记录'}

	 */

	pagingConfig:{

           pageSize: 15,

	   store: this.store,

	   displayInfo: true,

	   displayMsg: "显示第 {0} 条到 {1} 条记录,一共 {2} 条",

	   emptyMsg: '<b>0</b> 条记录',

	},

	

	viewConfig:{

		forceFit: true

	},

	

	//private

	initComponent: function(){

		if(this.CM_JR_Record){

			this.init_SM_CM_DS();

		}

		if(this.pagingBar){

			this.init_PagingBar();

		}

		if(this.keepSelectedOnPaging){

			this.init_OnPaging();

		}

		Ext.ux.grid.MyGrid.superclass.initComponent.call(this);

	},

	

	/*

	 * init the grid use the config options

	 * @return: void

	 * @params: none

	 */

	init_SM_CM_DS: function(){

		var gCm = new Array();

		var gRecord = new Array();

		

		if(this.rowNumber){

			gCm[gCm.length]=new Ext.grid.RowNumberer();

		}

		if(this.checkBox){

			var sm = new Ext.grid.CheckboxSelectionModel();

			gCm[gCm.length] = sm;

			this.selModel = sm;

		}

		

		for(var i=0;i<this.CM_JR_Record.length;i++)

		{

			var g = this.CM_JR_Record[i];

			if(g.visiable || g.visiable=='undefined' || g.visiable==null){

				gCm[gCm.length] = g;

			}

			

			gRecord[gRecord.length]={

				name: g.dataIndex,

				type: g.type || 'string' 

			}

		}

	

		//create grid columnModel

		this.cm = new Ext.grid.ColumnModel(gCm);

		this.cm.defaultSortable = true;

		

		//create a jsonStore

		this.store = new Ext.data.Store({

			proxy: new Ext.data.HttpProxy({

				url: this.url,

				method: 'post'

			}),

			reader:new Ext.data.JsonReader({

				totalProperty: 'total',

				root: 'list'

			},

			Ext.data.Record.create(gRecord)

			)

		

		});

		

		

		this.pagingConfig.store = this.store;

		

		if(this.pagingBar){

			this.store.load({params:{start:0,limit:this.pagingConfig.pageSize}});

		}else{

			this.store.load();

		}

		

	},

	

	/*

	 * 创建并初始化paging bar

	 */

	init_PagingBar: function(){

		var bbar = new Ext.PagingToolbar(this.pagingConfig);

		this.bbar = bbar;

	},

	

	init_OnPaging: function(){

		

		this.idColName = this.CM_JR_Record[0].dataIndex ;//默认第一列为ID列

		

		this.selModel.on('rowdeselect',function(selMdl,rowIndex,rec ){

		

			

				for(var i=0;i<this.recordIds.length;i++)

	      	 	{

	      	 		if(rec.data[this.idColName] == this.recordIds[i]){

		      	 		this.recordIds.splice(i,1);		      	 		

		      	 		return;

	      	 		}

	      	 	}

			

	      	 

       },this);

       

       this.selModel.on('rowselect',function(selMdl,rowIndex,rec){

       		if(this.hasElement(this.recordIds)){

       			for(var i=0;i<this.recordIds.length;i++){

        			if(rec.data[this.idColName] == this.recordIds[i]){

        				return;

        			}

        		}

       		}	

       		

      		this.recordIds.unshift(rec.data[this.idColName]);

      		

       },this);

       

       this.store.on('load',function(st,recs){

       		if(this.hasElement(this.recordIds)){

      	    	st.each(function(rec){

      	    		Ext.each(this.recordIds,function(item,index,allItems){

      	    			if(rec.data[this.idColName] == item){

      	    				this.selModel.selectRecords([rec],true);     	    			

      	    				return false;

      	    			}

      	    		},this);

      	    	},this);	

       		}	

       },this);

             

	},

	

	hasElement : function(recIds){

		if(recIds.length > 0)

			return true;

		else

		    return false;

	}

	

}

)<SPAN style="FONT-FAMILY: Arial,Verdana,Sans-Serif">

//**************************这是个demo</SPAN>*****************
<SPAN style="FONT-FAMILY: Arial,Verdana,Sans-Serif"><PRE class=jscript name="code">var CM_JR_Record = [ 

               { 

                  dataIndex:"id", 

                  visiable: false//不显示,反之为显示

               },{ 

                  dataIndex:"accid",

                  header:"发布人ID",

                  visiable: true

               },{

			        header: '标题',

			        width: 80,

			        dataIndex: 'bt',

                    visiable: true

			    }, {

			        header: '发布时间',

			        width: 80,

			        dataIndex: 'fbsj',

                    visiable: true

			    }, {

			        header: '发布人员',

			        width: 80,

			        dataIndex: 'fbry',

                    visiable: true

			    }, {

			        header: '审核',

			        width: 80,

			        dataIndex: 'sh',

                    visiable: true

			    }, {

			        header: '审核人员',

			        width: 80,

			        dataIndex: 'shry',

                  visiable: true

			    }, {

			        header: '审核时间',

			        width: 80,

			        dataIndex: 'shsj',

                    visiable: true

			    }, {

			        header: '点击数',

			        width: 80,

			        dataIndex: 'snum',

                    visiable: true

			    }];

	

	 var myGrid = new Ext.ux.grid.MyGrid({

		      url : '/ecommerce/findAllBulletin.action',          // the store load url (required)

		      CM_JR_Record: CM_JR_Record, //.....(required)

		      rowNumber:true,             //true to add a Ext.grid.RowNumberer,defalut(true)

		      checkBox:true,              //true to add a Ext.grid.CheckBoxSelectionModel,default(true)

		      pagingBar:true,             //true to add a Ext.PagingToolBar,default(true)

//		      pagingConfig:objcet,        //config pagingToolBar if pagingBar is true

		      keepSelectedOnPaging: true, //true to FireEvent when you paging to keep the state of record selected

		      recordIds : new Array() ,   // store seleced ids when keepSelectedOnPaging is true

		      idColName :'id',       //the id column name

		      width : 700, 

		      height: 600, 

		      renderTo: 'editor-grid',

			  tbar: [{

		          id : 'Add',

		          text : ' 新建  ',

		          tooltip : '新建一个表单',

		          iconCls : 'add',

				  pageSize: 15,

		          handler : function(){

		                 ptb_bt1();

		          }

		        },{

		            text: '删除所选',

					iconCls:'remove',

					tooltip : '删除所选数据',

		            handler : function(){

                                               //myGrid.recordIds是一个数组,里面存放选中的checkboxid

						if(myGrid.recordIds.length == 0){

		               		Ext.MessageBox.alert('提示','请选择一条记录!');

		            	}else{

							// 弹出对话框警告

							Ext.MessageBox.confirm('确认删除', 

				    	    	'你真的要删除所选用户吗?', 

				    	    	function(btn){

					    	     if(btn == 'yes') {// 选中了是按钮

					    	     	 // 调用 DWR, 执行结果成功时方删除所有数据

						    	     bulletinService.delBulletin(myGrid.recordIds.toString(), function() {

						    	     		// 更新界面, 来真正删除数据

									 		Ext.Msg.alert("成功","用户数据删除成功!");

											myGrid.recordIds = new Array();

											myGrid.store.load({params:{start:0,limit:15}});

									 });

								 }

								}

							);

						}  

	            }

        }]

		});

	

	myGrid.render();</PRE>
下载地址如下<A>http://download.csdn.net/source/521174</A></SPAN>
分享到:
评论

相关推荐

    扩展GridPanel

    扩展的GridPanel,让其分页后保持选择状态

    ExtJs GridPanel 操作

    在给定的`RowExpander.js`文件中,我们可以看到这是一个扩展GridPanel的功能,用于在每行旁边添加一个可展开的箭头,展示更多详细信息。RowExpander通常包含一个内嵌的Panel来展示额外内容。以下是如何使用...

    Gridpanel多表头的扩展

    例如,我们可以创建一个表头来表示部门,然后在下一级表头中列出部门内的员工,这样可以清晰地看出每个部门的人员分布。 描述中提到,这个扩展可能不适用于EXTJS 3及以上版本,这可能是因为EXTJS的后续版本进行了...

    扩展GridPanel,附带分页选中状态,实现快速构建一个功能齐全的Grid

    ExtJS是一个强大的JavaScript库,用于构建富客户端应用程序,而GridPanel是其核心组件之一,用于展示和操作表格数据。通过扩展GridPanel,我们可以定制其行为,使其更适合特定需求。 首先,我们关注的是`cgrid.js`...

    Ext GridPanel 中实现加链接操作

    Ext GridPanel 是该框架中的一个重要组件,常用于展示表格数据。本文将详细介绍如何在 Ext GridPanel 中实现加链接操作,包括基本原理、代码实现及注意事项。 #### 一、Ext GridPanel 基础 在了解如何添加链接之前...

    Extjs中的GridPanel

    总结起来,ExtJS 中的 GridPanel 是一个功能强大且灵活的数据展示组件,它为Web应用提供了丰富的数据管理和用户交互能力。通过熟练掌握 GridPanel 的使用,开发者能够创建出功能丰富、用户体验优秀的数据驱动应用。

    ExtJS 表格面板GridPanel完整例子

    ExtJS表格面板(GridPanel)是Sencha Ext JS框架中的一个核心组件,它用于展示大量结构化数据。在本文中,我们将深入探讨如何创建并使用一个完整的ExtJS GridPanel实例,以及与其相关的源码和工具。 首先,让我们...

    tapestry4.02中封装ext的GridPanel组件

    2. **定义Tapestry组件**:创建一个Tapestry组件(例如,名为`GridPanel`),这个组件负责与ExtJS的GridPanel交互。在`GridPanel.java`中,会定义Tapestry组件的Java类,包括属性、方法和事件处理逻辑。 3. **配置...

    Ext.grid.GridPanel属性祥解

    - 说明:此配置项指定了一个列ID,该列会自动扩展以填充表格剩余的空间。注意,该ID不能为0。 - 示例:`autoExpandColumn: 'name'` 4. **stripeRows** - 说明:布尔值,决定是否开启表格行的斑马纹(即隔行换色...

    ExtJS介绍以及GridPanel

    除了GridPanel,ExtJS还提供了许多其他组件,如FormPanel(用于创建表单)、TreePanel(用于展示树形数据结构)、Viewport(全屏布局容器)等,它们共同构成了一个强大的Web开发工具箱。 在源码层面,ExtJS使用了...

    GridPanel属性详解

    它接受一个配置数组,每个数组元素都是一个列配置对象,这些配置对象最终会被用来构建`ColumnModel`。 3. **autoExpandColumn** - **描述**:此属性用于指定哪个列可以自动扩展以填充表格中剩余的空间。其值应为...

    ExtJS4.0下的gridPanel组建完全版

    ExtJS 是一个强大的JavaScript库,专门用于构建富客户端的Web应用程序。在ExtJS 4.0版本中,GridPanel组件是其核心特性之一,它允许开发者创建数据密集型的表格展示,提供了丰富的功能和定制选项。这篇内容将深入...

    ext form gridpanel

    "Ext Form GridPanel"是Ext JS库中的一个重要组件,它结合了表格(Grid)和表单(Form)的功能,提供了一种强大而灵活的方式来显示和编辑数据。在Web应用程序开发中,这种组件常用于数据录入和展示,尤其适用于处理...

    无废话ExtJs 系统教程十四[列表:GridPanel]

    首先,要创建一个GridPanel,我们需要定义数据源。这通常通过Ext.data.Store完成,Store会连接到服务器端的数据源(如JSON或XML),并负责加载、缓存和管理数据。在配置Store时,我们需要指定模型(Model),定义...

    gridPanel添加按钮

    这只是一个基础示例,实际使用时应根据项目需求进行调整和扩展。在阅读提供的`gridpanel.doc`文档后,你将获得更具体的指导和代码示例,帮助你更好地理解并实践这个过程。记得根据你的实际环境和需求进行适当的代码...

    Extjs4 GridPanel 的几种样式使用介绍

    首先,我们来看一个简单的GridPanel示例。在这个例子中,我们创建了一个包含三列的表格,每列分别绑定到数据源中的'id'、'name'和'gender'字段。通过`dataIndex`属性,我们可以将列头与数据模型的字段关联起来,实现...

    Extjs树分页组件扩展

    在这个扩展中,TreeLoaderStore可能是结合了TreeLoader和Store概念的一个自定义类,它不仅管理树的数据,还提供了分页的功能。可能包含以下关键部分: 1. 数据接口:定义如何与服务器通信,获取分页数据。 2. 分页...

    ExtJs DWR扩展 DWRProxy、DWRTreeLoader、DWRGridProxy

    例如,开发者可以通过DWRProxy设置一个DWR服务,然后在客户端的GridPanel或TreePanel中配置相应的DWRGridProxy或DWRTreeLoader,这样就可以轻松地实现数据的动态加载和更新。这种集成方式尤其适用于需要大量实时数据...

Global site tag (gtag.js) - Google Analytics