I have a button which when clicked opens up a modal, and the content to show in the modal is based on the data-attributes passed to the button.
My button,
<button class="btn btn-info" data-toggle="modal"
data-target="#someModal" :data-id=item.id :data-name=item.name>
Edit
</button>
In my modal, I have some buttons and when clicked I should call a vuejs function with a parameter, which is the data-attribute.
My modal button,
<div class="modal-footer">
<button type="button" class="btn btn-danger"
@click.prevent="deleteItem()" data-dismiss="modal">
Delete
</button>
<button type="button" class="btn btn-warning" data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
Here I have to pass a parameter to deleteItem()
, and that parameter is the data-id
which I get from the button above.
Code for Modal
<div id="deleteModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete</h4>
</div>
<div class="modal-body">
<div class="deleteContent">
Are you Sure you want to delete ?
</div>
<div class="modal-footer">
<button type="button" class="btn actionBtn btn-danger"
@click.prevent="deleteItem()"
data-dismiss="modal">
Delete <span id="footer_action_button"
class='glyphicon glyphicon-trash'> </span>
</button>
<button type="button" class="btn btn-warning"
data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
</div>
</div>
</div>
</div>
<button type="button" @click.prevent="deleteItem()" :data-id="1" data-dismiss="modal">
Delete <span id="footer_action_button" class='glyphicon glyphicon-trash'> </span>
</button>
methods:{
deleteItem: function(){
var id = event.target.getAttribute('data-id');
console.log(id) // 1
}
}
You can pass the "item.id" like this too:
<button type="button" @click="deleteItem(item.id)">Delete</button>
I recommend doing a console.log(this)
inside a component function, then calling that function on a button click so you can inspect all the properties of the component.
(See the attached image, below, for example output of the above console.log
statement.)
This shows you that amongst others, you have access to the $el
property holding the DOM element. Which opens up a whole lot of possibilities, such as being able to add the following code to the mounted
lifecycle hook of your component:
mounted() {
console.log(this);
this.myAttribute = this.$el.getAttribute('data-attribute-name');
},
And for this example, this.myAttribute would have been defined in (for example) your data property:
// This value gets attributed during an earlier lifecycle hook.
// It will be overridden in the component's mounted() lifecycle hook.
data() {
return {
myAttribute: 'defaultvalue'
}
}
Vue.js (v2) Lifecycle hooks documentation
Example output when executing console.log(this)
inside a component:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With