source

vue 모드에서 요소 값을 표시하는 방법

goodcode 2022. 11. 5. 11:42
반응형

vue 모드에서 요소 값을 표시하는 방법

다음과 같이 표시되는 요소가 몇 가지 있습니다.<li>루프의 요소각 요소에 대해 요소를 클릭하면 모달 상자가 열리는 동작을 원합니다.modal 박스 내에서는 클릭한 요소에 특화된 콘텐츠를 원합니다.

아래 데이터는 모든 요소를 보여 줍니다.

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

요소를 클릭했을 때,value모달 박스 안에 있습니다.

이 바이올린을 만들었습니다.https://jsfiddle.net/hvb9hvog/14/

질문.

모달 작업을 했는데 어떻게 하면 각각의 요소를 보여줄 수 있을까요?value모달 내부 소유지?

여러 가지 방법이 있을 수 있지만, 한 가지 방법은 새로운 방법을 만드는 것입니다.data부동산이라고 부르자value.당신이@clickli아시겠죠?value, 로 설정합니다.value재산 및 표시value의 재산body모달의{{this.value}}).

두 개 주세요@clickmethods를 갱신하는 다른 method를 만듭니다.data방금 작성한 속성, 호출value.

여기 바이올린이 있다

관련 코드 변경:

고객님의 고객명li요소:

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

모달 마크업:

<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>

인뷰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
},

인뷰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>

데이터에 "req" 속성 추가

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

set click 이벤트:

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

본문 슬롯 추가

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

https://jsfiddle.net/w4e6hr86/

Vue.js를 사용하는지 JS만 사용하는지 잘 모르겠습니다.자, 여기 저의 답변(기본적인 예)이 있습니다.vuej의 이벤트 위임 + 이벤트에 대해 읽어보는 것이 좋습니다.

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>

플레인 자바스크립트

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. 이벤트 위임 정보.
  2. insertAdjacentHtml에 대해서
  3. Vuejs 이벤트 처리에 대해서

언급URL : https://stackoverflow.com/questions/47352788/how-to-show-an-elements-value-in-a-vue-modal

반응형