`
dowhathowtodo
  • 浏览: 783882 次
文章分类
社区版块
存档分类
最新评论

ExtJS4学习笔记(十)---带搜索的Grid(SearchGrid)

 
阅读更多

项目开发中,Grid组件少不了搜索功能,在Extjs4中,搜索组件以插件的形式出现,而且实现也非常简单,搜索组件位于examples/ux/form目录下,JS文件是SearchField.js。

Grid加载搜索功能,要注意的是:

1、开启延迟加载,即Ext.Loader.setConfig({enabled: true});

2、设置插件的目录:Ext.Loader.setPath('Ext.ux', '../../examples/ux'); ,需要注意,插件所在目录一定要正确,否则加载失败,就实现不了所要功能了。

效果图:

初始加载


输入查询条件后

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>SearchGrid-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. #search-resultsa:hover{
  14. text-decoration:underline;
  15. }
  16. #search-resultsp{
  17. margin:3px!important;
  18. }
  19. .search-item{
  20. font:normal11pxtahoma,arial,helvetica,sans-serif;
  21. padding:3px10px3px10px;
  22. border:1pxsolid#fff;
  23. border-bottom:1pxsolid#eeeeee;
  24. white-space:normal;
  25. color:#555;
  26. }
  27. .search-itemh3{
  28. display:block;
  29. font:inherit;
  30. font-weight:bold;
  31. color:#222;
  32. }

  33. .search-itemh3span{
  34. float:right;
  35. font-weight:normal;
  36. margin:005px5px;
  37. width:100px;
  38. display:block;
  39. clear:none;
  40. }
  41. /*这里要注意这两张图片的路径要正确*/
  42. .x-form-clear-trigger{
  43. background-image:url(../../resources/themes/images/default/form/clear-trigger.gif);
  44. }
  45. .x-form-search-trigger{
  46. background-image:url(../../resources/themes/images/default/form/search-trigger.gif);
  47. }
  48. </style>
  49. <scripttype="text/javascript"src="../../bootstrap.js"></script>
  50. <scripttype="text/javascript"src="../../locale/ext-lang-zh_CN.js"></script>
  51. <scripttype="text/javascript"src="searchgrid.js"></script>
  52. </head>

  53. <body>
  54. <divid="demo"></div>
  55. </body>
  56. </html>

SearchGrid.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. ]);

  10. Ext.onReady(function(){
  11. Ext.define('MyData',{
  12. extend:'Ext.data.Model',
  13. fields:[
  14. 'title','author',
  15. //第一个字段需要指定mapping,其他字段,可以省略掉。
  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:'searchgrid.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. //创建Grid
  45. vargrid=Ext.create('Ext.grid.Panel',{
  46. store:store,
  47. columnLines:true,
  48. columns:[
  49. {text:"标题",width:120,dataIndex:'title',sortable:true},
  50. {text:"作者",width:140,dataIndex:'author',sortable:false},
  51. {text:"点击数",width:100,dataIndex:'hits',sortable:true},
  52. {text:"添加时间",width:150,dataIndex:'addtime',sortable:true}
  53. ],
  54. height:400,
  55. width:520,
  56. x:20,
  57. y:40,
  58. title:'ExtJS4SearchGrid-Grid搜索',
  59. disableSelection:true,
  60. loadMask:true,
  61. renderTo:'demo',
  62. viewConfig:{
  63. id:'gv',
  64. trackOver:false,
  65. stripeRows:false
  66. },
  67. dockedItems:[{
  68. dock:'top',
  69. xtype:'toolbar',
  70. items:{
  71. width:300,
  72. fieldLabel:'搜索',
  73. labelWidth:50,
  74. xtype:'searchfield',
  75. store:store
  76. }
  77. },{
  78. dock:'bottom',
  79. xtype:'pagingtoolbar',
  80. store:store,
  81. pageSize:25,
  82. displayInfo:true,
  83. displayMsg:'显示{0}-{1}条,共计{2}条',
  84. emptyMsg:'没有数据'
  85. }]
  86. })
  87. store.loadPage(1);
  88. })

代码完成了Grid组件异步加载信息、分页和搜索功能,可以满足一般使用情况了。

服务端代码,由于使用测试数据,这里只使用了最简单的方法来实现搜索效果,实际操作中,需要将查询条件置于SQL语句中,达到搜索效果。

SearchGrid.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("""title"":""newstitle"&i&"""")
  51. Echo(",")
  52. Echo("""author"":""author"&i&"""")
  53. Echo(",")
  54. Echo("""hits"":"""&i&"""")
  55. Echo(",")
  56. Echo("""addtime"":"""&Now()&"""")
  57. Echo("}")
  58. Ifi<LsThen
  59. Echo(",")
  60. EndIf
  61. Next

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

至此,带搜索功能的Grid就全部完成了。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics