ExtJS4学习笔记(十一)---Grid多选/全选

上一篇文章,在Grid中加入了搜索功能,但大量数据需要删除操作的时候,总不能一条一条去删除吧,本文介绍如何在Extjs4 Grid中加入全选功能。

先来张效果图:

ExtJS4学习笔记(十一)---Grid多选/全选

点击显示所选后:

ExtJS4学习笔记(十一)---Grid多选/全选

注意点:

1、需要在JS文件中动态加载“'Ext.selection.CheckboxModel'”

2、服务端要返回数据的id值。

3、Ext.data.Model中,要有id的信息,就是因为JS代码中忘记了写id,导致调试了很久都无法获取id值,从头检查代码的时候才发现错误。

正文:

html代码:

  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
  5. <title>Grid多选/全选-MHZG.NET</title>
  6. <linkrel="stylesheet"type="text/css"href="../../resources/css/ext-all.css"/>
  7. <styletype="text/css">
  8. #search-resultsa{
  9. color:#385F95;
  10. font:bold11pxtahoma,arial,helvetica,sans-serif;
  11. text-decoration:none;
  12. }
  13. .add{
  14. background-image:url(../../examples/shared/icons/fam/cog.gif)!important;
  15. }
  16. #search-resultsa:hover{
  17. text-decoration:underline;
  18. }
  19. #search-resultsp{
  20. margin:3px!important;
  21. }
  22. .search-item{
  23. font:normal11pxtahoma,arial,helvetica,sans-serif;
  24. padding:3px10px3px10px;
  25. border:1pxsolid#fff;
  26. border-bottom:1pxsolid#eeeeee;
  27. white-space:normal;
  28. color:#555;
  29. }
  30. .search-itemh3{
  31. display:block;
  32. font:inherit;
  33. font-weight:bold;
  34. color:#222;
  35. }

  36. .search-itemh3span{
  37. float:right;
  38. font-weight:normal;
  39. margin:005px5px;
  40. width:100px;
  41. display:block;
  42. clear:none;
  43. }
  44. .x-form-clear-trigger{
  45. background-image:url(../../resources/themes/images/default/form/clear-trigger.gif);
  46. }
  47. .x-form-search-trigger{
  48. background-image:url(../../resources/themes/images/default/form/search-trigger.gif);
  49. }
  50. </style>
  51. <scripttype="text/javascript"src="../../bootstrap.js"></script>
  52. <scripttype="text/javascript"src="../../locale/ext-lang-zh_CN.js"></script>
  53. <scripttype="text/javascript"src="selectgrid.js"></script>
  54. </head>

  55. <body>
  56. <divid="demo"></div>
  57. </body>
  58. </html>

