简体   繁体   English

在 EXTJS 的网格中使用时,afterrender 不适用于组合框

[英]afterrender not working for combobox when used in grid in EXTJS

This is my code for combo box inside grid:这是我的网格内组合框的代码:

{
    header: 'FSCS',
    dataIndex: 'acntOvrrideTypeCd',
    flex: 1,
    renderer: function(val, metaData, record, rowIndex, colIndex) {
        var id = Ext.id();
        var store = new Ext.data.Store({
            fields: ['code', 'description'],
            data: [{
                "code": "",
                "description": ""
            }, {
                "code": "E",
                "description": "E"
            }, {
                "code": "D",
                "description": "D"
            }, {
                "code": "S",
                "description": "S"
            }]
        });

        Ext.Function.defer(
            (function() {
                var cb = Ext.create('Ext.form.ComboBox', {
                    id: 'acntOvrrideTypeCd-' + rowIndex,
                    queryMode: 'local',
                    renderTo: id,
                    store: store,
                    forceSelection: true,
                    triggerAction: 'all',
                    lazyRender: true,
                    size: 5,
                    valueField: 'code',
                    displayField: 'description',
                    value: val
                    //listeners:{
                    //    scope: this,
                    //    'select': Ext.getCmp('amlFscsForm').controller.amlShow(rowIndex)
                    //}

                });
                cb.on(afterrender, function() {
                    console.log("------- box---" + rowIndex);
                    Ext.getCmp('amlFscsForm').controller.amlShow(rowIndex);
                });
            }), 0.25);

        console.log("i----------" + id);
        return (Ext.String.format('<div id="{0}"></div>', id));
    }
}

'afterrender' event is not fired. 'afterrender' 事件不会被触发。 I need to enable or disable component after its rendered.我需要在渲染后启用或禁用组件。

Can anyone help?任何人都可以帮忙吗?

It's just a typo, afterrender should be in quotes otherwise you will just add the function for undefined event.这只是一个错字,afterrender 应该用引号引起来,否则你只会为未定义的事件添加函数。

cb.on('afterrender',function(){
     console.log("------- box---" + rowIndex);
     Ext.getCmp('amlFscsForm').controller.amlShow(rowIndex);
});

There are a few problems with your code.您的代码存在一些问题。

  1. It looks like you're trying to create a combobox in the renderer function of a grid (your code at the top didn't get included in the code block).看起来您正在尝试在网格的渲染器函数中创建一个组合框(您在顶部的代码未包含在代码块中)。 You're better off using the Ext.grid.plugin.CellEditing plugin instead, which will create a field on demand instead of when the column renders.您最好改用Ext.grid.plugin.CellEditing插件,它将按需创建一个字段,而不是在列呈现时创建。 Plus, every time your grid view refreshes you'll be creating another store and combobox for every row in the grid.另外,每次刷新网格视图时,您都会为网格中的每一行创建另一个存储和组合框。 Not good for performance, not good for the user experience either.不利于性能,也不利于用户体验。

  2. When calling defer, the duration is in milliseconds, not seconds.调用 defer 时,持续时间以毫秒为单位,而不是秒。 Also, you don't need to wrap the function in parenthesis.此外,您不需要将函数括在括号中。 Just give it the function itself.只需给它函数本身。 Like this:像这样:

     Ext.defer(function(){ // do stuff }, 25);
  3. Setting lazyRender to true only works if your component is the child of some container that doesn't render all its components immediately (like a tabpanel).将 lazyRender 设置为 true 仅当您的组件是某个容器的子级时才有效,这些容器不会立即呈现其所有组件(如选项卡面板)。

  4. It may be easier to just set the disabled config in the combobox when you create it instead of when you render it, unless you don't have the information available at creation time.除非在创建时没有可用的信息,否则在创建组合框中而不是在渲染时在组合框中设置禁用的配置可能更容易。

  5. Like nscrob said, when using the on method you need to specify the event as a string.就像 nscrob 所说的,使用on方法时,您需要将事件指定为字符串。 If you use the listeners config (which you have commented out), you can just do:如果您使用侦听器配置(已注释掉),则可以执行以下操作:

     listeners: { afterrender: function(){ console.log("------- box---" + rowIndex); Ext.getCmp('amlFscsForm').controller.amlShow(rowIndex); }, select: function(){ Ext.getCmp('amlFscsForm').controller.amlShow(rowIndex); } }

    It's important to note that the scope of these listener functions defaults to the component itself (your combobox) so scope: this is unnecessary.请务必注意,这些侦听器函数的范围默认为组件本身(您的组合框),因此scope: this是不必要的。 Unless you want the scope to be whatever object is creating this combobox, that is.除非您希望范围是创建此组合框的任何对象,否则。

The first point is the most important.第一点是最重要的。 Look into using the CellEditing (or RowEditing) plugin and I guarantee things will go a lot more smoothly.考虑使用 CellEditing(或 RowEditing)插件,我保证事情会更顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM