Home > OS >  VueJS: Displaying form input in list and modal
VueJS: Displaying form input in list and modal

Time:07-31

I am trying to:

  1. display form input (name, position, company) as an unordered list (v-for) which lists just the name inputted and a button for each person
  2. if click button, a modal appears which displays all the data inputted for that person (name, company, position) (**array of objects?)

Code sandbox: https://codesandbox.io/s/testing-2s8xvi?file=/src/components/EmployeeTable.vue

What logic looks like in vanilla JS (accessing data as array of objects, note use of .myParam to retrieve the object in array for a specific person): enter code herehttps://codepen.io/walrus2/pen/wvmoQOP

Would appreciate your help and suggestions, thank you in advance

CodePudding user response:

If I understood you correctly, You want to migrate that vanilla JS code into a Vue code. If Yes, Here you go :

Note : I did not add any validations in below demo, Please add as per your requirement.

new Vue({
  el: '#app',
  data: {
    formObj: {
        name: null,
      position: null,
      company: null
    },
    usersList: [],
    isShowDetails: false,
    modalContent: null
  },
  methods: {
    createList() {
      const obj = structuredClone(this.formObj);
        this.usersList.push(obj);
    },
    showDetails(id) {
        this.isShowDetails = true;
      this.modalContent = this.usersList.find((obj, index) => index === id);
    },
    closeModal() {
        this.isShowDetails = false;
    }
  }
})
/* The Modal (background) */
.modal {
  display: block; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

/* The Close Button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <label>name:</label><br/>
  <input type="text" id="name" name="name" v-model="formObj.name"/><br/><br/>

  <label>position:</label><br/>
  <input type="text" id="position" name="position" v-model="formObj.position"/><br/><br/>

  <label>company:</label><br />
  <input type="text" id="company" name="company" v-model="formObj.company"/><br/><br/>

  <button @click="createList()">Submit</button>

  <ul>
    <li v-for="(user, index) in usersList" :key="index">
      {{ user.name }}
      <button @click="showDetails(index)">Show Details</button>
    </li>
  </ul>

  <!-- The Modal -->
  <div id="myModal"  v-if="isShowDetails">

    <!-- Modal content -->
    <div >
      <span  @click="closeModal">&times;</span>
      <p>name: <span id="modalText1">{{ modalContent.name }}</span></p>
      <p>position: <span id="modalText2">{{ modalContent.position }}</span></p>
      <p>company: <span id="modalText3">{{ modalContent.company }}</span></p>
    </div>

  </div>

</div>

  • Related