23.VUE學習之-列表的排序sort


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="hdcms">
    <li v-for="(v,k) in comments">
        {{v.id}} - {{v.content}}
        <button v-on:click="remove(k)">刪除</button>
    </li>
    <textarea v-model="current_content" cols="30" rows="10"></textarea><br>
    <button v-on:click="push('end')">發表到后面</button>
    <button v-on:click="push('pre')">發表到前面</button>
    <br>
    <button v-on:click="del('last')">刪除最后一條評論</button>
    <button v-on:click="del('first')">刪除第一條評論</button>
    <button v-on:click="del('all')">刪除全部評論</button>
    <br>
    <button v-on:click="sort('asc')">按照編號順序排序</button>
    <button v-on:click="sort('dsc')">按照編號倒序排序</button>
    <button v-on:click="reverse()">反轉順序</button>

</div>
<script>
    var app = new Vue({
        el: '#hdcms',
        data: {
            //當前用戶輸入內容
            current_content: '',
            comments: [
                {id: 2, content: 'HDPHP'},
                {id: 4, content: 'HDCMS'},
                {id: 1, content: '后盾人'},
                {id: 3, content: '向軍老師'},
            ]
        },
        methods: {
            sort(type){
                switch (type) {
                    case 'asc':
                        this.comments.sort(function (a, b) {
                            return a.id > b.id;
                        });
                        break;
                    case 'dsc':
                        this.comments.sort(function (a, b) {
                            return a.id < b.id;
                        });
                        break;
                }


            },
            reverse(){
                this.comments.reverse();
            },
            remove(k){
                this.comments.splice(k, 1);
            },
            push(type){
                var id = this.comments.length+1;
                var content = {id:id,content: this.current_content}
                switch (type) {
                    case 'end':
                        this.comments.push(content);
                        break;
                    case 'pre':
                        this.comments.unshift(content);
                        break;
                }
                this.current_content = '';
            },
            del(type){
                switch (type) {
                    case 'last':
                        this.comments.pop();
                        break;
                    case 'first':
                        this.comments.shift();
                        break;
                    case 'all':
                        this.comments=[];
                        break;
                }
            }

        }
    });
</script>
</body>
</html>

效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM