简体   繁体   English

如何在单击事件时从 vue.js 实例中的 for 循环中提取特定数据

[英]How do you extract specific data from a for loop in vue.js instance on click event

I have a notepad app, where you click on the note and you see the details of that note, I have a for loop for the notes and I need to know how I can extract the data from the notes array and show the specific note details pertaining to that note.我有一个记事本应用程序,您可以在其中单击笔记并看到该笔记的详细信息,我有一个用于笔记的 for 循环,我需要知道如何从笔记数组中提取数据并显示特定的笔记详细信息与该注释有关。 For example, I want to click on note1 and see the details for that note only, when I click on the second note, same idea.例如,我想单击 note1 并仅查看该注释的详细信息,当我单击第二个注释时,同样的想法。

I am seeing all the note data on the click event.我看到了点击事件的所有笔记数据。 How do I extract the data from the vue.js instance to show the specific data?如何从 vue.js 实例中提取数据以显示具体数据? How do I click on the actual note, not the button(for testing only) to see this data?如何单击实际注释,而不是按钮(仅用于测试)来查看此数据?

Thank you in advance:)先感谢您:)

Here is the code I have so far:这是我到目前为止的代码:

App应用程序

<template>
  <div class="body">
    <div class="notepad-container h-75 w-75">
      <header class="header d-flex justify-content-center align-items-center">
        <h4>Light Notepad v1</h4>
      </header>

      <section class="notepad-content" v-if="editorIsOpen === false">
        <note-list
          v-for="note in notes"
          :key="note.id"
          :note="note"
        ></note-list>
        <add-note-button @open-editor="openNewEditor"></add-note-button>

        <button class="readNoteButton" @click="readNote">view note one</button>
         <button class="readNoteButton2" @click="readNote">view note two</button>

      </section>

      <section class="notepad-editor" v-if="editorIsOpen === true">
        <save-button></save-button>
      </section>

      <section v-if="readingNote === true" class="">
        <show-note
          @open-note="openNote"
          v-for="(note) in notes"
          :key="note.id"
          :note="note"
        ></show-note>
      </section>

    </div>
  </div>
</template>

<script>
import AddNoteButton from "./components/AddNoteButton.vue";
import NoteList from "./components/NoteList.vue";
import SaveButton from "./components/SaveButton.vue";
import ShowNote from "./components/ShowNote.vue";

export default {
  components: {
    NoteList,
    AddNoteButton,
    SaveButton,
    ShowNote,
  },
  data() {
    return {
      editorIsOpen: false,
      readingNote: false,
      openNote: false,
      props: {
        note: {
          required: true,
        },
      },
      notes: [
        {
          id: 1,
          title: "1st Note",
          body: "This is a note",
          date: "10/17/20",
        },
        {
          id: 2,
          title: "2nd Note",
          body: "This is a note",
          date: "11/17/20",
        },
      ],
    };
  },
  methods: {
    openNewEditor() {
      this.editorIsOpen = !this.editorIsOpen;
    },
    readNote() {
      this.readingNote = !this.readingNote;
      document.querySelector('.readNoteButton').innerHTML="close note"
      document.querySelector('.readNoteButton2').innerHTML="close note"
      // alert('you clicked me!');
    },
  },
};
</script>

NoteList笔记列表

<template>
<div>
    <b-list-group>
      <b-list-group-item button @click="openNote" 
        >{{ note.title }} - {{ note.date }}</b-list-group-item
      >
    </b-list-group>
    </div>
</template>
<script>
export default {
  name: 'noteList',
  emits: ['open-note'],
  props: {
    note: {
      required: true,
    },
  },
  methods: {
    openNote() {
      this.$emit('open-note');
      console.log("clicked from NoteList");

    },
  },
};
</script> 

ShowNote显示笔记

<template>
  <div>
    <p>
   note details: 
    Note ID: {{ note.id }}, Date: {{ note.date }},
    Title: {{ note.title }}, Body: {{ note.body }}
    </p>
  </div>
</template>

<script>
export default {
  name: 'showNote',
  props: {
    note: {
      required: true,
    }
  },
  
};
</script>

I am giving some modifications need to work我正在做一些修改需要工作

Pass note with the event.通过事件传递注释。

openNote() {
      this.$emit('open-note', this.note);
      console.log("clicked from NoteList");

 },

listen event at parent在父母处收听事件


 <note-list
          v-for="note in notes"
          :key="note.id"
          :note="note"
          @open-note="readNote" >
 </note-list>

declare a property in data object called currentNode: {}在名为currentNode: {}


 data() {
    return {
      editorIsOpen: false,
      readingNote: false,
      currentNode: {} // +====> add new property
      ....
}
}



modify readNote method修改readNote方法

readNote(note) {
      this.currentNote = note;
      this.readingNote = !this.readingNote;
      document.querySelector('.readNoteButton').innerHTML="close note"
      document.querySelector('.readNoteButton2').innerHTML="close note"
      // alert('you clicked me!');
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM