1

I'm trying to hide a div block, it should show after I press preview button, can anyone help?

Code:

gift-message-form.html

<div class="actions-toolbar">
    <div class="secondary">
        <button type="submit" class="action action-update" data-bind="
                attr: {title: $t('Update')},
                click: $data.submitOptions.bind($data)">
            <span data-bind="i18n: 'Update'"></span>
        </button>
        <button class="action action-cancel" data-bind="
                attr: {title: $t('Cancel')},
                click: $data.hideFormBlock.bind($data)">
            <span data-bind="i18n: 'Cancel'"></span>
        </button>
        <button class="action action-preview" data-bind="
                attr: {title: $t('Preview')},
                click: function($data){ viewPreviewOfMessage($data)};">
            <span data-bind="i18n: 'Preview'"></span>
        </button>
    </div>
</div>
<div data-bind="visible: showPreviewOfMessage">
    <img src="">
    <span>To</span>
    <span>Message</span>
    <span>from</span>
</div>

gift-message.js (view folder under web)

    viewPreviewOfMessage: function() {
        this.showPreviewOfMessage(false);
                    //document.getElementById('preview_of_gift_message').innerHTML = getObservabale('message');
    },
                
    showPreviewOfMessage: function(){
        return true;
    },
0

Try this:

<button class="action action-preview" data-bind="
            attr: {title: $t('Preview')},
            click: showPreviewOfMessage">
     <span data-bind="i18n: 'Preview'"></span>
</button>

and:

<div data-bind="visible: previewVisible">
    <img src="">
    <span>To</span>
    <span>Message</span>
    <span>from</span>
</div>

In js component:

previewVisible: ko.observable(),

showPreviewOfMessage: function(){
    this.previewVisible(true);
},
0

I got it, this is working

gift-message-form.html

<div class="actions-toolbar">
    <div class="secondary">
        <button type="submit" class="action action-update" data-bind="
                attr: {title: $t('Update')},
                click: $data.submitOptions.bind($data)">
            <span data-bind="i18n: 'Update'"></span>
        </button>
        <button class="action action-cancel" data-bind="
                attr: {title: $t('Cancel')},
                click: $data.hideFormBlock.bind($data)">
            <span data-bind="i18n: 'Cancel'"></span>
        </button>
        <button data-bind="
                attr: {title: $t('Preview')},
                click: $data.switchPreviewOfMessage.bind($data)">
            <span data-bind="i18n: 'Preview'"></span>
        </button>
        
    </div>
</div>
<div data-bind="visible: formPreviewVisibility() || resultBlockVisibility()" style="background-image: url('./images/giftcardimage.jpg')">

    <p>hello</p>
    
</div>

declare formPreviewVisibility:null, in view JS file and add .observe('formPreviewVisibility') in initilize function and define the function like this

switchPreviewOfMessage: function () {
            if (!this.model.getObservable('alreadyAdded')()) {
                this.formPreviewVisibility(!this.formPreviewVisibility());
            } else {
                this.resultBlockVisibility(!this.resultBlockVisibility());
            }
        },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.