最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ExtJS4 Transform Combobox onchange() event doesn't work - Stack Overflow

programmeradmin4浏览0评论

I have a transform ComboBox in extjs4:

<script>   

Ext.onReady(function() {
        Ext.tip.QuickTipManager.init();

        var transformed = Ext.create('Ext.form.field.ComboBox', {
            typeAhead: true,
            transform: 'DisplayListID',
            forceSelection: true
        });
    });
  </script>



 <select  id="DisplayListID" onChange="change();">
      <option> ...

    </select>

ExtJS successfully transform my HTML select into a ComboBox but when I select an element, the onChange event is not fired.

How can I bind the javascript function change() to my transform CombBox?

I have a transform ComboBox in extjs4:

<script>   

Ext.onReady(function() {
        Ext.tip.QuickTipManager.init();

        var transformed = Ext.create('Ext.form.field.ComboBox', {
            typeAhead: true,
            transform: 'DisplayListID',
            forceSelection: true
        });
    });
  </script>



 <select  id="DisplayListID" onChange="change();">
      <option> ...

    </select>

ExtJS successfully transform my HTML select into a ComboBox but when I select an element, the onChange event is not fired.

How can I bind the javascript function change() to my transform CombBox?

Share asked Mar 1, 2014 at 15:53 AllOutOfSaltAllOutOfSalt 1,4923 gold badges27 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Use a ExtJs change event by adding a listener to the bobox :

listeners: {
    change: change
}

where the second change is your function.

Lorenz is right, you should use change listener, however if you are trying to convert several bos at the same time and those bos already have defined functions you will need to manually link each new transformed bo with its handler method and this could be a real nightmare. You can however subclass ComboBox control and let it handle the method linkage automatically, for this you must take into account that original 'select' gets removed after ComboBox control gets initialized. So you must get sure to keep a reference to the target method before calling the parent ComboBox init method, otherwise you will not be able to get it, something like:

initComponent: function() {
    var me = this,
        transform = me.transform,
        transformSelect,
        transformMethod;

    // Check if transform id has been supplied
    if(transform) {        
        // Attempt to retrieve target select from the DOM
        transformSelect = Ext.getDom(transform);
        // Get sure select node exists 
        if (transformSelect) {
            // Keep a reference to target method
            transformMethod = transformSelect.onchange;
        }
    }

    // Now that we have the method info
    // Allow ComboBox init method to replace original
    // select tag with ExtJs Control
    me.callParent(arguments);

    // Bind change event with original handler method
    me.on('change', transformMethod, me);
}

I've created a full working example here. I hope you find it useful.

发布评论

评论列表(0)

  1. 暂无评论