Issues with reactivity are present in certain items within Vue.js cards

Can someone please assist me with this issue that has been causing me trouble for days?

I am working on updating the quantity and total price in a checkout card:

The parent component:

<template>
    <div>
        <upsell-checkout-product
                :product="product"
                :index="index"
                @remote="remove"
                @increment="increment"
                @decrement="decrement"
        ></upsell-checkout-product>
    </div>
</template>

<script>
    export default {
        props: {
            runId: {
                type: String,
                default: null
            },
            card: {
                type: Array,
                default: null
            },

            customer: {
                type: Object,
                default: null
            },

            order: {
                type: Object,
                default: null
            }
        },

        data() {
            return {
                localCard: []
            }
        },

        mounted() {
            this.localCard = this.card;
        },

        methods: {
            increment(index) {
                Vue.set(this.localCard[index], 'quantity', this.localCard[index].quantity+=1);
            },

            decrement(index) {
                if(this.localCard[index].quantity > 0) {
                    this.localCard[index].quantity--;
                }
            },

            remove(index) {
                this.localCard.splice(index, 1);
            }
        }
    }
</script>

Child component:

   <template>
        <div class="text-green">
            <div class="flex center p-2">
                <div class="w-1/3">
                    <img class="rounded-full h-24 w-24 border border-green"
                         :src="'/storage/articles/' + product.article.image_url"
                         v-if="product.article.image_url"
                    />
                </div>

                <div class="w-2/3">
                    <h3>{{ product.article.name }}</h3>

                    <h5 class="mt-2">Bestaalstatus</h5>

                    <div>
                        <div class="bg-red text-white text-xs mt-2 rounded p-1 w-1/3">
                            Niet voldaan
                        </div>
                    </div>
                </div>
            </div>

            <div class="center justify-between p-2 border-t border-b border-dotted border-green">
                <div class="w-1/5">
                    <div class="cursor-pointer" @click="$emit('remove', index)">
                        x
                    </div>
                </div>

                <div class="center w-2/5">
                    <div class="cursor-pointer" @click="$emit('decrement', index)">
                        -
                    </div>

                    <input type="number"
                           class="input-main w-1/2 ml-1 mr-1 h-8"
                           v-model="product.quantity">

                    <div class="cursor-pointer" @click="$emit('increment', index)">
                        +
                    </div>
                </div>

                <div class="flex justify-end w-2/5">
                    <div v-if="product.article.member_discount_percentage">
                        <p class="text-sm">€ <strike>{{ price }}</strike> - € {{ discountPrice }}</p>
                    </div>

                    <div v-else>
                        <p class="text-sm">€ {{ discountPrice }}</p>
                    </div>
                </div>
            </div>
        </div>
    </template>

    <script>
        export default {
            props: ['product', 'index'],

            computed: {
                price: function() {
                    return (this.product.quantity * this.product.article.price).toFixed(2);
                },

                discountPrice: function() {
                    return (this.product.quantity * this.product.article.discount_price).toFixed(2);
                }
            }
        }
    </script>

I have encountered an issue where card items that have not been fetched from the server but added during the checkout process are not reactive within the localCard...

Although I attempted to create a JsFiddle, I was unable to replicate the problem. It appears to be related to reactivity, and I am unsure of how to resolve it.

https://i.stack.imgur.com/1J4p3.png

Your assistance would be greatly appreciated. Thank you!

Answer №1

Hooray! The problem is finally resolved!

After adding new items to the array and using this method:

.push(Object.assign({}, article));

It magically started working! Thank you for the assistance.

Answer №2

It appears that the issue does not lie within the code snippet you provided.

mounted() {
            this.localCard = this.card;
        },

I suspect that this.card may not be reactive for all items, causing the problem at hand. You can verify my theory by using a simple console.log(this.card) to check if all items in the cards array are Observers.

Hopefully this insight proves helpful.

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

Looking for a JavaScript function that can utilize AJAX to execute PHP code and display the PHP output on the webpage

I've been trying to use JavaScript to execute some PHP code and then display the results on the page. I came across a piece of code that achieves this using ajax and by placing the PHP code in an external file. However, the code I found seems to show ...

Passing a property to a click event handler in ES6 with React: What's the best approach?

Struggling with passing props to a click function in my React learning journey. I'm attempting to make a basic ES6 counter component that increases when a button is clicked. The click function I have so far is: click() { this.setState({ c ...

Tips for choosing an image using jQuery from an external HTML page

I'm attempting to embed an HTML page within a div and then individually select all the img tags within that page to display an overlay div on top of the images. Currently, I can insert the HTML into a div named "asd" and it seems like the jQuery is f ...

