`
朱嘉华
  • 浏览: 238464 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

S的使用方法汇总

阅读更多

S的使用方法汇总(1)——配置和表格控件使用
ExtJS时一套AJAX控件,本人认为它是目前我见过最好最美的JS控件库,所以非常有学习和使用价值,如果你还没有接触过ExtJS或者压根对其不感兴趣,那么没有必要看下去了,ExtJS是一套完整的RIA解决方案,也因为功能完整造成了ext-all.js有400多k,由于是基于JS和CSS的功能实现,对客户端机器性能也有一定的要求,即不支持IE6以下的版本。如果您的项目对网页响应时间有严格的限制,或者客户端操作系统过于陈旧,一定不要选择ExtJS。

本文主要是介绍ExtJS的下载和配置以及一些简单的使用方法。目前最新版本为3.0,但是本文主要介绍2.2版本。

一、ExtJS下载以及配置
1、下载地址:www.extjs.com/(这是官网,大家可以选择自己喜欢的版本下载)

2、配置过程,假设下载后的目录为Ext,我们在该目录下建立我们自己的目录MyExample(该目录用于存放你自己写的代码),配置过程如下:

(1) 新建一个页面文件Helloworld.html

(2) 在<head>和</head>之间添加如下代码:

view plaincopy to clipboardprint?
<link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" /> 
<script type="text/javascript" src="../adapter/ext/ext-base.js"></script> 
<script type="text/javascript" src="../ext-all.js"></script> 
<script type="text/javascript"> 
    Ext.onReady(function(){  
        Ext.MessageBox.alert('HelloWorld','Hello World');  
    })  
</script> 
    <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" />
    <script type="text/javascript" src="../adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../ext-all.js"></script>
    <script type="text/javascript">
        Ext.onReady(function(){
            Ext.MessageBox.alert('HelloWorld','Hello World');
        })
    </script>

(3) 这里注意<script></script>不能用</script>取代

(4) js的导入顺序不要更改

(3) 如果弹出一个HelloWorld的对话框,则代表配置成功。

二、表格控件Grid的使用
EXT中的表格功能非常强大,包括排序、缓存、拖动、隐藏某一列、自动显示行号、列汇总、单元格编辑等使用功能。我们首先介绍如何制作一个简单的Grid。

1、创建表格的列信息:

view plaincopy to clipboardprint?
var cm=new Ext.grid.ColumnModel([  
     {header:'编号',dataIndex:'id'},  
     {header:'名称',dataIndex:'name'},  
     {header:'描述',dataIndex:'desn'}  
 ]); 
           var cm=new Ext.grid.ColumnModel([
                {header:'编号',dataIndex:'id'},
                {header:'名称',dataIndex:'name'},
                {header:'描述',dataIndex:'desn'}
            ]);

2、添加数据信息:

view plaincopy to clipboardprint?
var data=[  
      ['1','name1','desn1'],  
      ['2','name1','desn1'],  
      ['3','name1','desn1'],  
      ['4','name1','desn1'],  
      ['5','name1','desn1']  
  ]; 
          var data=[
                ['1','name1','desn1'],
                ['2','name1','desn1'],
                ['3','name1','desn1'],
                ['4','name1','desn1'],
                ['5','name1','desn1']
            ]; 

3、创建数据存储对象:

view plaincopy to clipboardprint?
var ds=new Ext.data.Store({  
      proxy:new Ext.data.MemoryProxy(data),  
      reader:new Ext.data.ArrayReader({},[  
          {name:'id'},  
          {name:'name'},  
          {name:'desn'}  
      ])  
  });  
  ds.load();//这个相当的重要 
          var ds=new Ext.data.Store({
                proxy:new Ext.data.MemoryProxy(data),
                reader:new Ext.data.ArrayReader({},[
                    {name:'id'},
                    {name:'name'},
                    {name:'desn'}
                ])
            });
            ds.load();//这个相当的重要

4、表格的列模型定义好了,原始数据和数据的转换也已经完成,剩下的只需要把它们装配在一起,我们的Grid就创建成功了。

view plaincopy to clipboardprint?
var grid=new Ext.grid.GridPanel({  
     renderTo:"grid",  
     store: ds,  
     height:600,  
     cm:cm                  
 }); 
           var grid=new Ext.grid.GridPanel({
                renderTo:"grid",
                store: ds,
                height:600,
                cm:cm               
            });

5、注意:Ext.grid.Grid的renderTo属性指示EXT将表格渲染到什么地方,所以,在HTML里应该有一个<div id='grid'></div>与之对应。

6、所有代码清单如下(已通过测试):

+ expand sourceview plaincopy to clipboardprint?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Grid.aspx.cs" Inherits="Ext_example_Grid" %>  
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head id="Head1" runat="server">  
    <title>Grid </title>  
    <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" mce_href="resources/css/ext-all.css" />  
    <mce:script type="text/javascript" src="../adapter/ext/ext-base.js" mce_src="adapter/ext/ext-base.js"></mce:script>  
    <mce:script type="text/javascript" src="../ext-all.js" mce_src="ext-all.js"></mce:script>  
    <mce:script type="text/javascript"><!--  
        Ext.onReady(function(){  
              
            var cm=new Ext.grid.ColumnModel([  
                {header:'编号',dataIndex:'id'},  
                {header:'名称',dataIndex:'name'},  
                {header:'描述',dataIndex:'desn'}  
            ]);  
            var data=[  
                ['1','name1','desn1'],  
                ['2','name1','desn1'],  
                ['3','name1','desn1'],  
                ['4','name1','desn1'],  
                ['5','name1','desn1']  
            ];  
              
            var ds=new Ext.data.Store({  
                proxy:new Ext.data.MemoryProxy(data),  
                reader:new Ext.data.ArrayReader({},[  
                    {name:'id'},  
                    {name:'name'},  
                    {name:'desn'}  
                ])  
            });  
            ds.load();  
                          
            var grid=new Ext.grid.GridPanel({  
                renderTo:"grid",  
                store: ds,  
                height:600,  
                cm:cm                  
            });              
        });  
      
// --></mce:script>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div id="grid">  
      
    </div>  
    </form>  
</body>  
</html> 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Grid.aspx.cs" Inherits="Ext_example_Grid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Grid </title>
    <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" mce_href="resources/css/ext-all.css" />
    <mce:script type="text/javascript" src="../adapter/ext/ext-base.js" mce_src="adapter/ext/ext-base.js"></mce:script>
    <mce:script type="text/javascript" src="../ext-all.js" mce_src="ext-all.js"></mce:script>
    <mce:script type="text/javascript"><!--
        Ext.onReady(function(){
           
            var cm=new Ext.grid.ColumnModel([
                {header:'编号',dataIndex:'id'},
                {header:'名称',dataIndex:'name'},
                {header:'描述',dataIndex:'desn'}
            ]);
            var data=[
                ['1','name1','desn1'],
                ['2','name1','desn1'],
                ['3','name1','desn1'],
                ['4','name1','desn1'],
                ['5','name1','desn1']
            ];
           
            var ds=new Ext.data.Store({
                proxy:new Ext.data.MemoryProxy(data),
                reader:new Ext.data.ArrayReader({},[
                    {name:'id'},
                    {name:'name'},
                    {name:'desn'}
                ])
            });
            ds.load();
                       
            var grid=new Ext.grid.GridPanel({
                renderTo:"grid",
                store: ds,
                height:600,
                cm:cm               
            });           
        });
   
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="grid">
   
    </div>
    </form>
</body>
</html>


实验效果图如图1所示

 

图1 一个简单的Grid

三、表格控件Grid的功能详解
第二部分简单介绍了如何创建简单的Grid,本章将对Grid的功能进行详细的分析。

3.1 部分属性功能
1、默认情况下,Grid是可以拖放列,也可以改变列的宽度,如果要禁用这两个功能,在定义Grid对象时分别设置enableColumnMove和enableColumnResize为false即可。

2、如果希望显示斑马线效果,可以加上stripeRows:true

3、Grid还支持一种读取数据时的遮罩和提示功能,设置属性loadMask:true,在store.load()完成之前一直会显示"Loading…"

3.2 自主决定每列的宽度
1、如果要定义宽度,只要设置该列的width属性即可,如下面的代码所示。效果图如图2所示。

view plaincopy to clipboardprint?
var cm=new Ext.grid.ColumnModel([  
                {header:'编号',dataIndex:'id',width:60},  
                {header:'名称',dataIndex:'name',width:180},  
                {header:'描述',dataIndex:'desn',width:200}  
            ]); 
var cm=new Ext.grid.ColumnModel([
                {header:'编号',dataIndex:'id',width:60},
                {header:'名称',dataIndex:'name',width:180},
                {header:'描述',dataIndex:'desn',width:200}
            ]); 

 

图2 自定义每列的宽度

2、这样需要自己去计算每列的宽度,如果想让每列自动填满Grid,只需要viewConfig中的forceFit即可。使用forceFit后,Grid会根据你在cm里设置的width按比例分配,非常智能。实现代码如下:

view plaincopy to clipboardprint?
var grid=new Ext.grid.GridPanel({  
                renderTo:"grid",  
                stripeRows:true,//斑马线效果  
                loadMask:true,  
                store: ds,  
                height:600,  
                cm:cm,  
                viewConfig:{  
                    forceFit:true 
                }                  
            });  
var grid=new Ext.grid.GridPanel({
                renderTo:"grid",
                stripeRows:true,//斑马线效果
                loadMask:true,
                store: ds,
                height:600,
                cm:cm,
                viewConfig:{
                    forceFit:true
                }               
            }); 

3、我们也可以考虑autoExpandColumn,它可以让指定列的宽度自动伸展,从而填充整个表格。代码如下

view plaincopy to clipboardprint?
var grid=new Ext.grid.GridPanel({  
                renderTo:"grid",  
                stripeRows:true,//斑马线效果  
                loadMask:true,  
                store: ds,  
                height:600,  
                cm:cm,  
                autoExpandColumn:'desn' 
//                viewConfig:{  
//                    forceFit:true  
//                }                  
            });  
var grid=new Ext.grid.GridPanel({
                renderTo:"grid",
                stripeRows:true,//斑马线效果
                loadMask:true,
                store: ds,
                height:600,
                cm:cm,
                autoExpandColumn:'desn'
//                viewConfig:{
//                    forceFit:true
//                }               
            }); 

注意:autoExpandColum只能指定一列的id,注意,必须是id,原来我们设置的cm里面都没有id,现在为了使用autoExpandColumn,要给cm的desn设置id.于是在渲染时desn就可以自动延伸,否则会出错。

view plaincopy to clipboardprint?
var cm=new Ext.grid.ColumnModel([  
                {header:'编号',dataIndex:'id',width:60},  
                {header:'名称',dataIndex:'name',width:180},  
                {id:'desn',header:'描述',dataIndex:'desn',width:200}  
            ]); 
var cm=new Ext.grid.ColumnModel([
                {header:'编号',dataIndex:'id',width:60},
                {header:'名称',dataIndex:'name',width:180},
                {id:'desn',header:'描述',dataIndex:'desn',width:200}
            ]);

3.3 让Grid支持按列排序
在EXT中可以很方便地实现排序功能,只需要在定义列模型时增加sortable属性,如下面代码所示:

view plaincopy to clipboardprint?
var cm=new Ext.grid.ColumnModel([  
                {header:'编号',dataIndex:'id',width:60,sortable:true},  
                {header:'名称',dataIndex:'name',width:180},  
                {id:'desn',header:'描述',dataIndex:'desn',width:200}  
            ]); 
var cm=new Ext.grid.ColumnModel([
                {header:'编号',dataIndex:'id',width:60,sortable:true},
                {header:'名称',dataIndex:'name',width:180},
                {id:'desn',header:'描述',dataIndex:'desn',width:200}
            ]);

效果图如图3所示

 

图3 按列排序效果

3.4 显示时间类型数据
尽管返回的JSON里都是数字和字符串,但是在EXT中我们同样可以从后台取得日期类型的数据,然后交给Grid进行格式化。

1、首先定义一组数据,其中最后一列是日期格式的数据。

view plaincopy to clipboardprint?
var data=[  
                ['1','name1','desn1','2009-09-17T02:58:04'],  
                ['2','name2','desn1','2009-09-17T02:58:04'],  
                ['3','name3','desn1','2009-09-17T02:58:04'],  
                ['4','name4','desn1','2009-09-17T02:58:04'],  
                ['5','name5','desn1','2009-09-17T02:58:04']  
            ]; 
var data=[
                ['1','name1','desn1','2009-09-17T02:58:04'],
                ['2','name2','desn1','2009-09-17T02:58:04'],
                ['3','name3','desn1','2009-09-17T02:58:04'],
                ['4','name4','desn1','2009-09-17T02:58:04'],
                ['5','name5','desn1','2009-09-17T02:58:04']
            ];

2、接着我们在reader里面增加一行配置,除了设置name以外,还设置了type和dateFormat两个属性。代码如下:

view plaincopy to clipboardprint?
var store1= new Ext.data.Store({  
                proxy:new Ext.data.MemoryProxy(data),  
                reader:new Ext.data.ArrayReader({},[  
                    {name:'id'},  
                    {name:'name'},  
                    {name:'desn'},  
                    {name:'date',type:'date',dateFormat:'Y-m-dTH:i:s'}  
                ])  
            }); 
var store1= new Ext.data.Store({
                proxy:new Ext.data.MemoryProxy(data),
                reader:new Ext.data.ArrayReader({},[
                    {name:'id'},
                    {name:'name'},
                    {name:'desn'},
                    {name:'date',type:'date',dateFormat:'Y-m-dTH:i:s'}
                ])
            }); 

3、同样的,我们还需要在cm里面增加一行配置:

view plaincopy to clipboardprint?
var cm=new Ext.grid.ColumnModel([  
                {header:'编号',dataIndex:'id',width:60,sortable:true},  
                {header:'名称',dataIndex:'name',width:180},  
                {id:'desn',header:'描述',dataIndex:'desn',width:200},  
                {header:'时间',dataIndex:'date',type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日')}  
            ]); 
var cm=new Ext.grid.ColumnModel([
                {header:'编号',dataIndex:'id',width:60,sortable:true},
                {header:'名称',dataIndex:'name',width:180},
                {id:'desn',header:'描述',dataIndex:'desn',width:200},
                {header:'时间',dataIndex:'date',type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日')}
            ]);

4、代码详细信息如下所示,效果图如图4所示。

+ expand sourceview plaincopy to clipboardprint?
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head id="Head1" runat="server">  
    <title>Grid </title>  
    <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" mce_href="resources/css/ext-all.css" />  
    <mce:script type="text/javascript" src="../adapter/ext/ext-base.js" mce_src="adapter/ext/ext-base.js"></mce:script>  
    <mce:script type="text/javascript" src="../ext-all.js" mce_src="ext-all.js"></mce:script>  
    <mce:script type="text/javascript"><!--  
        Ext.onReady(function(){  
              
            var cm=new Ext.grid.ColumnModel([  
                {header:'编号',dataIndex:'id',width:60,sortable:true},  
                {header:'名称',dataIndex:'name',width:180},  
                {id:'desn',header:'描述',dataIndex:'desn',width:200},  
                {header:'时间',dataIndex:'date',type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日')}  
            ]);              
              
            var data=[  
                ['1','name1','desn1','2009-09-17T02:58:04'],  
                ['2','name2','desn1','2009-09-17T02:58:04'],  
                ['3','name3','desn1','2009-09-17T02:58:04'],  
                ['4','name4','desn1','2009-09-17T02:58:04'],  
                ['5','name5','desn1','2009-09-17T02:58:04']  
            ];  
              
            var store1= new Ext.data.Store({  
                proxy:new Ext.data.MemoryProxy(data),  
                reader:new Ext.data.ArrayReader({},[  
                    {name:'id'},  
                    {name:'name'},  
                    {name:'desn'},  
                    {name:'date',type:'date',dateFormat:'Y-m-dTH:i:s'}  
                ])  
            });   
            store1.load();  
            var grid1=new Ext.grid.GridPanel({  
                renderTo:"grid1",  
                stripeRows:true,//斑马线效果  
                loadMask:true,  
                store: store1,  
                height:200,  
                cm:cm,                  
                viewConfig:{  
                    forceFit:true 
                }                  
            });   
                     
        });  
      
// --></mce:script>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div id="grid1">  
      
    </div>  
    </form>  
</body>  
</html> 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Grid </title>
    <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" mce_href="resources/css/ext-all.css" />
    <mce:script type="text/javascript" src="../adapter/ext/ext-base.js" mce_src="adapter/ext/ext-base.js"></mce:script>
    <mce:script type="text/javascript" src="../ext-all.js" mce_src="ext-all.js"></mce:script>
    <mce:script type="text/javascript"><!--
        Ext.onReady(function(){
           
            var cm=new Ext.grid.ColumnModel([
                {header:'编号',dataIndex:'id',width:60,sortable:true},
                {header:'名称',dataIndex:'name',width:180},
                {id:'desn',header:'描述',dataIndex:'desn',width:200},
                {header:'时间',dataIndex:'date',type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日')}
            ]);           
           
            var data=[
                ['1','name1','desn1','2009-09-17T02:58:04'],
                ['2','name2','desn1','2009-09-17T02:58:04'],
                ['3','name3','desn1','2009-09-17T02:58:04'],
                ['4','name4','desn1','2009-09-17T02:58:04'],
                ['5','name5','desn1','2009-09-17T02:58:04']
            ];
           
            var store1= new Ext.data.Store({
                proxy:new Ext.data.MemoryProxy(data),
                reader:new Ext.data.ArrayReader({},[
                    {name:'id'},
                    {name:'name'},
                    {name:'desn'},
                    {name:'date',type:'date',dateFormat:'Y-m-dTH:i:s'}
                ])
            });
            store1.load();
            var grid1=new Ext.grid.GridPanel({
                renderTo:"grid1",
                stripeRows:true,//斑马线效果
                loadMask:true,
                store: store1,
                height:200,
                cm:cm,               
                viewConfig:{
                    forceFit:true
                }               
            });
                  
        });
   
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="grid1">
   
    </div>
    </form>
</body>
</html> 

 

图4 具有时间数据的Grid

3.5 自动显示行号和复选框
实际上,行号和复选框都是renderer的延伸。当然,复选框的功能要复杂得多。

1、自动显示行号:修改列模型cm,加入RowNumberer对象;

2、复选框:我们创建一个CheckboxSelectionModel()

3、详细代码如下,效果图如图5所示

view plaincopy to clipboardprint?
var sm=new Ext.grid.CheckboxSelectionModel();  
            var cm=new Ext.grid.ColumnModel([  
                new Ext.grid.RowNumberer(),  
                sm,  
                {header:'编号',dataIndex:'id',width:40,sortable:true},  
                {header:'名称',dataIndex:'name',width:180},  
                {id:'desn',header:'描述',dataIndex:'desn',width:200},  
                {header:'时间',dataIndex:'date',type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日')}  
            ]); 
var sm=new Ext.grid.CheckboxSelectionModel();
            var cm=new Ext.grid.ColumnModel([
                new Ext.grid.RowNumberer(),
                sm,
                {header:'编号',dataIndex:'id',width:40,sortable:true},
                {header:'名称',dataIndex:'name',width:180},
                {id:'desn',header:'描述',dataIndex:'desn',width:200},
                {header:'时间',dataIndex:'date',type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日')}
            ]);

 

图5 自动行号以及复选框效果图

3.6 表格分页
Grid控件对性能要求较高,如果在一个Grid里面显示上千条记录,效率会有明显下降,所以必须考虑分页问题。

1、为Grid添加分页工具条:在前面代码的基础上修改grid代码:

view plaincopy to clipboardprint?
var grid1=new Ext.grid.GridPanel({  
                renderTo:"grid1",  
                stripeRows:true,//斑马线效果  
                loadMask:true,  
                store: store1,  
                height:200,  
                cm:cm,                  
                viewConfig:{  
                    forceFit:true 
                },  
                bbar:new Ext.PagingToolbar({  
                    pageSize:10,  
                    store: store1,  
                    displayInfo:true,  
                    displayMsg:'显示第{0}条到{1}条记录,一共{2}条',  
                    emptyMsg:"没有记录" 
                })                  
            }); 
var grid1=new Ext.grid.GridPanel({
                renderTo:"grid1",
                stripeRows:true,//斑马线效果
                loadMask:true,
                store: store1,
                height:200,
                cm:cm,               
                viewConfig:{
                    forceFit:true
                },
                bbar:new Ext.PagingToolbar({
                    pageSize:10,
                    store: store1,
                    displayInfo:true,
                    displayMsg:'显示第{0}条到{1}条记录,一共{2}条',
                    emptyMsg:"没有记录"
                })               
            });

2、效果图如图6所示:

 

图6 添加分页工具条

3、如果要真正实现分页,还需要通过后台脚本获得分页数据,这部分在此不再给出

四、可编辑表格控件——EditorGrid
EditorGrid可以直接在表格里面执行添加、删除、修改、查找等功能,然后一次性保持。还有可以动态修改某个单元格,这些单元格我们先暂时不能为空,保存时会进行检测,为空就无法保存,验证信息会给予提示。

4.1 制作一个简单的EditorGrid

1、定义列,代码如下:

view plaincopy to clipboardprint?
var cm=new Ext.grid.ColumnModel([  
                {header:'编号',dataIndex:'id',width:40,editor:new Ext.grid.GridEditor(  
                    new Ext.form.TextField({  
                        allowBlank:false 
                    })  
                )},  
                {header:'名称',dataIndex:'name',width:180,editor:new Ext.grid.GridEditor(  
                    new Ext.form.TextField({  
                        allowBlank:false 
                    })  
                )},  
                {id:'desn',header:'描述',dataIndex:'desn',width:200},  
                {header:'时间',dataIndex:'date',type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日')}  
            ]);  
var cm=new Ext.grid.ColumnModel([
                {header:'编号',dataIndex:'id',width:40,editor:new Ext.grid.GridEditor(
                    new Ext.form.TextField({
                        allowBlank:false
                    })
                )},
                {header:'名称',dataIndex:'name',width:180,editor:new Ext.grid.GridEditor(
                    new Ext.form.TextField({
                        allowBlank:false
                    })
                )},
                {id:'desn',header:'描述',dataIndex:'desn',width:200},
                {header:'时间',dataIndex:'date',type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日')}
            ]);

2、定义grid,注意此时是EditorGridPanel.

view plaincopy to clipboardprint?
var grid1=new Ext.grid.EditorGridPanel({  
                renderTo:"grid1",  
                store: store1,  
                height:200,  
                clicksToEdit:1,  
                cm:cm           
            });  
var grid1=new Ext.grid.EditorGridPanel({
                renderTo:"grid1",
                store: store1,
                height:200,
                clicksToEdit:1,
                cm:cm        
            }); 

3、默认情况下,需要双击单元格才能激活编辑器,从而进行修改,不过,也可以给Grid配置clickToEdit:1,这样就可以单击单元格激活编辑器,从而进行修改,如图7所示:

 

图7 通过单击修改单元格

五、属性表格——PropertyGrid
属性表格扩展自EditorGridPanel,所以可以直接编辑右边的内容,注意:只有右边的,即使你单击左边的单元格,编辑器也会出现在右边。

定义的方法如下:

view plaincopy to clipboardprint?
<mce:script type="text/javascript"><!--  
        Ext.onReady(function(){  
            var grid=new Ext.grid.PropertyGrid({  
                title:"属性表格",  
                autoHeight:true,  
                width:400,  
                renderTo:'grid1',  
                source:{  
                    "名字":"薛敬明",  
                    "创建时间":new Date(Date.parse('12/15/2009')),  
                    "是否有效":false,  
                    "版本号":.01,  
                    "描述":"估计没有啥说的" 
                }  
            });   
                     
        });  
      
// --></mce:script> 
<mce:script type="text/javascript"><!--
        Ext.onReady(function(){
            var grid=new Ext.grid.PropertyGrid({
                title:"属性表格",
                autoHeight:true,
                width:400,
                renderTo:'grid1',
                source:{
                    "名字":"薛敬明",
                    "创建时间":new Date(Date.parse('12/15/2009')),
                    "是否有效":false,
                    "版本号":.01,
                    "描述":"估计没有啥说的"
                }
            });
                  
        });
   
// --></mce:script>

效果图如图8所示:

 

图8 属性表格效果图

六、分组表格——Group
分组表格就是在普通表格的基础上,根据某一列的数据将表格中的数据分组显示的表格控件。

1、首先定义一组数据

view plaincopy to clipboardprint?
var data=[  
                ['1','male','name1','desn1'],  
                ['2','male','name2','desn1'],  
                ['3','female','name3','desn1'],  
                ['4','male','name4','desn1'],  
                ['5','female','name5','desn1'],  
                ['6','male','name6','desn1'],  
                ['7','male','name7','desn1']  
            ]; 
var data=[
                ['1','male','name1','desn1'],
                ['2','male','name2','desn1'],
                ['3','female','name3','desn1'],
                ['4','male','name4','desn1'],
                ['5','female','name5','desn1'],
                ['6','male','name6','desn1'],
                ['7','male','name7','desn1']
            ];

2、创建表格的列信息

view plaincopy to clipboardprint?
var sm=new Ext.grid.CheckboxSelectionModel();  
            var cm=new Ext.grid.ColumnModel([  
                new Ext.grid.RowNumberer(),  
                sm,  
                {header:'编号',dataIndex:'id',width:40,sortable:true},  
                {header:'性别',dataIndex:'sex',width:180},  
                {header:'名称',dataIndex:'name',width:200},  
                {header:'描述',dataIndex:'desn',width:200}  
            ]);   
var sm=new Ext.grid.CheckboxSelectionModel();
            var cm=new Ext.grid.ColumnModel([
                new Ext.grid.RowNumberer(),
                sm,
                {header:'编号',dataIndex:'id',width:40,sortable:true},
                {header:'性别',dataIndex:'sex',width:180},
                {header:'名称',dataIndex:'name',width:200},
                {header:'描述',dataIndex:'desn',width:200}
            ]);  

3、创建数据存储对象

view plaincopy to clipboardprint?
var store1= new Ext.data.GroupingStore({  
                proxy:new Ext.data.MemoryProxy(data),  
                reader:new Ext.data.ArrayReader({},[  
                    {name:'id'},  
                    {name:'sex'},  
                    {name:'name'},  
                    {name:'desn'}  
                ]),                  
                groupField:'sex',  
                sortInfo:{field:'id',direction:"ASC"}  
            });   
            store1.load(); 
var store1= new Ext.data.GroupingStore({
                proxy:new Ext.data.MemoryProxy(data),
                reader:new Ext.data.ArrayReader({},[
                    {name:'id'},
                    {name:'sex'},
                    {name:'name'},
                    {name:'desn'}
                ]),               
                groupField:'sex',
                sortInfo:{field:'id',direction:"ASC"}
            });
            store1.load();

4、设计分组设计表格

view plaincopy to clipboardprint?
var grid1=new Ext.grid.GridPanel({  
                store: store1,  
                height:300,  
                cm:cm,                  
                view:new Ext.grid.GroupingView(),  
                renderTo:"grid1"                
            }); 
var grid1=new Ext.grid.GridPanel({
                store: store1,
                height:300,
                cm:cm,               
                view:new Ext.grid.GroupingView(),
                renderTo:"grid1"             
            });

5、代码清单如下,效果图如图9所示。

 view plaincopy to clipboardprint?
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head id="Head1" runat="server">  
    <title>分组表格控件</title>  
    <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" mce_href="resources/css/ext-all.css" />  
    <mce:script type="text/javascript" src="../adapter/ext/ext-base.js" mce_src="adapter/ext/ext-base.js"></mce:script>  
    <mce:script type="text/javascript" src="../ext-all.js" mce_src="ext-all.js"></mce:script>  
    <mce:script type="text/javascript"><!--  
        Ext.onReady(function(){  
            var sm=new Ext.grid.CheckboxSelectionModel();  
            var cm=new Ext.grid.ColumnModel([  
                new Ext.grid.RowNumberer(),  
                sm,  
                {header:'编号',dataIndex:'id',width:40,sortable:true},  
                {header:'性别',dataIndex:'sex',width:180},  
                {header:'名称',dataIndex:'name',width:200},  
                {header:'描述',dataIndex:'desn',width:200}  
            ]);              
              
            var data=[  
                ['1','male','name1','desn1'],  
                ['2','male','name2','desn1'],  
                ['3','female','name3','desn1'],  
                ['4','male','name4','desn1'],  
                ['5','female','name5','desn1'],  
                ['6','male','name6','desn1'],  
                ['7','male','name7','desn1']  
            ];  
              
            var store1= new Ext.data.GroupingStore({  
                proxy:new Ext.data.MemoryProxy(data),  
                reader:new Ext.data.ArrayReader({},[  
                    {name:'id'},  
                    {name:'sex'},  
                    {name:'name'},  
                    {name:'desn'}  
                ]),                  
                groupField:'sex',  
                sortInfo:{field:'id',direction:"ASC"}  
            });   
            store1.load();  
            var grid1=new Ext.grid.GridPanel({  
                store: store1,  
                height:300,  
                cm:cm,                  
                view:new Ext.grid.GroupingView(),  
                renderTo:"grid1"                
            });                      
        });  
      
// --></mce:script>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div id="grid1">  
      
    </div>  
    </form>  
</body>  
</html> 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>分组表格控件</title>
    <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" mce_href="resources/css/ext-all.css" />
    <mce:script type="text/javascript" src="../adapter/ext/ext-base.js" mce_src="adapter/ext/ext-base.js"></mce:script>
    <mce:script type="text/javascript" src="../ext-all.js" mce_src="ext-all.js"></mce:script>
    <mce:script type="text/javascript"><!--
        Ext.onReady(function(){
            var sm=new Ext.grid.CheckboxSelectionModel();
            var cm=new Ext.grid.ColumnModel([
                new Ext.grid.RowNumberer(),
                sm,
                {header:'编号',dataIndex:'id',width:40,sortable:true},
                {header:'性别',dataIndex:'sex',width:180},
                {header:'名称',dataIndex:'name',width:200},
                {header:'描述',dataIndex:'desn',width:200}
            ]);           
           
            var data=[
                ['1','male','name1','desn1'],
                ['2','male','name2','desn1'],
                ['3','female','name3','desn1'],
                ['4','male','name4','desn1'],
                ['5','female','name5','desn1'],
                ['6','male','name6','desn1'],
                ['7','male','name7','desn1']
            ];
           
            var store1= new Ext.data.GroupingStore({
                proxy:new Ext.data.MemoryProxy(data),
                reader:new Ext.data.ArrayReader({},[
                    {name:'id'},
                    {name:'sex'},
                    {name:'name'},
                    {name:'desn'}
                ]),               
                groupField:'sex',
                sortInfo:{field:'id',direction:"ASC"}
            });
            store1.load();
            var grid1=new Ext.grid.GridPanel({
                store: store1,
                height:300,
                cm:cm,               
                view:new Ext.grid.GroupingView(),
                renderTo:"grid1"             
            });                   
        });
   
// --></mce:script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="grid1">
   
    </div>
    </form>
</body>
</html>

 

图9 分组表格

六、可拖放的表格
首先我们看图10的效果图:

 

图10 可拖放的表格

注意图10四周的蓝色细条,把鼠标放到上面,就可以用拖放改变表格的高度和宽度,实现这一效果并不难,也不需要对写好的Grid做大的修改,只要在原先的基础上添加如下代码:

view plaincopy to clipboardprint?
var rz=new Ext.Resizable('grid1',{  
                wrap:true,  
                minHeight:100,  
                pinned:true,  
                handles:'all' 
            });  
            rz.on('resize',grid1.syncSize,grid1); 
var rz=new Ext.Resizable('grid1',{
                wrap:true,
                minHeight:100,
                pinned:true,
                handles:'all'
            });
            rz.on('resize',grid1.syncSize,grid1);

注意:

(1) Resizable必须放在render之后,否则就会出现问题;

(2) handles:'all'代表可以向所有放心拖动表格

七、Grid与右键菜单
Grid提供了四个与右键菜单相关的事件:

(1) contextmenu:全局性的右键事件;

(2)cellcontextmenu:单元格上的右键事件

(3)rowcontextmenu:行上的右键事件

(4)headercontextmenu:表头的右键事件

以下代码我们实现一个行上的右键事件,代码清单如下,效果图如11所示

view plaincopy to clipboardprint?
var contextmenu=new Ext.menu.Menu({  
                id:'theContextMenu',  
                items:[{  
                    text:'查看详情',  
                    handler:function(){  
                    }  
                }]  
            });  
            grid1.on("rowcontextmenu",function(grid,rowIndex,e){  
                e.preventDefault();  
                grid1.getSelectionModel().selectRow(rowIndex);  
                contextmenu.showAt(e.getXY());  
            });  
var contextmenu=new Ext.menu.Menu({
                id:'theContextMenu',
                items:[{
                    text:'查看详情',
                    handler:function(){
                    }
                }]
            });
            grid1.on("rowcontextmenu",function(grid,rowIndex,e){
                e.preventDefault();
                grid1.getSelectionModel().selectRow(rowIndex);
                contextmenu.showAt(e.getXY());
            }); 

0
2
分享到:
评论

相关推荐

    Thunderbird使用方法汇总

    Thunderbird是一款开源、免费的电子邮件客户端,由Mozilla基金会开发,集成了邮件管理、新闻组、RSS阅读等功能。这款软件以其强大的可定制性、安全...阅读《Thunderbird使用汇总.pdf》文件,将获得更详细的指导和示例。

    k8s(kubernetes)常见故障处理总结-详细笔记文档总结

    然而,在实际使用中,k8s 也会出现各种故障,影响系统的稳定性和可用性。因此,本文总结了 k8s 中常见的故障处理方法,以便快速诊断和解决问题。 一、k8s 之连接异常(集群故障) 在 k8s 集群中,pod 是最小的调度...

    H3C各种型号交换机端口镜像配置方法汇总集.rar

    6 S5000PV2_S5120系列交换机本地端口镜像配置方法(命令行版) 7 S5000PV3_5130_5150系列交换机本地端口镜像配置方法(WEB版) 8 S5000PV3_5130_5150系列交换机本地端口镜像配置方法(命令行版) 9 S5500_S5800_S...

    c# ms chart 控件使用方法汇总 附统计图

    C# MS Chart 控件使用方法汇总 附统计图 MS Chart 控件是 Microsoft 为 .NET 平台提供的一种数据可视化控件,可以用来创建各种类型的图表,如曲线图、柱状图、饼图等。下面是 MS Chart 控件的一些使用方法和示例...

    空间插值方法汇总.doc

    4. 改进谢别德法(Modified Shepard’s Method): 谢别德法基于距离倒数加权最小二乘法,改进版增加了圆滑参数,既能实现精确插值,又能减少“牛眼”现象。圆滑参数的大小影响插值结果的平滑程度。 5. 多元回归法...

    c#-ms-chart-控件使用方法汇总(附统计图).doc

    "C# MS Chart 控件使用方法汇总" C# MS Chart 控件是 Microsoft 公司提供的一个功能强大且灵活的图表控件,用于在 Windows 窗体应用程序中显示各种类型的图表。下面将总结 C# MS Chart 控件的使用方法,并提供一些...

    终端管理平台安装常见问题处理方法汇总.pdf

    终端管理平台安装常见问题处理方法汇总 终端管理平台安装常见问题...终端管理平台安装常见问题处理方法汇总.pdf 文件提供了详细的解决方案,旨在帮助用户解决可能出现的问题,并确保终端管理平台的顺利安装和使用。

    关于DevExpress自定义汇总计算方法的实现

    这篇博客“关于DevExpress自定义汇总计算方法的实现”探讨的是如何在使用DevExpress控件时,根据业务需求定制自己的计算逻辑。 在DevExpress的数据网格(GridView)中,内置的汇总功能可以进行简单的加减乘除等统计...

    Python字符串常用方法汇总.docx

    另一种更现代的方法是使用 `format()` 函数,它提供了一种更灵活的方式来格式化字符串: ```python message = "Name: {}, Age: {}".format(name, age) print(message) # 输出: Name: John, Age: 23 ``` ### Python...

    16种常用的数据分析方法汇总.pdf

    数据分析方法汇总 数据分析是数据挖掘和知识发现过程中的重要步骤。它可以帮助人们对数据进行描述、分析和解释,从而揭示数据中的规律和模式。本文总结了16种常用的数据分析方法,涵盖了描述统计、假设检验、信度...

    H3C各设备版本升级典型经验案例集汇总.rar

    29 S5500_S5800_S5830系列交换机使用XModem协议升级软件版本方法 30 S7000_S7500E_10500系列交换机使用XModem协议升级软件版本方法(V5) 31 S7000_S7500E_10500系列交换机使用XModem协议升级软件版本方法(V7) ...

    python格式化输出方法汇总

    `%`操作符来源于C语言的`printf`函数,虽然在Python中也有使用,但它在复杂场景下不如`str.format()`和f-string方便。例如: ```python print("My name is %s and I am %d years old." % (name, age)) ``` `%`...

    B/S结构下动态汇总报表的设计与实现

    本文深入探讨了B/S(Browser/Server,浏览器/服务器)结构下动态汇总报表的设计与实现方法。汇总报表作为管理信息系统中的重要组成部分,其设计不仅要考虑到数据的准确性与及时性,还需要具备灵活性与可扩展性。本文...

    富士施乐s1810维修手册中文版

    6. **第九章**可能是手册的最后部分,包含一些综合性的内容,比如常见问题汇总、疑难杂症解答,以及售后服务联系方式等。这里可能还会有关于打印机的保修政策和推荐的原厂配件信息。 每个章节的末尾可能还有问答集...

    S7-200SMART中定时器的使用方法和常见注意事项汇总.rar

    本文将详细解析S7-200SMART中定时器的使用方法及常见注意事项,帮助用户更好地理解和操作这一关键功能。 首先,我们要了解定时器的基本概念。在PLC编程中,定时器是用于控制程序执行时间的关键元件,它能够根据预设...

    联想V480s、TinkpadE330各版本BIOS汇总,每个版本都有exe文件和ISO两种文件,升级非常方便,内有各版本对图片

    联想V480s、TinkpadE330各版本BIOS汇总,包含的版本为1.06-1.16,每个版本都有exe文件和ISO两种文件,升级非常方便 一、exe文件升级方法:1.重新启动计算机。2. 找到已下载的文件 xxx.exe升级文件。双击运行按步骤...

    数据降维方法汇总(中文)

    ### 数据降维方法汇总 #### 一、引言 数据降维是数据分析和机器学习中的一个重要环节,旨在减少数据集的维度,同时保留尽可能多的信息。本文将详细介绍几种主流的数据降维方法,包括主成分分析(PCA)、线性判别分析...

    算法相关汇总1

    算法汇总是指在解决问题时,使用数学计算和数组问题的方法来解决问题。以下是算法相关汇总的知识点: 1. 连续子数组的问题和为某值的倍数问题 利用s[i,j]= s[0,j]-s[0,i]的概念,这里用到了余数的思想。如果上一个...

Global site tag (gtag.js) - Google Analytics