current position:Home>Vue - make todolist

Vue - make todolist

2022-01-27 01:26:53 Nanchen_ forty-two

The effect is as follows :

  Implementation code :

HTML part :

    <div id="todo" class="todo">
        <div class="header">
            <section>
                <!-- input part  -->
                <label for="title">ToDoList</label>
                <input type="text" name="title" placeholder=" Please enter your to-do list ........." required="required" autocomplete="off"
                    v-model="title" v-on:keypress.enter="addTodo">
            </section>
        </div>
        <section>
            <!--  The first half  -->
            <h2> The first half <span id="todoCount" class="count">{
   {firstList.length}}</span></h2>
            <ol id="todoList">
                <li v-for="(item,index) in firstList">
                    <input type="checkbox" v-on:click.prevent="checkedTodo(index)" v-model="item.done">
                    <p>{
   {item.title}}</p>
                    <a href="###" v-on:click="deleteABtn1(index)">-</a>
                </li>
            </ol>
            <!--  The second part of  -->
            <h2> The second part of <span id="doneCount" class="count">{
   {doneList.length}}</span></h2>
            <ol id="doneList">
                <li v-for="(item,index) in doneList">
                    <input type="checkbox" v-on:click.prevent="checkedDone(index)" v-model="item.done">
                    <p>{
   {item.title}}</p>
                    <a href="###" v-on:click="deleteABtn2(index)">-</a>
                </li>
            </ol>
        </section>
        <p class="footer">footer</p>
    </div>

CSS part :

* {
    margin: 0;
    padding: 0;
}

body {
    font-size: 16px;
    background: #cdcdcd;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

.header {
    height: 50px;
    background: rgba(47, 47, 47, 0.98);
}

section {
    max-width: 620px;
    margin: 0 auto;
    padding: 0 10px;
}

label {
    float: left;
    width: 100px;
    line-height: 50px;
    color: #ddd;
    font-size: 24px;
    cursor: pointer;
}

.header input {
    float: right;
    width: 60%;
    height: 24px;
    margin-top: 12px;
    text-indent: 10px;
    border-top-left-radius: 5px;
    border-radius: 5px;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.24), 0 1px 6px rgba(0, 0, 0, 0.45) inset;
    outline: none;
    border: none;
}
h2 {
    position: relative;
    margin: 26px 0;
}
h2 .count {
    position: absolute;
    top: 2px;
    right: 5px;
    display: inline-block;
    padding: 0 5px;
    height: 20px;
    border-radius: 20px;
    background: #e6e6fa;
    line-height: 22px;
    text-align: center;
    color: #666;
    font-size: 14px;
}

ol {
    margin: 28px 0 32px;
}

ol,
ul {
    list-style-type: none;
}

li {
    height: 32px;
    line-height: 32px;
    background: #fff;
    position: relative;
    margin-bottom: 10px;
    padding: 0 48px;
    border-radius: 3px;
    border-left: 5px solid #629a9c;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
}

li input {
    position: absolute;
    top: 6px;
    left: 22px;
    width: 22px;
    height: 22px;
    cursor: pointer;
}

li p {
    line-height: 34px;
}

li a {
    position: absolute;
    top: 4px;
    right: 5px;
    display: inline-block;
    width: 14px;
    height: 12px;
    border-radius: 14px;
    border: 6px double #fff;
    background: #ccc;
    line-height: 12px;
    text-align: center;
    color: #fff;
    font-weight: bold;
    font-size: 14px;
    cursor: pointer;
}

#doneList li {
    border-left: 5px solid #999;
    opacity: 0.5;
}

.footer {
    font-size: 14px;
    text-align: center;
    color: #666;
}

@media only screen and (max-width: 400px){
    .header input{
        width: 52%;
    }
}

script part :

 var todo = new Vue({
            el: "#todo",
            data: {
                title: '',
                //  Set up firstList Empty array 
                firstList: [],
                //  Set up doneList An empty array is used to store data locally 
                doneList: [],
            },
            //  Before loading :
            beforeMount() {
                //  load localstorage
                var storage = window.localStorage;
                //  If you get todo When not null 
                if (storage.getItem("todo") !== null) {
                    //  Parse from string json object , And get the data todo
                    this.firstList = JSON.parse(storage.getItem("todo"));
                }
                if (storage.getItem("done") !== null) {
                    this.doneList = JSON.parse(storage.getItem("done"));
                }
            },
            methods: {
                //  Trigger when clicked 
                addTodo() {
                    //  If input If the box is empty, a pop-up window will pop up 
                    if (this.title === '') {
                        return alert(' You have not entered the data you need to store !!');
                    }
                    //  If there's something, then   The upper part of span The new data obtained will be placed in the first position of the array 
                    this.firstList.unshift({
                        //  Give Way input Medium title Binding 
                        title: this.title,
                        //  hold done The button is set to off 
                        done: false
                    });
                    //  Remember to clear... After entering input Value in box 
                    this.title = '';
                    //  Store data locally 
                    this.setLocalStorage();
                },
                //  When you click the delete button in the first part a This event is triggered when 
                deleteABtn1(index) {
                    //  Delete the stored value 
                    this.firstList.splice(index, 1);
                    //  After deletion, store the rest locally 
                    this.setLocalStorage();
                },
                //  When you click the delete button in the second part a Trigger event 
                deleteABtn2(index) {
                    //  Click to delete the corresponding part 
                    this.doneList.splice(index, 1);
                    //  Store 
                    this.setLocalStorage();
                },
                //  Click... In the first part input Trigger event 
                checkedTodo(index) {
                    // span The index of the number in done for true
                    // firstList.children[0].checked = 'checked'
                    this.firstList[index].done = true;
                    //  hold firstList Put it in doneList Foremost 
                    this.doneList.unshift(this.firstList[index]);
                    //  And then the top half span The index of is also deleted 
                    this.firstList.splice(index, 1);
                    //  And store it 
                    this.setLocalStorage();
                },
                //  After the second half appears input box ( That is, in the checked box )   Set the click event 
                //  The main purpose is to change the check box to closed state after clicking , And put it in the top half , Delete your location 
                checkedDone(index) {
                    //  The lower part of span Index of numbers done Value is set to flase
                    this.doneList[index].done = false;
                    //  Put the lower half at the front of the upper half 
                    this.firstList.unshift(this.doneList[index]);
                    //  The lower part of span Click to delete the current value 
                    this.doneList.splice(index, 1);
                    this.setLocalStorage();
                },
                setLocalStorage() {
                    //  Storage localstorage
                    var storage = window.localStorage;
                    //  take  JavaScript  Value to  JSON  character string 
                    storage.setItem("todo", JSON.stringify(this.firstList));
                    storage.setItem("done", JSON.stringify(this.doneList));
                }
            }
        });

copyright notice
author[Nanchen_ forty-two],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270126513661.html

Random recommended