Why is dd() not functioning in my axios call within a vue-laravel project? Can I utilize dump instead in axios calls?

I am currently using Vue with Laravel, but I am experiencing issues with my save function not working as expected. In an attempt to troubleshoot, I tried dumping and dying the request object, however, I noticed that it appears blank in the preview. protec ...

What is the best way to access a custom object in JavaScript that was created in a different function?

Currently working with JavaScript and jQuery technology. In one of my functions that runs on document ready, I am creating objects with different attributes. While I can easily access these object attributes within the same function, I'm facing diff ...

Changing from system mode to dark mode or light mode

Within my Next.js web application, I am implementing MUI to facilitate the transition between system, light, and dark modes. Persistence between sessions is achieved by storing the selected theme in local storage. The user has the option to change the them ...

Adaptive Table Layout that Creates a Page-breaking Design

I have a page layout with three columns: a fixed-width left-hand navigation, a responsive content column, and a fixed-width right-hand navigation. The issue arises when the content in the middle column includes HTML tables that sometimes exceed the availab ...

Is there a way to extract information from an uploaded file in JavaScript without having to actually submit the file?

Looking for a way to extract data from a user uploaded file using Javascript without page submission? The goal is to process this data to provide additional options in a form on the same page. Any assistance with this would be highly appreciated. ...

Avoiding drag events in hammer.js until the previous event is finished

I've been working on a basic photo gallery that switches images during a drag event. However, I'm encountering an issue with the iOS7 browser where the drag event gets triggered multiple times when dragging left or right. I attempted to use a glo ...

jquery is in motion while svg paths are at a standstill, waiting to

i have been attempting to incorporate a css-animated svg into my project. initially, the animation would start automatically, which was undesirable. after some research, i discovered that by declaring it as paused and then triggering it using jQuery with $ ...

What is the best way to efficiently set up a scrolling text ticker for repeated use?

I am currently utilizing GreenSock/TweenMax for the creation of scrolling text, inspired by the design seen on this webpage: If you're interested in learning more about Greensock and its capabilities, take a look at their documentation here: While I ...

Join the Observable in Angular2 Newsletter for the latest updates and tips

One of my functions stores the previous URL address. prevId () { let name, id, lat, lng; this.router.events .filter(event => event instanceof NavigationEnd) .subscribe(e => { console.log('prev:', this.previo ...

What is the best way to set up prettier in VSCODE so that it organizes jsx attributes across multiple lines?

My preference is for the following syntax: <input id="inputForEmail" type="email" className="form-control" aria-describedby="Enter email address" placeholder="Enter emai ...

Using Highcharts to dynamically color a map based on data values in a React TypeScript project

I'm attempting to generate a map where each country is filled with colors based on its specific data, similar to the example shown in this map. I am looking for a functionality akin to the use of the formatter function within the tooltip. I have expe ...

"Despite the null date in Node.js, the validation for expiration dates less than Date.now() is still being enforced

I am currently working on implementing validation for evaluating the finish status. However, my validation is encountering a problem with the "null" value of expiresAt. It should indicate that the evaluation has been successfully completed. The issue lie ...

Adjust the menu scrollbar position to the right or limit scrolling to within the menu area

$(function() { "use strict"; $(".navbar-toggler").on("click", function() { $(".navbar-toggler").toggleClass("collapsed"); $(".offcanvas-collapse").toggleClass("open"); let menuposition = $("#toggler").offset().left + $("#toggler").width() + ...

Ways to determine if prototype methods vary

Is there a technique to verify if functions are distinct despite originating from the same prototype? I'm inquiring because I want to save functions in an array, and when attempting to delete one, it removes all functions due to sharing prototypes. ...

What is the process for extracting content from CSS comments or annotations in a stylesheet?

Here's an interesting concept: allowing users to define a set of CSS rules with annotations. For example: /* @name Page style */ body { font: 16px/1.5 Arial; /* @editable */ background-color: #fff; /* @editable */ } /* @name Section header */ ...

Issue with the functionality of Bootstrap 5 dismissable alert needs to be addressed

I encountered an issue with the alert in my html code: <div class="alert alert-success alert-dismissible fade show d-flex align-items-center justify-content-center" id="alert" style="display:none;"> <button type=& ...

"Utilizing OpenLayers to display markers coupled with interactive pop

I am struggling to implement a map with markers on my website. I want users to be able to click on these markers and view additional information, similar to how it works in Google Earth. Although I have the map and markers set up, I am having trouble getti ...