Question

I am using w2ui. I have a toolbar with one button. This button has the icon-image "icon-delete".

When I click on the button, I want it to change the icon-image to "icon-add", but my code doesn't work.

toolbar: {
    items: [{
        type: 'button',
        id: 'hide',
        caption: 'Menü',
        img: 'icon-delete'
    }],
    onClick: function (target, data) {
        if (target == 'hide') {
            this.items.img('icon-add');
        }
    }
}
Was it helpful?

Solution 2

I created a hidden button "show" with the "icon-add" image. when the button "hide" is clicked, it get hidden and the button "show" get shown.

toolbar: {
            name: 'toolbar',
            items: [
                { type: 'button',  id: 'hide', caption: 'Menü', img: 'icon-delete' },
                { type: 'button',  id: 'show', hidden: 'true', caption: 'Menü', img: 'icon-add' }
            ],
            onClick: function (target, data) {
                if (target == 'hide' ) {w2ui['layout'].toggle('left', window.instant);

                                        this.hide('hide');
                                        this.show('show');
                                        }
                if (target == 'show' ) {w2ui['layout'].toggle('left', window.instant);

                                        this.hide('show');
                                        this.show('hide');
                                        }                       

            }
        }

OTHER TIPS

You can use toolbar.set() method to update toolbar button icon. So in your onClick event do the following:

onClick: function (target, data) {
   this.set(target, { icon: 'new_icon' });
}

See more here: http://w2ui.com/web/docs/w2toolbar.set

I think you were missing the refresh line in your original approach. Here is an example that worked for me. I added an else part

if (event.target == 'hide') {
   if (this.items[0].icon == 'icon-delete') {
     this.items[0].icon = 'icon-add';
     //do something code
   } else {
     this.items[0].icon = 'icon-delete';
     //do something else code
   }
   w2ui['toolbar'].refresh('hide');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top