Can you provide guidance on extracting the ID from the notes[] array within Getnote.vue and then using that ID to update a specific card?

On my dashboard, I have several cards that display titles and descriptions retrieved from the backend using axios.js. While the display section is working fine, I need to implement a feature where clicking on a card opens a popup for updating that specific card. The purpose of this popup is to update the content based on the card ID (which is stored inside the notes[] array in Getnote.vue). My question is, how can I obtain the ID of the selected card when clicked by the user and establish a connection with the backend API to update that particular note? Your assistance in resolving this issue would be greatly appreciated.

Updatenote.vue

User.js

axios.js

Getnotes.vue

Answer №1

To enhance the functionality of the GetNotes.vue component, you can introduce a new property within the data section to keep track of the currently selected card by its ID. This ID can then be passed on to the UpdateNote component for further processing.

GetNotes.vue

<template>
<div class="note-cards-container">
    <div class="note-card" v-for="card in cards" :key="card.id">
        <div class="note-content" @click="handleCardClick(card.id)">
            <h3>{{card.title}}</h3>
            <p>{{card.text}}</p>
        </div>
        <div class="card-options">
            <ActionIcon />
            <button class="close-button" type="button" v-if="isPopupVisible" @click="submitChanges();togglePopup();">Close</button>
        </div>
    </div>
    <div class="update-popup" id="popup">
        <Updatenote :cardId="selectedCard"/>
    </div>
</div>
</template>

<script>
import service from '../../services/Service'
import ActionIcon from '../../components/shared/ActionIcon.vue'
import Updatenote from '../../components/modals/Updatenote.vue'

export default {
    name: 'GetNotes',
    components: {
        ActionIcon,
        Updatenote
    },
    data() {
        return {
            isPopupVisible: false,
            cards: [{
                id: 1,
                title: 'Sample Note',
                text: 'This is a sample note.'
            }],
            selectedCard: '',
        }
    },
    methods: {
        togglePopup() {
            this.isPopupVisible = !this.isPopupVisible;
        },
        async submitChanges() {
            service.updateNote().then(response => {
                this.notes.push(...response.data);
            })
        },
        handleCardClick(id) {
            var popup = document.getElementById('popup');
            popup.classList.toggle('active');
            this.selectedCard = id;
        }
    }
}
</script>

<style lang="scss" scoped>
@import "@/styles/GetNotes.scss";
</style>

In the Updatenote.vue component, utilize the provided ID to trigger an API call related to that specific card. The cardId attribute will hold the ID of the currently clicked card for reference.

Updatenote.vue

<template>
<div class="update-note-container">
    <form class="update-form" @submit.prevent="handleSubmit" id="update-id">
        <input name="title" id="name-input" v-model="title" placeholder="Title" autocomplete="off" />
        <textarea name="content" id="text-input" v-model="description" placeholder="Enter your note..." autocomplete="off"></textarea>
        <ActionIcon />
        <button @click="updateNote()" type="submit">Close</button>
    </form>
</div>
</template>

<script>
import service from '../../services/Service'
import ActionIcon from '../../components/shared/ActionIcon.vue'

export default {
    name: 'Updatenote',
    components: {
        ActionIcon
    },
    props: ['cardId'],
    data() {
        return {
            title: '',
            description: ''
        }
    },
    methods: {
        updateNote: function() {
            const updatedData = {
                id: this.cardId,
                title: this.title,
                description: this.description
            };
            service.updateNote(updatedData).then(response => {
                console.log("Update successful", response);
                localStorage.setItem('token', response.data.token);
                this.notes.push(...response.data);
            })
        },
    }
}
</script>

<style scoped>
...........
</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Effortlessly rearrange and remove specific elements using jQuery UI's drag-and-drop

Hello everyone, I have designed a simple interface with 3 different "zones". The first zone contains a list of elements that the user possesses, the second zone allows the user to drag and sort these elements, and the third zone enables the user to delete ...

How can I create a click event in vuejs for a custom component?

In my main component, I have a custom component and I want to trigger a custom event when it is clicked. I attempted to do this in the following way: <child-component @click="fireCustomEvent"></child-component> Unfortunately, this method did ...

Shifting an HTML anchor to account for a stationary header - Safari compatibility with alternative CSS styling

