Dynamic slot value

Asked

Viewed 103 times

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.

Note:

I would like to do this with the least possible change (unless there is a better way) to the modal component as I intend to make it as reusable as possible for future projects.

1 answer

3


You can use the showModal to store the ID of the product you want to show and then do so:

// 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: {}
  }
})
.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 = prod">Dados do produto</button></td>
    </tr>
  </table>
  <modal v-if="showModal.id" @close="showModal = {}">
    <h3 slot="header">{{showModal.id}}</h3>
    <h3 slot="body">{{showModal.name}}</h3>
  </modal>
</div>


<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>

  • Thank you Sergio

  • @Nothing from Hulahula! Great that I could help!

  • Sergio, is there any reason not to do it soon: v-on:click="showModal = prod", avoid the search (find()) right?

  • @Hulahula excellent, even better. I will edit.

  • 1

    @Hulahula I edited

  • Cool, thanks Sergio

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.