Search code examples
eventsextjsextjs6extjs6-classictreepanel

ExtJS 6: TreePicker does not fire change event


See fiddle here: https://fiddle.sencha.com/#fiddle/2iig&view/editor

The docs (https://docs.sencha.com/extjs/6.6.0/classic/Ext.ux.TreePicker.html#event-change) list 'change' in the events section but when I set the value or reset the field this event never fires. The 'select' event fires as expected but that only fires when the user selects a field.

EDIT:

Based on Snehal's suggestion below, I was able to accomplish this using the following override. Not sure if there is a simpler way to do it but this was the best I could manage:

Ext.define('MyApp.overrides.TreePicker', {
    override: 'Ext.ux.TreePicker',

    setValue: function (value) {
        var me = this,
            record;
        me.value = value;
        if (me.store.loading) {
            // Called while the Store is loading. Ensure it is processed by the onLoad method.
            return me;
        }
        // try to find a record in the store that matches the value
        record = value ? me.store.getNodeById(value) : me.store.getRoot();
        if (value === undefined) {
            record = me.store.getRoot();
            me.value = record.getId();
        } else {
            record = me.store.getNodeById(value);
        }

        // zeke - this is the only line I added to the original source
        // without this the 'change' event is not fired
        me.callSuper([value]);

        // set the raw value to the record's display field if a record was found
        me.setRawValue(record ? record.get(me.displayField) : '');

        return me;
    }
});

Solution

  • Because setValue function does not call this.callParent(). You can do something like this in setValue function.

    setValue: function(value) {
        var me = this,
            record;
        if (me.store.loading) {
            // Called while the Store is loading. Ensure it is processed by the onLoad method.
            return me;
        }
    
        // try to find a record in the store that matches the value
        record = value ? me.store.getById(value) : me.store.getRoot();
    
        me.callParent([record.get('valueField')]);
        return me;
    },