• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    Ext4.2的Ext.grid.plugin.RowExpander无法触发事件解决办法

    发布者: 竹韵9933 | 发布时间: 2025-8-13 21:35| 查看数: 89| 评论数: 0|帖子模式

    Ext4.2+ Ext.grid.plugin.RowExpander存在bug,添加的collapsebody,expandbody无法触发,查看了下 Ext.grid.plugin.RowExpander对应的源代码,没有添加collapsebody,expandbody事件,即使按照网上的方 法重写Ext.grid.plugin.RowExpander的init和toggleRow方法也无法触发 collapsebody,expandbody事件。
    解决办法:给grid对象添加collapsebody,expandbody事件,然后给grid配置这2个事件,同时重写Ext.grid.plugin.RowExpander的toggleRow方法,触发grid添加的这2个事件即可。
    测试源代码如下:
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    2. <html>
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    5. <title>Ext4.2+ Ext.grid.plugin.RowExpander无法触发collapsebody,expandbody事件解决办法</title>
    6. <link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" rel="external nofollow" />
    7. <script type="text/javascript" src="../../ext-all-debug.js"> </script>
    8. <script>
    9. Ext.override(Ext.grid.plugin.RowExpander, { // Override to fire collapsebody & expandbody
    10. init: function(grid) {
    11.   this.callParent(arguments);
    12. //  grid.getView().addEvents('collapsebody', 'expandbody');//ext论坛找到的解决办法,这样也无法添加事件
    13. //存储grid对象
    14. this.grid=grid
    15.   this.grid.addEvents('collapsebody', 'expandbody');//给grid对象添加事件
    16. },
    17. toggleRow: function(rowIdx, record) {
    18.   var me = this,
    19.    view = me.view,
    20.    rowNode = view.getNode(rowIdx),
    21.    row = Ext.fly(rowNode, '_rowExpander'),
    22.    nextBd = row.down(me.rowBodyTrSelector, true),
    23.    isCollapsed = row.hasCls(me.rowCollapsedCls),
    24.    addOrRemoveCls = isCollapsed ? 'removeCls' : 'addCls',
    25.    ownerLock, rowHeight, fireView;


    26.   // Suspend layouts because of possible TWO views having their height change
    27.   Ext.suspendLayouts();
    28.   row[addOrRemoveCls](me.rowCollapsedCls);
    29.   Ext.fly(nextBd)[addOrRemoveCls](me.rowBodyHiddenCls);
    30.   me.recordsExpanded[record.internalId] = isCollapsed;
    31.   view.refreshSize();


    32.   // Sync the height and class of the row on the locked side
    33.   if (me.grid.ownerLockable) {
    34.    ownerLock = me.grid.ownerLockable;
    35. //   fireView = ownerLock.getView();
    36.    view = ownerLock.lockedGrid.view;
    37.    fireView=ownerLock.lockedGrid.view;
    38.    rowHeight = row.getHeight();
    39.    // EXTJSIV-9848: in Firefox the offsetHeight of a row may not match
    40.    // it is actual rendered height due to sub-pixel rounding errors. To ensure
    41.    // the rows heights on both sides of the grid are the same, we have to set
    42.    // them both.
    43.    row.setHeight(isCollapsed ? rowHeight : '');
    44.    row = Ext.fly(view.getNode(rowIdx), '_rowExpander');
    45.    row.setHeight(isCollapsed ? rowHeight : '');
    46.    row[addOrRemoveCls](me.rowCollapsedCls);
    47.    view.refreshSize();
    48.   } else {
    49.    fireView = view;
    50.   }
    51. //通过grid触发事件,而不是view
    52. this.grid.fireEvent(isCollapsed ? 'expandbody' : 'collapsebody', row.dom, record, nextBd);
    53. //下面为ext论坛的解决办法,无效
    54. //fireView.fireEvent(isCollapsed ? 'expandbody' : 'collapsebody', row.dom, record, nextBd);
    55.   // Coalesce laying out due to view size changes
    56.   Ext.resumeLayouts(true);
    57. },
    58. });
    59. //Ext.loader.setConfig({enabled:true});
    60. Ext.onReady(function() {
    61. Ext.QuickTips.init();
    62. var store = new Ext.data.Store({
    63.    
    64.   fields:[
    65.    {name:'fileName',type:'string'},
    66.    {name:'room',type:'string'},
    67.    {name:'recordDate',type:'string'},
    68.    
    69.   ],
    70.   data:[
    71.    {fileName:'文件1',room:'会议室1',recordDate:'2014-07-03'},
    72.    {fileName:'文件2',room:'会议室2',recordDate:'2014-07-03'},
    73.    {fileName:'文件3',room:'会议室3',recordDate:'2014-07-03'}
    74.   ],
    75.   autoLoad:true
    76. });
    77. var expander = new Ext.grid.plugin.RowExpander({
    78.   rowBodyTpl:new Ext.XTemplate('<div class="detailData">pp</div>'),
    79.   listeners:{
    80.    expandbody:function(){//无法触发这个是事件
    81.     console.log('Ext.grid.plugin.RowExpander');
    82.    }
    83.   }
    84. });
    85.   Ext.create('Ext.grid.Panel',{
    86.   xtype: 'row-expander-grid',
    87.   store: store,
    88.   listeners:{
    89.    expandbody:function(){//OK,可以触发
    90.     console.log('fired from grid');
    91.    }
    92.   },
    93.   renderTo:Ext.getBody(),
    94.   columns: [
    95.    {text: "文件名称", flex: 1, dataIndex: 'fileName'},
    96.    {text: "会议室", dataIndex: 'room'},
    97.    {text: "录制日期", renderer: Ext.util.Format.dateRenderer('Y-m-d'), dataIndex: 'recordDate'}
    98.   ],
    99.   width: 600,
    100.   height: 300,
    101.   plugins:expander,
    102.   collapsible: true,
    103.   animCollapse: false,
    104.   title: 'Expander Rows in a Collapsible Grid',
    105.   iconCls: 'icon-grid'
    106. });
    107. });
    108. </script>
    109. </head>
    110. <body id="docbody">
    111. </body>
    112. </html>
    复制代码


    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表