I made adjustments to my HTML anchors by adding an offset in the CSS to accommodate for a fixed header in my theme: margin-top: -175px; padding-top: 175px; } /* Adjustments for screen sizes of 760px and smaller */ @media (max-width:760px){ #r ...

What is the correct way to utilize href in CSS content property?

Below is the content of a box I have: https://i.sstatic.net/QmG5i.png When the above box is hovered over, it flips to this: https://i.sstatic.net/QsS9t.png This block's HTML code is as follows: <div class='tiles'> <div class= ...

Personalizing Twitter Bootstrap Directly from the Bootstrap Source Directory

As I work on developing a responsive web application using Twitter Bootstrap, my goal is to customize the Bootstrap framework to fit my specific needs. To do so, I have downloaded the Bootstrap source files from getbootstrap.com. My plan is to customize B ...

Step-by-step guide for setting up chartjs on Laravel 6 using npm

Can anyone guide me on how to properly install and integrate [email protected] in my laravel application? Currently, I am using cdn links, but I want to avoid that in the future. List of cdn links being used: <script src="https://cdnjs.c ...

issue: [astro preview] adapter lacks preview entry point

Recently, I encountered an issue while working on a website using astro (). When running astro preview, I encountered an error. Here is the code from my astro.config.mjs file: import { defineConfig } from 'astro/config'; import vercel from ' ...

Is it possible to leverage the dimensions (width and height) of an element in JavaScript to obtain a zoom scale?

Currently experiencing an issue with the zoom level in d3.js I obtained the dimensions for the <g className ="_zoomElement"> element using this function const g = select("._zoomElement") console.log(g.node().getBBox()) My ...

Customize Nuxt default document using a custom app.html file

According to the guidelines provided by Nuxt documentation, you have the ability to customize the default document by... placing an app.html file in the source directory of your project, which is typically the root directory. I followed these instructio ...

What is the best way to reduce the size of my JavaScript files within my framework?

I have developed a custom PHP framework from scratch. The framework includes views, controllers, and models. Within the views, there are variables that can be set accordingly. $js = array('custom/testimonial.js','jquery.js'); Within ...

Raspberry Pi Fast Server: Issue with AJAX not locating images in directory and malfunctioning ID search

I'm completely new to Raspberry Pi, linux, and server concepts, so I'd appreciate explanations at a beginner level. My goal is to create a display similar to those you see in lobbies showing corporate slides and weather updates. While setting up ...

Analyzing and adding Angular JSON information

I'm struggling to understand why the compare function and insert function in my code aren't functioning correctly. I suspect that one reason for this could be that the function is not being called by the button. How can I confirm this? It seems l ...

The click event does not trigger in a Vue.js component when using v-on

I am utilizing VueJs to develop the following component. var ComponentTest = { props: ['list', 'symbole'], data: function(){ return { regexSymbole: new RegExp(this.symbole), } }, template: ` ...

How to instantly return progress bar to zero in bootstrap without any animations

I am currently working on a task that involves multiple actions, and I have implemented a bootstrap progress bar to visually represent the progress of each action. However, after completion of an action, the progress bar is reset to zero using the followi ...

Rearrange Firebase data on the client side

Having trouble sorting data in Firebase for a web project. I tried using orderByChild to filter the data, but now I need to also order it by date. Here's what I've attempted: Tried using object.values with .sort, no luck Attempted to use lodas ...

React form not detecting the Enter key press in onSubmit event

Currently, I am in the process of working on a form that includes a tag input feature. The issue I am facing is that when a user enters a tag and presses enter, it not only adds the tag to a specific array but also submits the form. While I can use the e.p ...

Get rid of any empty space in the image preview icon

Is there a way to eliminate the white space that appears when mixing landscape and portrait images? I want the images to move up and fill the space, even if they don't align perfectly. Additionally, I would like the images to resize based on the scal ...

how to uncheck a checkbox using Vue.js

When the next button is clicked, I want to remove the checked status of the checkbox without using Vue. Here is a demo code snippet: new Vue({ el: "#app", data: { id:0, items: [ {'id':1,'name':'chk-a',&apos ...

JavaScript Age Calculator - Counting Days

Hey there! I've got an interesting problem. I currently have three text boxes on my webpage, and what I want to achieve is having a fourth text box generated when the user clicks a button. The content of this new text box should be filled with the dat ...

How can I stretch a background image using jquery to cover the entire document instead of just the window

I have implemented a plugin called https://github.com/srobbin/jquery-backstretch to effectively stretch the background image. The problem arises when the content of the page is long enough to create a scrollbar. In this case, while scrolling, the backgrou ...