How to add vue js functions to jquery dynamically

2019-07-30 00:58发布

I am using a reusable bootstrap modal i.e same modal for edit, delete operations.

When edit or update buttons are clicked same modal will pop up and I need to append appropriate functions to the modal footer buttons according which operation it is.

My footer buttons are like,

 <div class="modal-footer">
   <button class="btn actionBtn" data-dismiss="modal">
     <span id="footer_action_button" class='glyphicon'> </span>
   </button>
   <button class="btn btn-warning" data-dismiss="modal">
     <span class='glyphicon glyphicon-remove'></span> Close
   </button>
 </div>

When edit button is clicked,

    $(document).on('click', '.edit-modal', function() {
        $('#footer_action_button').text(" Update");
        $('#footer_action_button').addClass('glyphicon-check');
        $('#footer_action_button').removeClass('glyphicon-trash');
        $('.actionBtn').addClass('btn-success');
        .....
        ......
        $('#myModal').modal('show');
        $('.actionBtn').click(updateOperation);
    });

similarly for delete action too..

here $('.actionBtn').click(updateOperation); would work well normallly.

but here updateOperation is a vueJs function,

    methods: {
            updateOperation: function () {
            .....
            .....
        },
   }

I am unable to call this update operation.

When statically adding @click="updateOperation()", it works fine.

<button class="btn actionBtn" data-dismiss="modal"
 @click="updateOperation()">            
  <span id="footer_action_button" class='glyphicon'> </span>
</button>

1条回答
姐就是有狂的资本
2楼-- · 2019-07-30 01:27

I'm not sure to quite understand your question, but here is how I've done it:

I have a Modal component:

Modal.vue:

<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" @click="close()">
            <span aria-hidden="true">×</span>
        </button>
        <h4 class="modal-title">
            {{ title }}
        </h4>
    </div>
    <div class="modal-body">
        <slot></slot>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" @click="close()">Fermer</button>
        <button type="button" class="btn btn-primary" @click="save()" :disabled="loading"><i class="fa fa-spin fa-spinner" v-if="loading"></i>Confirmer</button>
    </div>
</div>

And:

export default {
    props: ['title','save','close','loading']
}

As you can see, I take a save and close method in argument of the Modal component.

Then, when I call the component from another one, I send the methods as parameters:

Parent.vue:

<modal v-show="showModal" title="Ajouter une adresse" :save="saveAddress" :close="hideModal" :loading="savingNewAddress">
    My Modal content here
</modal>

export default {
    data () {
        return {
            this.showModal = false;
        }
    },
    methods: {
        saveAddress () {
            console.log('fired from my parent component');
        },
        hideModal () {
            this.showModal = false;
        },
        displayModal () {
            this.showModal = true;
        }
    },
    components: {
        Modal
    }
}

I hope this helps!

查看更多
登录 后发表回答