ExtJS4学习笔记(八)---Grid分页

Grid组件,当数据量很大的时候,就需要分页显示,本文介绍如何实现Extjs4 Grid的分页功能。

先看THML代码:

  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>PagingGrid-MHZG.NET</title>
  6. <linkrel="stylesheet"type="text/css"href="../../resources/css/ext-all.css"/>
  7. <scripttype="text/javascript"src="../../bootstrap.js"></script>
  8. <scripttype="text/javascript"src="../../locale/ext-lang-zh_CN.js"></script>
  9. <scripttype="text/javascript"src="paing.js"></script>
  10. </head>

  11. <body>
  12. <divid="demo"></div>
  13. </body>
  14. </html>

这里引用的JS文件,都是相对于Extjs4整个目录的。如果已经将JS和CSS文件剥离并分目录放置了,那么一定要注意修改bootstrap.js。

JS代码:

  1. Ext.require([
  2. 'Ext.grid.*',
  3. 'Ext.toolbar.Paging',
  4. 'Ext.data.*'
  5. ]);

  6. Ext.onReady(function(){
  7. Ext.define('MyData',{
  8. extend:'Ext.data.Model',
  9. fields:[
  10. 'title','author',
  11. //第一个字段需要指定mapping,其他字段,可以省略掉。
  12. {name:'hits',type:'int'},
  13. 'addtime'
  14. ]
  15. });
  16. //创建数据源
  17. varstore=Ext.create('Ext.data.Store',{
  18. //分页大小
  19. pageSize:50,
  20. model:'MyData',
  21. //是否在服务端排序
  22. remoteSort:true,
  23. proxy:{
  24. //异步获取数据,这里的URL可以改为任何动态页面,只要返回JSON数据即可
  25. type:'ajax',
  26. url:'mydata.asp',
  27. reader:{
  28. root:'items',
  29. totalProperty:'total'
  30. },
  31. simpleSortMode:true
  32. },
  33. sorters:[{
  34. //排序字段。
  35. property:'hits',
  36. //排序类型,默认为ASC
  37. direction:'DESC'
  38. }]
  39. });
  40. //创建Grid
  41. vargrid=Ext.create('Ext.grid.Panel',{
  42. store:store,
  43. columns:[
  44. {text:"标题",width:120,dataIndex:'title',sortable:true},
  45. {text:"作者",flex:200,dataIndex:'author',sortable:false},
  46. {text:"点击数",width:100,dataIndex:'hits',sortable:true},
  47. {text:"添加时间",width:100,dataIndex:'addtime',sortable:true}
  48. ],
  49. height:400,
  50. width:520,
  51. x:20,
  52. y:40,
  53. title:'ExtJS4Grid分页示例',
  54. disableSelection:true,
  55. loadMask:true,
  56. renderTo:'demo',
  57. viewConfig:{
  58. id:'gv',
  59. trackOver:false,
  60. stripeRows:false
  61. },
  62. bbar:Ext.create('Ext.PagingToolbar',{
  63. store:store,
  64. displayInfo:true,
  65. displayMsg:'显示{0}-{1}条,共计{2}条',
  66. emptyMsg:"没有数据"
  67. })
  68. })
  69. store.loadPage(1);
  70. })

关于数据,任何服务端都可以,只要返回相应的数据就可以了。这里简单使用ASP代码做了一些测试用的数据,但是这些测试代码包含了参数接收、基本判断以及分页方法。具体情况具体实施,这些代码只作为抛砖引玉的作用。

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. Ifstart=""Then
  12. start=0
  13. EndIf
  14. Iflimit=""Then
  15. limit=50
  16. EndIf
  17. sorts=Replace(Trim(Request.Form("sort")),"'","")
  18. dir=Replace(Trim(Request.Form("dir")),"'","")

  19. Dimcounts:counts=300
  20. '注意,这里的counts相当于Rs.RecordCount,也就是记录总数。

  21. DimLs:Ls=Cint(start)+Cint(limit)
  22. IfLs>=countsThen
  23. Ls=counts
  24. EndIf

  25. Echo("{")
  26. Echo("""total"":")
  27. Echo(""""&counts&""",")
  28. Echo("""items"":[")
  29. Fori=start+1ToLs
  30. Echo("{")
  31. Echo("""title"":""newstitle"&i&"""")
  32. Echo(",")
  33. Echo("""author"":""author"&i&"""")
  34. Echo(",")
  35. Echo("""hits"":"""&i&"""")
  36. Echo(",")
  37. Echo("""addtime"":"""&Now()&"""")
  38. Echo("}")
  39. Ifi<LsThen
  40. Echo(",")
  41. EndIf
  42. Next
  43. Echo("]}")
  44. FunctionEcho(str)
  45. Response.Write(str)
  46. EndFunction
  47. %>

最后,来张效果图:

ExtJS4学习笔记(八)---Grid分页