• 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>

     

  • 相关阅读:
    zz:android sharedpreferences用法
    Android viewHolder
    Python学习笔记~
    zz:Java多线程编程总结
    zz:Android 2.1 源码结构
    onSaveInstanceState的用法
    zz:如何学习Linux操作系统
    Python学习笔记II
    Python类型转换
    zz:Android应用程序基础
  • 原文地址:https://www.cnblogs.com/dilireba/p/13111974.html
Copyright © 2020-2023  润新知