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

本文介绍了如何在ExtJS4 Grid中实现多选与全选功能,并提供了详细的HTML、CSS及JavaScript代码示例。文章还强调了在实现过程中需要注意的关键点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

标签: extjsurlfunctionquery测试json
  2057人阅读  评论(0)  收藏  举报
  分类:

上一篇文章,在Grid中加入了搜索功能(链接地址是:www.mhzg.net/a/20115/20115219110248.html),但大量数据需要删除操作的时候,总不能一条一条去删除吧,本文介绍如何在Extjs4 Grid中加入全选功能。

先来张效果图:


点击显示所选后:


注意点:

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

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

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

正文:

html代码:

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Grid多选/全选-MHZG.NET</title>
  6. <link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
  7. <style type="text/css">
  8.         #search-results a {
  9.             color: #385F95;
  10.             font:bold 11px tahoma, 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-results a:hover {
  17.             text-decoration:underline;
  18.         }
  19.         
  20.         #search-results p {
  21.             margin:3px !important;
  22.         }
  23.         
  24.         .search-item {
  25.             font:normal 11px tahoma, arial, helvetica, sans-serif;
  26.             padding:3px 10px 3px 10px;
  27.             border:1px solid #fff;
  28.             border-bottom:1px solid #eeeeee;
  29.             white-space:normal;
  30.             color:#555;
  31.         }
  32.         .search-item h3 {
  33.             display:block;
  34.             font:inherit;
  35.             font-weight:bold;
  36.             color:#222;
  37.         }
  38.         .search-item h3 span {
  39.             float: right;
  40.             font-weight:normal;
  41.             margin:0 0 5px 5px;
  42.             width:100px;
  43.             display:block;
  44.             clear:none;
  45.         }
  46.         
  47.         .x-form-clear-trigger {
  48.             background-image: url(../../resources/themes/images/default/form/clear-trigger.gif);
  49.         }
  50.         
  51.         .x-form-search-trigger {
  52.             background-image: url(../../resources/themes/images/default/form/search-trigger.gif);
  53.         }
  54.     </style>
  55. <script type="text/javascript" src="../../bootstrap.js"></script>
  56. <script type="text/javascript" src="../../locale/ext-lang-zh_CN.js"></script>
  57. <script type="text/javascript" src="selectgrid.js"></script>
  58. </head>
  59. <body>
  60. <div id="demo"></div>
  61. </body>
  62. </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.     //创建数据源
  22.     var store = Ext.create('Ext.data.Store', {
  23.         //分页大小
  24.         pageSize: 50,
  25.         model: 'MyData',
  26.         //是否在服务端排序
  27.         remoteSort: true,
  28.         proxy: {
  29.            //异步获取数据,这里的URL可以改为任何动态页面,只要返回JSON数据即可
  30.             type: 'ajax',
  31.             url: 'selectgrid.asp',
  32.             
  33.             reader: {
  34.                 root: 'items',
  35.                 totalProperty  : 'total'
  36.             },
  37.             simpleSortMode: true
  38.         },
  39.         sorters: [{
  40.             //排序字段。
  41.             property: 'hits',
  42.             //排序类型,默认为 ASC
  43.             direction: 'DESC'
  44.         }]
  45.     });
  46.     
  47.     //创建多选
  48.      var selModel = Ext.create('Ext.selection.CheckboxModel');
  49.     //创建Grid
  50.     var grid = Ext.create('Ext.grid.Panel',{
  51.         store: store,
  52.         selModel: selModel,
  53.         columnLines: true,
  54.         columns: [
  55.                 {text: "标题", width: 120, dataIndex: 'title', sortable: true},
  56.                 {text: "作者", width: 140, dataIndex: 'author', sortable: false},
  57.                 {text: "点击数", width: 100, dataIndex: 'hits', sortable: true},
  58.             {text: "添加时间", width: 150, dataIndex: 'addtime', sortable: true}
  59.         ],
  60.         height:400,
  61.         width:520,
  62.         x:20,
  63.         y:40,
  64.         title: 'ExtJS4 SearchGrid-Grid 搜索',
  65.         
  66.         disableSelection: false,//值为TRUE,表示禁止选择行
  67.         frame: true,
  68.         loadMask: true,
  69.         renderTo: 'demo',
  70.         viewConfig: {
  71.             id: 'gv',
  72.             trackOver: false,
  73.             stripeRows: false
  74.         },
  75.         dockedItems: [{
  76.             dock: 'top',
  77.             xtype: 'toolbar',
  78.             items: [{
  79.                 itemId: 'Button',
  80.                 text:'显示所选',
  81.                 tooltip:'Add a new row',
  82.                 iconCls:'add',
  83.                 handler:function(){
  84.                     var record = grid.getSelectionModel().getSelection();
  85.                     if(record.length == 0){
  86.                         Ext.MessageBox.show({
  87.                             title:"提示",
  88.                             msg:"请先选择您要操作的行!"
  89.                             //icon: Ext.MessageBox.INFO
  90.                         })
  91.                         return;
  92.                     }else{
  93.                         var ids = "";
  94.                         for(var i = 0; i < record.length; i++){
  95.                             ids += record[i].get("id")
  96.                             if(i<record.length-1){
  97.                                 ids = ids + ",";
  98.                             }
  99.                         }
  100.                         Ext.MessageBox.show({
  101.                             title:"所选ID列表",
  102.                             msg:ids
  103.                             //icon: Ext.MessageBox.INFO
  104.                         })
  105.                     }
  106.                 }
  107.             },'-',{
  108.                 width: 300,
  109.                 fieldLabel: '搜索',
  110.                 labelWidth: 50,
  111.                 xtype: 'searchfield',
  112.                 store: store
  113.             }]
  114.         }, {
  115.             dock: 'bottom',
  116.             xtype: 'pagingtoolbar',
  117.             store: store,
  118.             pageSize: 25,
  119.             displayInfo: true,
  120.             displayMsg: '显示 {0} - {1} 条,共计 {2} 条',
  121.             emptyMsg: '没有数据'
  122.         }]
  123.         
  124.     })
  125.     store.loadPage(1);
  126. })

服务端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的ORDER BY里即可。
  9. start = Request("start")
  10. limit = Request("limit")
  11. '查询时获取的参数。
  12. query = Request("query")
  13. If start = "" Then
  14.     start = 0
  15. End If
  16. If limit = "" Then
  17.     limit = 50
  18. End If
  19. sorts = Replace(Trim(Request.Form("sort")),"'",""
  20. dir = Replace(Trim(Request.Form("dir")),"'","")
  21. '测试数据,这里直接输出结果,实际应用中,应该把查询条件放到SQL语句中。
  22. If query = "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. Dim counts:counts=300
  39. '注意,这里的counts相当于Rs.RecordCount,也就是记录总数。
  40. Dim Ls:Ls = Cint(start) + Cint(limit)
  41. If Ls >= counts Then
  42.    Ls = counts
  43. End If
  44. Echo("{")
  45. Echo("""total"":")
  46. Echo(""""&counts&""",")
  47. Echo("""items"":[")
  48. For i = start+1 To Ls
  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.    If i<Ls Then
  61.      Echo(",")
  62.    End If
  63. Next
  64. Echo("]}")
  65. End If
  66. Function Echo(str)
  67.    Response.Write(str)
  68. End Function
  69. %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值