ExtJS4.2实例:表格Grid嵌套(内部Grid)-MVC模式

前文 ExtJS4.2 Grid嵌套实例中讲解了如何在表格Grid中嵌套Grid,即外部Grid中每行分别展现为不同的内部Grid,比如常见的费用报销业务,费用类别有往返车费和住宿费,其中往返车费需要填写往返类型、交通工具、费用日期、出发地、目的地和金额,住宿费只需填写入住日期、离开日期、单价和金额,在一个表格中包含多种费用类别,每个费用类别下又有多条明细,所需实现的业务场景图片如:


在线演示  /   示例代码

由于前文未采用MVC模式来组织代码,代码可读性不强,本文将在前文基础上按照MVC模式来重新实现该业务场景,以供有需求的朋友参考学习。

外部表格Grid
外部表格Grid主要采用Ext.grid.plugin.RowExpander插件来实现在行记录里展示内部Grid,需设置ptype='rowexpander',并指定rowBodyTpl,同时pluginId设为'rowExpander'( 后续需通过 pluginId来获取 ),代码如下:
  1. Ext.define('Itdatum.view.UserList' ,{
  2.     extend: 'Ext.grid.Panel',
  3.     alias : 'widget.userlist',
  4.                
  5.     border : false,
  6.                         columnLines : true,
  7.                         columns : [ {
  8.                                 align : 'left',
  9.                                 dataIndex : 'feeitmtyp',
  10.                                 exportable : true,
  11.                                 header : '费用类别',
  12.                                 id : 'fituitag12',
  13.                                 menuDisabled : true,
  14.                                 renderer : renderFeeItemType,
  15.                                 style : 'text-align:center;',
  16.                                 width : 200
  17.                         }, {
  18.                                 align : 'left',
  19.                                 dataIndex : 'rbmfndnam',
  20.                                 exportable : true,
  21.                                 hidden : true,
  22.                                 id : 'fituitag13',
  23.                                 menuDisabled : true,
  24.                                 style : 'text-align:center;',
  25.                                 width : 80
  26.                         }, {
  27.                                 align : 'left',
  28.                                 dataIndex : 'remark',
  29.                                 exportable : true,
  30.                                 flex : 1,
  31.                                 header : '备注',
  32.                                 id : 'fituitag14',
  33.                                 menuDisabled : true,
  34.                                 style : 'text-align:center;',
  35.                                 width : 150
  36.                         }, {
  37.                                 align : 'right',
  38.                                 dataIndex : 'rbmtot',
  39.                                 exportable : true,
  40.                                 header : '金额',
  41.                                 id : 'fituitag15',
  42.                                 menuDisabled : true,
  43.                                 style : 'text-align:center;',
  44.                                 summaryType : 'sum',
  45.                                 width : 100
  46.                         }, {
  47.                                 align : 'left',
  48.                                 dataIndex : 'rbmbeltyp',
  49.                                 exportable : true,
  50.                                 hidden : true,
  51.                                 id : 'fituitag16',
  52.                                 menuDisabled : true,
  53.                                 style : 'text-align:center;',
  54.                                 width : 80
  55.                         }, {
  56.                                 align : 'center',
  57.                                 exportable : false,
  58.                                 header : '操作',
  59.                                 id : 'actioncolumn17',
  60.                                 items : [ {
  61.                                         handler : deleteFeeItemType,
  62.                                         iconCls : 'delete-default',
  63.                                         id : 'button18',
  64.                                         tooltip : '删除',
  65.                                         xtype : 'button'
  66.                                 } ],
  67.                                 menuDisabled : true,
  68.                                 style : 'text-align:center;',
  69.                                 width : 80,
  70.                                 xtype : 'actioncolumn'
  71.                         } ],
  72.                         fbar : [ {
  73.                                 handler : doSubmit,
  74.                                 iconCls : 'submit-default',
  75.                                 id : 'bt_submit',
  76.                                 text : '保存',
  77.                                 xtype : 'button'
  78.                         }, {
  79.                                 iconCls : 'reset-default',
  80.                                 id : 'button11',
  81.                                 text : '重置',
  82.                                 xtype : 'button'
  83.                         } ],
  84.                         features : [ {
  85.                                 ftype : 'summary',
  86.                                 id : 'summary19'
  87.                         } ],
  88.                         forceFit : false,
  89.                         id : 'feeGrid',
  90.                         loadMask : true,
  91.                         plugins : [ {
  92.                                 pluginId : 'rowExpander',
  93.                                 ptype : 'rowexpander',
  94.                                 rowBodyTpl : [
  95.                                         '
    ',
  96.                                         '
'
                                ]
                        } ],
                        selType : 'rowmodel',
                        sortableColumns : false,
                        store : 'FeeGridStore',
                        stripeRows : true,
                        tbar : [ {
                                displayField : 'rbmfndnam',
                                editable : false,
                                fieldLabel : '添加费用类别',
                                hiddenName : 'feeitmtyp',
                                id : 'feeitmtyp',
                                labelStyle : 'text-align:right',
                                listeners : {
                                        'select' : selectFeeItemType
                                },
                                name : 'feeitmtyp',
                                repeatTriggerClick : true,
                                store : createStore(),
                                triggerAction : 'all',
                                valueField : 'rbmfndcod',
                                xtype : 'combo'
                        } ],
                        title : '费用报销明细',
                        viewConfig : {
                                enableTextSelection : true
                        },
               

    initComponent: function() {
                        
        this.callParent(arguments);
    }
});

监听Grid.view的expandBody和collapsebody事件

