Dynamic slot value

3

I would like to know how to make the contents of the slots dynamic, in this case with the name / value of the product.

What I have 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>

Exampleofmodalremoved from here .

Note:

I would like to do this with the least possible change (unless of course there is a better way) to the modal component as I intend to make it as reusable as possible for future projects.     
asked by anonymous 28.09.2017 / 10:56

1 answer

3

You can use showModal to save the ID of the product you want to display 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>
    
28.09.2017 / 11:07