zoukankan      html  css  js  c++  java
  • 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>
    

    效果:

  • 相关阅读:
    POJ 2750 Potted Flower (单点改动求线段树上最大子序列和)
    [异能程序猿]第四章 偶遇(第四更)
    【web开发学习笔记】Struts-Tags学习笔记1
    UVa 10100
    【算法拾遗】大数相加(不开辟额外空间)
    cocos2d-html5学习笔记(六)--alpha2中cc.Sequence.create中的bug
    大一暑假和大二专业学习的规划
    Android如何获得系统版本
    Struts2——(1)Struts2入门
    设置m_pszAppName值的做法
  • 原文地址:https://www.cnblogs.com/haima/p/10236867.html
Copyright © 2011-2022 走看看