selectgrid.js:

  1. Ext.Loader.setConfig({enabled:true});
  2. Ext.Loader.setPath('Ext.ux','../../examples/ux');
  3. Ext.require([
  4. 'Ext.grid.*',
  5. 'Ext.toolbar.Paging',
  6. 'Ext.util.*',
  7. 'Ext.data.*',
  8. 'Ext.ux.form.SearchField',
  9. 'Ext.selection.CheckboxModel'
  10. ]);

  11. Ext.onReady(function(){
  12. Ext.define('MyData',{
  13. extend:'Ext.data.Model',
  14. fields:[
  15. 'id','title','author',
  16. {name:'hits',type:'int'},
  17. 'addtime'
  18. ]
  19. });
  20. //创建数据源
  21. varstore=Ext.create('Ext.data.Store',{
  22. //分页大小
  23. pageSize:50,
  24. model:'MyData',
  25. //是否在服务端排序
  26. remoteSort:true,
  27. proxy:{
  28. //异步获取数据,这里的URL可以改为任何动态页面,只要返回JSON数据即可
  29. type:'ajax',
  30. url:'selectgrid.asp',
  31. reader:{
  32. root:'items',
  33. totalProperty:'total'
  34. },
  35. simpleSortMode:true
  36. },
  37. sorters:[{
  38. //排序字段。
  39. property:'hits',
  40. //排序类型,默认为ASC
  41. direction:'DESC'
  42. }]
  43. });
  44. //创建多选
  45. varselModel=Ext.create('Ext.selection.CheckboxModel');
  46. //创建Grid
  47. vargrid=Ext.create('Ext.grid.Panel',{
  48. store:store,
  49. selModel:selModel,
  50. columnLines:true,
  51. columns:[
  52. {text:"标题",width:120,dataIndex:'title',sortable:true},
  53. {text:"作者",width:140,dataIndex:'author',sortable:false},
  54. {text:"点击数",width:100,dataIndex:'hits',sortable:true},
  55. {text:"添加时间",width:150,dataIndex:'addtime',sortable:true}
  56. ],
  57. height:400,
  58. width:520,
  59. x:20,
  60. y:40,
  61. title:'ExtJS4SearchGrid-Grid搜索',
  62. disableSelection:false,//值为TRUE,表示禁止选择行
  63. frame:true,
  64. loadMask:true,
  65. renderTo:'demo',
  66. viewConfig:{
  67. id:'gv',
  68. trackOver:false,
  69. stripeRows:false
  70. },
  71. dockedItems:[{
  72. dock:'top',
  73. xtype:'toolbar',
  74. items:[{
  75. itemId:'Button',
  76. text:'显示所选',
  77. tooltip:'Addanewrow',
  78. iconCls:'add',
  79. handler:function(){
  80. varrecord=grid.getSelectionModel().getSelection();
  81. if(record.length==0){
  82. Ext.MessageBox.show({
  83. title:"提示",
  84. msg:"请先选择您要操作的行!"
  85. //icon:Ext.MessageBox.INFO
  86. })
  87. return;
  88. }else{
  89. varids="";
  90. for(vari=0;i<record.length;i++){
  91. ids+=record[i].get("id")
  92. if(i<record.length-1){
  93. ids=ids+",";
  94. }
  95. }
  96. Ext.MessageBox.show({
  97. title:"所选ID列表",
  98. msg:ids
  99. //icon:Ext.MessageBox.INFO
  100. })
  101. }
  102. }
  103. },'-',{
  104. width:300,
  105. fieldLabel:'搜索',
  106. labelWidth:50,
  107. xtype:'searchfield',
  108. store:store
  109. }]
  110. },{
  111. dock:'bottom',
  112. xtype:'pagingtoolbar',
  113. store:store,
  114. pageSize:25,
  115. displayInfo:true,
  116. displayMsg:'显示{0}-{1}条,共计{2}条',
  117. emptyMsg:'没有数据'
  118. }]
  119. })
  120. store.loadPage(1);
  121. })

服务端selectgrid.asp:

  1. <%
  2. Response.ContentType="text/html"
  3. Response.Charset="UTF-8"
  4. %>
  5. <%
  6. '返回JSON数据,自定义一些测试数据。。
  7. '这里的参数与EXT3.x相同,区别在于排序字段和排序方式使用了新的属性。
  8. '由于这里是测试数据,接收的参数只用到了start,limit。sorts和dir在实际操作过程中,将之加入SQL的ORDERBY里即可。
  9. start=Request("start")
  10. limit=Request("limit")
  11. '查询时获取的参数。
  12. query=Request("query")
  13. Ifstart=""Then
  14. start=0
  15. EndIf
  16. Iflimit=""Then
  17. limit=50
  18. EndIf
  19. sorts=Replace(Trim(Request.Form("sort")),"'","")
  20. dir=Replace(Trim(Request.Form("dir")),"'","")

  21. '测试数据,这里直接输出结果,实际应用中,应该把查询条件放到SQL语句中。
  22. Ifquery="newstitle"Then
  23. Echo("{")
  24. Echo("""total"":")
  25. Echo("""1")
  26. Echo(""",""items"":[")
  27. Echo("{")
  28. Echo("""title"":""newstitle""")
  29. Echo(",")
  30. Echo("""author"":""author""")
  31. Echo(",")
  32. Echo("""hits"":""100""")
  33. Echo(",")
  34. Echo("""addtime"":"""&Now()&"""")
  35. Echo("}")
  36. Echo("]}")
  37. Else
  38. Dimcounts:counts=300
  39. '注意,这里的counts相当于Rs.RecordCount,也就是记录总数。

  40. DimLs:Ls=Cint(start)+Cint(limit)
  41. IfLs>=countsThen
  42. Ls=counts
  43. EndIf

  44. Echo("{")
  45. Echo("""total"":")
  46. Echo(""""&counts&""",")
  47. Echo("""items"":[")
  48. Fori=start+1ToLs
  49. Echo("{")
  50. Echo("""id"":"""&i&"""")
  51. Echo(",")
  52. Echo("""title"":""newstitle"&i&"""")
  53. Echo(",")
  54. Echo("""author"":""author"&i&"""")
  55. Echo(",")
  56. Echo("""hits"":"""&i&"""")
  57. Echo(",")
  58. Echo("""addtime"":"""&Now()&"""")
  59. Echo("}")
  60. Ifi<LsThen
  61. Echo(",")
  62. EndIf
  63. Next

  64. Echo("]}")
  65. EndIf
  66. FunctionEcho(str)
  67. Response.Write(str)
  68. EndFunction
  69. %>