首页 > 解决方案 > 如何以编程方式将工具提示添加到组合框

问题描述

我正在尝试combobox使用以下代码在运行时向 a 添加提示,但它不起作用:

onStartReport: function (aButton) {
    var lTip = Ext.getCmp('datasources');
    lTip.setTooltip("Information on datasource");
}

我也试过这个,但我得到一个错误:

onStartReport: function (aButton) {
     var tip = Ext.create('Ext.tip.ToolTip', {
     target: 'datasources',
     html: 'Information on datasource'
     });        
}

查看经典:

{
    xtype: 'combo',
    itemId: 'datasources',
    name: 'datasources',
    fieldLabel: 'Data sources',
    displayField: 'description',
    valueField: 'id',
    queryMode: 'local',
    value: 0,
    forceSelection: true,
    editable: false,   
    store: {
        data: [
            {id: 0, description: 'OnLine'},
            {id: 1, description: 'History'}
        ],
        fields: [
            {name: 'id', type: 'int'},
            {name: 'description', type: 'string'}               
        ],
        autoLoad: true
    }
}

标签: extjs

解决方案


这个方法应该没问题:

onStartReport: function (aButton) {
    var tip = Ext.create('Ext.tip.ToolTip', {
        target: 'datasources',
        html: 'Information on datasource'
});

问题是您的组件并没有真正的id,您添加的唯一配置是itemId并且它们并不完全相同,请参阅文档。这也是为什么Ext.getCmp('datasources')不起作用。

解决此问题的一种方法是简单地更改itemIdid并找到参考。

如果您不想为组件添加 id 并继续使用 itemId,则可以使用以下代码:

onStartReport: function (aButton) {
    var combo = Ext.ComponentQuery.query('#datasources')[0],
        tip = Ext.create('Ext.tip.ToolTip', {
        target: combo.el,
        html: 'Information on datasource'
});

还有第三个选项是在组合框与调用该onStartReport方法的组件/控制器的关系中获取组合框。我在这里添加了一个示例:https ://fiddle.sencha.com/#view/editor&fiddle/2hap


推荐阅读