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

javascript - w2ui - toolbar change button image on click - Stack Overflow

programmeradmin0浏览0评论

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');
        }
    }
}

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');
        }
    }
}
Share edited Oct 28, 2013 at 13:49 KyleMit 30.9k72 gold badges511 silver badges702 bronze badges asked Aug 30, 2013 at 17:21 MikeMike 1631 gold badge4 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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./web/docs/w2toolbar.set

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');
                                        }                       

            }
        }

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');
}
发布评论

评论列表(0)

  1. 暂无评论