The dynamics of local storage are revolutionizing the way each entry in the task list is

The local storage should store the state only for the task where the completed button has been pressed. At present, when I refresh the page, all tasks are shown as completed.

<template>
<button type="button" v-bind:class="order_button_style" @click="on_order_button_click()">
  {{ buttonText }}
</button>
</div>
</template>

<script>
export default {

props: 'itemId', required: true,
data() {
    return {
        index: 'this.itemId',
        status: ''
    }
},
methods: {
on_order_button_click() {
  this.status = !this.status;
  localStorage.setItem('index', this.status);
}

},
mounted() {

this.status = localStorage.getItem('index') === "true";
},
computed: {
buttonText() {
  return this.status === true ? "Completed" : "Complete";
},
 order_button_style() {
  return this.status === true
    ? "btn btn-danger"
    : "btn btn-primary";
}

}

};
</script>

Answer №1

When updating your local storage values, make sure you are using the correct key to store the data specific to each component instance. Currently, all components are reading from the same local storage key 'index'.

For example, make this adjustment:

mounted() {
    this.status = localStorage.getItem(this.index) === "true";
},

It seems that setting a data property equal to a passed property might not work as intended. Double-check to ensure that you are correctly accessing and assigning the value of `this.itemId`.

I have made some modifications to your component in an attempt to address these issues. Please test it out and let me know if it resolves the problems:

<template>
    <button type="button" v-bind:class="order_button_style" @click="on_order_button_click()">
        {{ buttonText }}
    </button>
</template>

<script>
export default {
    props: {
        itemId: {
            type: String,
            required: true,
        }
    },
    data() {
        return {
            status: false,
        }
    },
    methods: {
        on_order_button_click() {
            this.status = !this.status;
            localStorage.setItem(this.itemId, this.status);
        }
    },
    mounted() {
        this.status = localStorage.getItem(this.itemId) === "true";
    },
    computed: {
        buttonText() {
            return this.status === true ? "Completed" : "Complete";
        },
        order_button_style() {
            return this.status === true ?
                "btn btn-danger" :
                "btn btn-primary";
        }

    }

};
</script>

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

A step-by-step guide on uploading a file with Laravel Vue API

I am currently working on creating a form that allows file uploads. I have successfully implemented storing string and integer data, but I am facing challenges with handling files in the controller and view. Below is the template for adding categories: &l ...

When the mouse is clicked, rotate the object along its axis using OBJ Loader in THREE.js

I am looking to add a feature where my object rotates on its axis when the mouse is dragged. The challenge I am facing is that I can only access my skull object within the function, which limits where I can place a rotation increment inside render(). Coul ...

Exploring a POST request using jQuery

Apologies if I struggle with my question or use incorrect terminology, as I am new to this. I have a basic web service that processes a JSON string and returns a JSON result. Here is the code: $jsonStr = ''; if(isset($_POST['request'] ...

Are the JQuery appended elements exceeding the width of the parent div?

I have an HTML div that is styled using the Bootstrap class span9. <div class="span9"> <div id = "selectedtags" class="well"> </div> <button class="btn" type="button" id="delegatecontent">Generate report</button&g ...

Instructions for sorting an array of objects by Firebase Timestamp

I am looking for a way to order my messages based on timestamp in Firebase v9. In earlier versions, I was able to do this but now I seem to be facing some difficulties. Here is the data structure set up on Firestore: const [messages, setMessages] = useSta ...

Leveraging JQuery in Master Pages

I am attempting to integrate JQuery into my Master Page. Despite not receiving any errors, the JQuery does not seem to be functioning properly. Specifically, I am trying to implement the Table Sorter function. MasterFile: <head runat="server"> ...

Combining frontend and backend functionality

It is important to note that the front end should be independent of the backend, meaning a REST API is a REST API regardless. In discussions with various developers for an upcoming project, there seems to be a consensus that Vue works best with Laravel on ...

Properly setting up event handling for a file input and Material UI Button

In my attempt to create a customized form using material UI elements, I am facing an issue. The form allows users to upload files and add notes for each option, which are stored in an array in the component's state. Here is a simplified version of th ...

Maximizing Efficiency in Combining Two Objects With Arrays of Objects

I am in need of optimizing a code path that will be heavily used for merging two objects. The current code is functional, but I believe it lacks optimization for speed. Any suggestions on how to enhance or replace the existing solution would be greatly app ...

The EJS view fails to render when called using the fetch API

Within my client-side JavaScript, I have implemented the following function which is executed upon an onclick event: function submitForm(event) { const data = { name, image_url }; console.log(data); fetch('/', { method: &apo ...

JavaScript: A step-by-step guide to extracting the file name and content from a base64 encoded CSV

I have a base64 string that was generated by encoding a csv file, const base64 = 'LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTExNDc2MDgwNjM5MTM4ODk4MTc2NTYwNA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlIjsgZmlsZW5hbWU9ImNoYXJ0T2ZBY2NvdW50LmNzd ...

Is there a way to create Angular components sourced from an external library using JavaScript?

I'm currently developing an Angular project and leveraging an external component library called NGPrime. This library provides me with a variety of pre-built components, including <p-button> (which I am using in place of a standard <button> ...

utilizing Typescript object within an array of objects

How can I optimize typing this nested array of objects? const myItem: Items[] = [{ id: 1, text: 'hello', items: [{ id: 1, text: 'world' }] }] One way to approach this is by using interfaces: interface It ...

Tips for safely using Buefy's Dialog component with custom user input to prevent cross-site scripting attacks

When using Buefy's Dialog component, it requires a prop called message, which should be a string and can potentially include HTML. I want to incorporate template values into the string securely to prevent XSS vulnerabilities. Unsafe Example Currently ...

Displaying the revised v-model value in the input field is not possible at the

Here is a code snippet that I am working with: const BasicInput = { template: '<input v-model="content" @input="handleInput" />', prop: ["value"], data() { return { content: this.value }; }, methods: { handleI ...

As for the Pixel Art Creator feature, you can now switch between different modes and

I'm seeking assistance regarding my Pixel Art Maker project. I recently implemented two new features, toggle() and remove(). The issue I'm facing is that when I input numbers to create the grid and click the submit button, the grid is successfull ...

The Laravel classloader.php encountered an error while trying to open the stream, as the file or directory does not exist

Everything seems to be running smoothly when I execute "php artisan migrate" and retrieve all the form input using Request::all(). However, when attempting to insert this data into my MySQL database table, I encounter the following error: ErrorException i ...

The beautiful synergy between Vuetify's v-input component and overflow animation

I am having trouble implementing an overflow animation on vuetify's v-text-field component. Despite my efforts, I can't seem to make it work as intended: <script setup> const text = 'very long long long long long text' </scri ...

Retrieve the outermost shell of the VUEjs wrapper test-utils

While working on a VueJS test, I encountered an issue where accessing the outermost layer of HTML seemed impossible. No matter what methods I tried, the outermost layer was always ignored. Is there a way to gain access to this outermost layer so that I c ...

I struggle with parsing JSON data in JavaScript

After reviewing the post, I am still unclear about what is going on. Although my variable "text" seems to be valid based on most JSON online data checkers, when I attempt to parse it, nothing happens. Here's a snippet of example code: <!DOCTYPE ...