Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                    <div id="app" class="container my-3">
        <div class="input-group mb-3">
            <div class="input-group-prepend">
                <span class="input-group-text" id="basic-addon1">待辦事項</span>
            </div>
            <input type="text" class="form-control" placeholder="準備要做的任務" v-model="newTodo" @keyup.enter="addTodo">
            <div class="input-group-append">
                <button class="btn btn-primary" type="button" @click='addTodo'>新增</button>
            </div>
        </div>
        <div class="card text-center">
            <div class="card-header">
                <ul class="nav nav-tabs card-header-tabs">
                    <li class="nav-item">
                        <a class="nav-link" href="#" :class="{'active' : visibility == 'all'}"
                            @click='visibility="all"'>全部</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link " href="#" :class="{'active' : visibility == 'active'}"
                            @click='visibility="active"'>進行中</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" :class="{'active' : visibility == 'completed'}"
                            @click='visibility="completed"'>已完成</a>
                    </li>
                </ul>
            </div>
            <ul class="list-group list-group-flush text-left">
                <li class="list-group-item" v-for="(item, key) in filterTodos" @dblclick="editTodo(item)">
                    <div class="d-flex" v-if="item.id !==cacheTodo.id">
                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" v-model="item.completed" :id="item.id">
                            <label class="form-check-label" :for="item.id" :class="{'completed' :item.completed}">
                                {{item.title}}
                            </label>
                        </div>
                        <button type="button" class="close ml-auto" aria-label="Close" @click="removeTodo(item)">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <input type="text" class="form-control" v-if="item.id ===cacheTodo.id" v-model="cacheTitle"
                        @keyup.esc="cancelEdit()" @keyup.enter="doneEdit(item)">
                </li>
            </ul>
            <div class="card-footer d-flex justify-content-between">
                <span>還有 {{countTodos}} 筆任務未完成</span>
                <a href="#" @click.prevent="removeCompleted">清除已完成任務</a>
                <a href="#" @click.prevent="removeAll">清除所有任務</a>
            </div>
        </div>
    </div>
              
            
!

CSS

              
                .completed {
  text-decoration: line-through
}
              
            
!

JS

              
                
        var app = new Vue({
            el: '#app',
            data: {
                newTodo: '',
                todos: [
                    {
                        id: '1',
                        title: '完成todo練習',
                        completed: false,
                    }
                ],
                cacheTodo: {},
                cacheTitle: {},
                visibility: 'all',
            },
            methods: {
                addTodo: function () {
                    let value = this.newTodo.trim();
                    let timesId = Math.floor(Date.now());
                    if (!value) {
                        return;
                    }
                    this.todos.push({
                        id: timesId,
                        title: value,
                        completed: false,
                    });
                    this.newTodo = "";
                },
                removeTodo: function (thisTodo) { //刪去todo事項        
                    let newIndex = this.todos.findIndex(function (item, key) { //在todos陣列中,找尋id與現在點擊的id一樣的,傳出其陣列序號
                        return thisTodo.id === item.id;
                    })
                    this.todos.splice(newIndex, 1);  //在todos陣列中刪去該序號的值
                },
                editTodo: function (item) {
                    this.cacheTodo = item;
                    this.cacheTitle = item.title;
                },
                cancelEdit: function () {
                    this.cacheTodo = "";
                },
                doneEdit: function (item) {
                    item.title = this.cacheTitle;
                    this.cacheTitle = "";
                    this.cacheTodo = {};
                },
                removeAll: function () {
                    this.todos = [];
                },
                removeCompleted: function () {
                    this.todos = this.todos.filter(function(item){
                        return !item.completed;
                    })
                },
            },
            computed: {
                filterTodos: function () {
                    let newTodos = [];
                    switch (true) {
                        case (this.visibility == "all"):
                            return this.todos;
                            break;
                        case (this.visibility == "active"):
                            this.todos.forEach(function (item) {
                                if (!item.completed) {
                                    newTodos.push(item);
                                }
                            })
                            return newTodos;
                            break;
                        case (this.visibility == "completed"):
                            this.todos.forEach(function (item) {
                                if (item.completed) {
                                    newTodos.push(item);
                                }
                            })
                            return newTodos;
                            break;
                        default:
                    }
                },
                countTodos: function () {
                    let newTodos = []
                    this.todos.forEach(function (item) {
                        if (!item.completed) {
                            newTodos.push(item);
                        }
                    })
                    return newTodos.length;
                }
            },
        })

              
            
!
999px

Console