3
I would like to know how I make the slots content dynamic, in this case with the name/value of the product.
What I got is:
// register modal component
Vue.component('modal', {
template: '#modal'
})
// start app
new Vue({
el: '#app',
data: {
products: [
{id: 1, name: 'prod1', value: 20},
{id: 2, name: 'prod2', value: 40},
],
showModal: false
}
})
.modal-mask {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .5);
display: table;
transition: opacity .3s ease;
}
.modal-wrapper {
display: table-cell;
vertical-align: middle;
}
.modal-container {
width: 300px;
margin: 0px auto;
padding: 20px 30px;
background-color: #fff;
border-radius: 2px;
box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
transition: all .3s ease;
font-family: Helvetica, Arial, sans-serif;
}
.modal-header h3 {
margin-top: 0;
color: #42b983;
}
.modal-body {
margin: 20px 0;
}
.modal-default-button {
float: right;
}
.modal-enter {
opacity: 0;
}
.modal-leave-active {
opacity: 0;
}
.modal-enter .modal-container,
.modal-leave-active .modal-container {
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
<template id="modal">
<transition name="modal">
<div class="modal-mask">
<div class="modal-wrapper">
<div class="modal-container">
<div class="modal-header">
<slot name="header">
</slot>
</div>
<div class="modal-body">
<slot name="body">
default body
</slot>
</div>
<div class="modal-footer">
<slot name="footer">
default footer
<button class="modal-default-button" v-on:click="$emit('close')">
OK
</button>
</slot>
</div>
</div>
</div>
</div>
</transition>
</template>
<div id="app">
<table>
<tr v-for="prod in products" :key="prod.id">
<td>{{prod.name}}</td>
<td>{{prod.value}}</td>
<td><button id="show-modal" @click="showModal = true">Dados do produto</button></td>
</tr>
</table>
<modal v-if="showModal" @close="showModal = false">
<h3 slot="header">custom header</h3>
<h3 slot="body">custom body</h3>
</modal>
</div>
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
Example of the withdrawn modal from here.
Thank you Sergio
– Hula Hula
@Nothing from Hulahula! Great that I could help!
– Sergio
Sergio, is there any reason not to do it soon:
v-on:click="showModal = prod"
, avoid the search (find()
) right?– Hula Hula
@Hulahula excellent, even better. I will edit.
– Sergio
@Hulahula I edited
– Sergio
Cool, thanks Sergio
– Hula Hula