zoukankan      html  css  js  c++  java
  • Vue的动态表格+分页

    实现动态输出列名和数据并且分页

    一、创建表格和分页的模板

      

    <template id="tabletest">
                <div class="container">
                    <table class="table table-hover">
                    <tr>
                        <td>num</td>
                        <td v-for="(value,key) in persons[0]">{{key}}</td>
                    </tr>
                    <tr v-for="(p,index) in pagelist">
                        <td>{{index+1}}</td>
                        <td v-for="value in p">{{value}}</td>
                    </tr>
                    </table>
                    <!-- 父组件监听子组件的事件 -->
                    <page @prev="changPage" @next="changPage" :length="persons.length"></page>
                </div>
            </template>
            <template id="page">
                <div>
                    <button type="button" class="btn btn-primary" @click="prev">上一页</button>
                    <button type="button" class="btn btn-primary" @click="next">下一页</button>
                </div>
            </template>

    二、编写表格和分页的逻辑关系

      

    <script type="text/javascript">
            //记住要写在myTable之前
            var page={
                template:"#page",
                data:function(){
                    return {
                        pageNum:1
                    }
                },
                props:{
                    //数据长度
                    length:{
                        type:Number,
                        required:true
                    },
                    pageSize:{
                        type:Number,
                        default:3
                    }
                        
                    
                },
                methods:{
                    prev:function(){
                        if(this.pageNum>1)
                            this.pageNum--;
                        //给父组件发射数据
                        this.$emit("prev",this.pageNum);
                    },
                    next:function(){
                        var sumPage=parseInt(((this.length-1)/this.pageSize+1));
                        if(this.pageNum<sumPage)
                            {
                                this.pageNum++;
                            }
                        //给父组件发射数据
                        this.$emit("next",this.pageNum);
                    }
                }
            }
            var myTable={
                template:"#tabletest",
                props:{
                    persons:{
                        type:Array,
                        required:true
                    },
                    pageNum:{
                        type:Number,
                        default:1
                    },
                    pageSize:{
                        type:Number,
                        default:3
                    }
                },
                data:function(){
                    return {
                        //由于子组件不能直接更改父组件的值,所有用一个替换
                        pn:this.pageNum
                    }
                },
                methods:{
                    changPage:function(pageNum){
                        this.pn=pageNum;
                        console.log("page组件点击了页码数为:"+pageNum);
                    }
                },
                components:{
                    //注册子组件
                    page
                },
                //钩子函数
                computed:{
                    pagelist:function(){
                        var start=(this.pn-1)*this.pageSize;
                        var end=this.pn*this.pageSize;
                        return this.persons.slice(start,end);
                    }
                }
                
                
            };
            
            var vue = new Vue({
                el: "#app",
                data: {
                    persons: [{"name": "张三1","age": 19}, 
                              {"name": "张三2","age": 20}, 
                              {"name": "张三3","age": 21},
                              {"name": "张三4","age": 21},
                              {"name": "张三5","age": 21},
                              {"name": "张三6","age": 21},
                              {"name": "张三7","age": 21}
                              ],
                    person2:[{"name":"诸葛亮","age":1900,"birthday":"181年","sex":""},
                             {"name":"貂蝉","age":1900,"birthday":"190年","sex":""},
                             {"name":"刘备","age":1920,"birthday":"161年","sex":""},
                             {"name":"曹操","age":1930,"birthday":"161年","sex":""},
                             {"name":"张飞","age":1930,"birthday":"161年","sex":""},
                             {"name":"关羽","age":1930,"birthday":"161年","sex":""},
                             {"name":"张辽","age":1930,"birthday":"161年","sex":""},
                             {"name":"马超","age":1930,"birthday":"161年","sex":""},
                             {"name":"黄忠","age":1930,"birthday":"161年","sex":""},
                             {"name":"孙权","age":1930,"birthday":"161年","sex":""}
                    ]
                },
                components:{
                    //注册
                    myTable,page
                },
                methods:{
                    
                }
            });
            
            
    </script>

    三、测试

      注:我使用了bootstrap框架,和我的显示效果会不一样

    <div id="app">
                <my-table :persons="persons"></my-table>
                <my-table :persons="person2"></my-table>
    </div>

     

  • 相关阅读:
    《深度学习推荐系统中各类流行的Embedding方法(上)》
    《替代梯度下降——基于极大值原理的深度学习训练算法》&《加速对抗训练——YOPO算法浅析》
    《【炼丹技巧】功守道:NLP中的对抗训练 + PyTorch实现》
    《论文阅读:对抗训练(adversarial training)》
    《一文搞懂NLP中的对抗训练》
    CLOUD配置审批流发消息
    CLOUD财务成本核算
    K3CLOUDJOBPROCESS每分钟重启
    查看服务器日志
    数据规则列表加导入导出
  • 原文地址:https://www.cnblogs.com/dilireba/p/13111974.html
Copyright © 2011-2022 走看看