How to show an elements value in a vue modal?

Viewed 4356

I have several elements that are displayed as <li> elements in a loop. For each element I want behavior such that when the element is clicked a modal box opens up. Inside the modal box I want contents that are specific to the element that was clicked.

The data below shows all the elements:

              {value: 10, name: "foo"},
              {value: 12, name: "bar"},
              {value: 14, name: "foobar"},
              {value: 22, name: "test"},
              {value: 1, name: "testtooo"},
              {value: 8, name: "something"}

When I click on an element, I want to see the value property for it inside the modal box.

I've created a fiddle for this: https://jsfiddle.net/hvb9hvog/14/

Question

I've gotten the modal working, however, how can I show each elements value property inside the modal?

3 Answers

I am sure there are multiple ways to go about this, but one way would be to create a new data property, let's call it value. When you @click the li you get it's value, set it to the value property and display that value property in the body of the modal ({{this.value}}).

You can have two @click methods, so create another one that updates the data property you just created, called value.

Here's a fiddle

Relevant code changes:

In your li element:

<li v-for="request in filteredRequests">
    <a href="#" @click="showModal = true; setVal(request.value)">{{request.name}}</a>
</li>

In your modal markup:

<modal v-if="showModal" @close="showModal = false">
    <!--
    you can use custom content here to overwrite
    default content
    -->
    <h3 slot="header">custom header</h3>
    <div slot="body">
        {{this.value}}
    </div>
</modal>

In vue data:

data: {
    requests:   [
        {value: 10, name: "foo"},
        {value: 12, name: "bar"},
        {value: 14, name: "foobar"},
        {value: 22, name: "test"},
        {value: 1, name: "testtooo"},
        {value: 8, name: "something"}
    ], 
    num: 0,
    showModal: false,
    value: 9999999999
},

In vue methods:

methods: {
    setVal(val) {
        this.value = val;
    }
},

Vue.component('modal', {
  template: '#modal-template'
})
var vm = new Vue({
  el: "#app",
  data: {
    requests: [{
        value: 10,
        name: "foo"
      },
      {
        value: 12,
        name: "bar"
      },
      {
        value: 14,
        name: "foobar"
      },
      {
        value: 22,
        name: "test"
      },
      {
        value: 1,
        name: "testtooo"
      },
      {
        value: 8,
        name: "something"
      }

    ],
    num: 0,
    showModal: false,
    value: 9999999999
  },
  methods: {
    setVal(val) {
      this.value = val;
    }
  },
  computed: {
    c: function() {
      return `Slider Number: (${this.num})`
    },
    filteredRequests() {
      return this.requests.filter(r => r.value > this.num)
    }
  },
});
.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;
}


/*
 * The following styles are auto-applied to elements with
 * transition="modal" when their visibility is toggled
 * by Vue.js.
 *
 * You can easily play with the modal transition by editing
 * these styles.
 */

.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);
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://unpkg.com/vue@2.3.4/dist/vue.js"></script>
<script type="text/x-template" id="modal-template">
  <transition name="modal">
    <div class="modal-mask">
      <div class="modal-wrapper">
        <div class="modal-container">

          <div class="modal-header">
            <slot name="header">
              default 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" @click="$emit('close')">
                OK
              </button>
            </slot>
          </div>
        </div>
      </div>
    </div>
  </transition>
</script>
<div id="app">
  <div class="form-horizontal">
    <div class="form-group">
      <label class="col-md-2 control-label">色</label>
      <div class="col-md-10">
        <input class="form-control" v-model="c" :style="{backgroundColor: c}" />
        <div class="help-block">
          <input type="range" min="0" max="360" v-model.number="num" />
          <ul class="ml-thumbs">
            <li v-for="request in filteredRequests">
              <a href="#" @click="showModal = true; setVal(request.value)">{{request.name}}</a>
            </li>
          </ul>
          <modal v-if="showModal" @close="showModal = false">
            <!--
            you can use custom content here to overwrite
            default content
          -->
            <h3 slot="header">custom header</h3>
            <div slot="body">
              {{this.value}}
            </div>
          </modal>
        </div>
      </div>
    </div>
  </div>
</div>

Add "req" property to data

data() {
  return {
     ...
     req: {},
     ...
  }
}

set click event:

<a href="#" @click="showModal = true;req = request">{{request.name}}</a>

add body slot

 ...
 <h3 slot="header">custom header</h3>
 <div slot="body">
   {{req.value}}
 </div>
 ...

https://jsfiddle.net/w4e6hr86/

I'm not sure if you are asking this using Vue.js or just JS. So, here are my answers (basic examples). I recommend you to read about event delegation + events on vuejs.

Vue Js

<template>
  <div class="content">
    <ul>
      <li v-for="item in items" @click.prevent="showModal(item)">{{ item }}</li>
    </ul>

    <div class="modal" v-show="isModalVisible">
      {{ JSON.stringify(selectedItem) }}
      <a href="#" @click.prevent="selectedItem = null">close modal</a>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'something',
    data () {
      return {
        selectedItem: item,
        items: [{
          id: 1,
          name: 'something'
        }, {
          id: 2,
          name: 'something #2'
        }]
      }
    },
    computed: {
      isModalVisible () {
        return this.selectedItem !== null
      }
    }
    methods: {
      showModal (item) {
        this.selectedItem = item
      }
    }
  }
</script>

Plain javascript

const toggleModal = content => {
  const $body = document.querySelector('body')
  const $modal = $body.querySelector('.modal')
  $modal && $modal.remove()
  $body.insertAdjacentHTML('beforeend',`<div class="modal">${content}</div>`)
}

document.querySelector('ul').addEventListener('click', e => {
  if (! e.target.matches('li')) {
    return
  }

  toggleModal(e.target.innerText)
});
  1. About Event delegation.
  2. About insertAdjacentHtml.
  3. About Vuejs Event handling
Related