同时监听Grid.store的add事件,即新增一种费用类别时,自动展开该费用类别的内部Grid。
  1. Ext.define('Itdatum.controller.UserController', {
  2.     extend: 'Ext.app.Controller',
  3.                
  4.                 views: [
  5.         'UserList','UserEdit'
  6.     ],
  7.    
  8.     stores: [
  9.         'UserStore','FeeGridStore','WfcfGridStore','CcbtGridStore','SnjtfGridStore','ZsfGridStore'
  10.     ],
  11.    
  12.     models: ['UserModel','FeeGridModel','WfcfInnerModel','CcbtInnerModel','SnjtfInnerModel','ZsfInnerModel'],
  13.                
  14.     init: function() {
  15.         this.control({
  16.             'viewport > userlist': {
  17.                 itemdblclick: this.editUser
  18.             },
  19.             'useredit button[action=save]': {
  20.                 click: this.updateUser
  21.             },
  22.             'userlist': {
  23.                 afterrender: function(feeGrid){//侦听userlist渲染  
  24.                      feeGrid.view.on('expandBody', function (rowNode, record, expandRow, eOpts) {
  25.                           var rbmbeltyp=record.get('rbmbeltyp');
  26.                           if(rbmbeltyp==='WFCF') {
  27.                               displayWfcfInnerGrid(record);
  28.                            }else if(rbmbeltyp==='SNJTF') {
  29.                                displaySnjtfInnerGrid(record);
  30.                            }else if(rbmbeltyp==='ZSF') {
  31.                                displayZsfInnerGrid(record);
  32.                            }else if(rbmbeltyp==='CCBT') {
  33.                                displayCcbtInnerGrid(record);
  34.                            }
  35.                      });
  36.                                                                   
  37.                      feeGrid.view.on('collapsebody', function (rowNode, record, expandRow, eOpts) {
  38.                            var rbmbeltyp=record.get('rbmbeltyp');
  39.                            if(rbmbeltyp==='WFCF') {
  40.                                destroyWfcfInnerGrid(record);
  41.                            }else if(rbmbeltyp==='SNJTF') {
  42.                                destroySnjtfInnerGrid(record);
  43.                            }else if(rbmbeltyp==='ZSF') {
  44.                                destroyZsfInnerGrid(record);
  45.                            }else if(rbmbeltyp==='CCBT') {
  46.                                destroyCcbtInnerGrid(record);
  47.                            }
  48.                      });
  49.                                                                   
  50.                      feeGrid.getStore().on('add',function( store, records, index, eOpts){
  51.                            //alert(records.length);
  52.                            feeGrid.getPlugin('rowExpander').toggleRow(index,records[0]);
  53.                      });
  54.                 }  
  55.             }
  56.         });
  57.     },
  58.    
  59.     editUser: function(grid, record) {
  60.         alert('Edit User');
  61.         //console.log('Double clicked on ' + record.get('name'));
  62.         
  63.         var view = Ext.widget('useredit');

  64.         view.down('form').loadRecord(record);
  65.     },
  66.    
  67.     updateUser: function(button) {
  68.         alert('UpdateUser');
  69.         //console.log('clicked the Save button');
  70.     },

  71.     onPanelRendered: function() {
  72.         //console.log('The panel was rendered');
  73.     }
  74. });

其他代码较多,不一一贴出来了,请自行研究在线实例

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11984612/viewspace-1259224/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11984612/viewspace-1259224/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尝试回答你的问题。以下是一个 ExtJS4.2 实例,包含一个表格和一个带有联动下拉列表的列: ```javascript Ext.require([ 'Ext.grid.*', 'Ext.data.*', 'Ext.form.*' ]); Ext.onReady(function() { // 创建一个简单的数据存储 var store = Ext.create('Ext.data.Store', { fields: ['name', 'email', 'phone', 'department'], data: [ { 'name': 'Lisa', "email":"lisa@simpsons.com", "phone":"555-111-1224", "department": "Sales" }, { 'name': 'Bart', "email":"bart@simpsons.com", "phone":"555-222-1234", "department": "Marketing" }, { 'name': 'Homer', "email":"homer@simpsons.com", "phone":"555-222-1244", "department": "Engineering" }, { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254", "department": "Support" } ] }); // 创建一个下拉框的存储 var departmentStore = Ext.create('Ext.data.Store', { fields: ['name'], data : [ { "name": "Sales" }, { "name": "Marketing" }, { "name": "Engineering" }, { "name": "Support" } ] }); // 创建一个表格 var grid = Ext.create('Ext.grid.Panel', { renderTo: Ext.getBody(), store: store, height: 250, width: 500, title: 'Employee Directory', columns: [ { header: 'Name', dataIndex: 'name' }, { header: 'Email', dataIndex: 'email', flex: 1 }, { header: 'Phone', dataIndex: 'phone' }, // 创建一个带有联动下拉列表的列 { header: 'Department', dataIndex: 'department', editor: { xtype: 'combobox', store: departmentStore, displayField: 'name', valueField: 'name', editable: false, forceSelection: true } } ], // 启用编辑功能 plugins: [ Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1 }) ] }); }); ``` 这个例子中,我们创建了一个名为 `grid` 的表格,包含四个列:姓名、电子邮件、电话和部门。其中,部门列是一个带有联动下拉列表的列,该下拉列表的数据存储在 `departmentStore` 中。我们使用 `Ext.grid.plugin.CellEditing` 插件启用了编辑功能,从而允许用户编辑表格中的数据。 希望这个例子对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值