用vue实现简单分页
在这个demo中,我用vue对一个json文件中的数据进行了简单的分页,没用用到交互,一下是我的实现过程。
基础逻辑
1.将json文件引入app.vue,并作为data返回
data(){
var testData = require("../test.json");
return {testData}
},
2.将testData使用v-for呈递在页面上
<table>
<tr v-for="(item,index) in testData">
<td v-for="items in item">
{{items}}
</td>
</tr>
</table>
3.用v-if判断显示条数
<table>
<tr v-for="(item,index) in testData" v-if="index>=0&&index<10">
<td v-for="items in item">
{{items}}
</td>
</tr>
</table>
4.我考虑到vue双向数据绑定的特点,所以想到改变v-if中大于和小于两个值就可以实现分页了,所以我把这两个值改成了变量,通过点击上一页下一页改变变量的值。
<table>
<tr v-for="(item,index) in testData" v-if="index>=begin&&index<end">
<td v-for="items in item">
{{items}}
</td>
</tr>
</table>
<input type="button" value="上一页" @click="decrement">
<input type="button" value="下一页" @click="increment">
5.我开始直接把decrement和increment函数放在方法里面,结果发现根本改变不了begin和end的值。于是我用了vuex.
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
var state = {
front:0,
count:10
};//设置初始数字
const mutations = {
increment(state) {//处理数据变化的函数
state.front = state.front+10;
state.count=state.count+10;
},
decrement(state) {
state.front = state.front-10;
state.count=state.count-10;
}
};
const actions = {//处理你要干什么
increment:({commit})=> {
commit('increment');
},
decrement:({commit}) => {
commit('decrement');
},
clickAsync: ({commit}) => {
new Promise((resolve) => {
setTimeout(function(){
commit('increment');
resolve();
},1000);
})
}
}
const getters = {
begin(state) {
return state.front;
},
end(state) {
return state.count;
}
};
//需要导出Store对象
export default new Vuex.Store({
state,
mutations,
actions,
getters
});
main.js
import Vue from 'vue'
import App from './App.vue'
import store from './store'
new Vue({
store,
el: '#app',
render: h => h(App)
})
app.vue
<template>
<div id="app">
<input type="button" value="上一页" @click="decrement">
<button>第{{end/10}}页</button>
<input type="button" value="下一页" @click="increment">
<table>
<tr v-for="(item,index) in testData" v-if="index>=begin&&index<end">
<td v-for="items in item">
{{items}}
</td>
</tr>
</table>
</div>
</template>
<script>
import {mapGetters,mapActions} from 'vuex'
export default {
data(){
var testData = require("../test.json");
return {testData}
},
computed:mapGetters([//管理所有的事件
'begin',
'end'
]),
methods:mapActions([//获取数据
'increment',
'decrement',
'clickOdd',
'clickAsync'
])
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
这样,一个简单的分页就实现了
用vue实现简单分页的更多相关文章
- Vue下简单分页及搜索功能
最近利用Vue和element ui仿写了个小页面,记一哈分页和搜索功能的简单实现. 首页 emmmm..... 搜索框输入..... 搜索完成 数据是直接写在这里面的: cardPhoto:[ ...
- vue.js 2.0实现的简单分页
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- JavaScript简单分页,兼容IE6,~3KB
简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...
- MVC简单分页
对Car汽车表分页 实现简单分页,放在这里方便查看回顾,自定义每页几条有点问题,有待完善······ 1.新建mvc项目 2.添加linq to sql 数据库连接 3.添加CarBF类 using ...
- 使用Vs2005打造简单分页浏览器(1)原创
原文:使用Vs2005打造简单分页浏览器(1)原创 使用Vs2005打造简单分页浏览器(1)原创1引言2功能3实现过程以及关键点4总结5不足之处6其他7 代码下载 1 引言很早就有搞一个浏览器的 ...
- react构建淘票票webapp,及react与vue的简单比较。
前言 前段时间使用vue2.0构建了淘票票页面,并写了一篇相关文章vue2.0构建淘票票webapp,得到了很多童鞋的支持,因此这些天又使用react重构了下这个项目,目的无他,只为了学习和共同进步! ...
- vue.js之生命周期,防止闪烁,计算属性的使用,vue实例简单方法和循环重复数据
摘要:今天是比较糟糕的一天没怎么学习,原因是学校的wifi连不上了~~.今天学习一下vue的生命周期,如何防止闪烁(也就是用户看得到花括号),计算属性的使用,vue实例简单方法,以及当有重复数据时如何 ...
- Vue的简单入门
Vue的简单入门 一.什么是Vue? vue.js也一个渐进式JavaScript框架,可以独立完成前后端分离式web项目 渐进式:vue可以从小到控制页面中的一个变量后到页面中一块内容再到整个页面, ...
- MVC001之mvcpager简单分页
描述:用mvcpager实现简单分页功能 参考网址: http://www.cnblogs.com/iamlilinfeng/archive/2013/03/11/2951460.html http: ...
随机推荐
- c++的引用和c的指针之创建链表,二叉树的烦恼和区别
/* **代码功能:创建一个令人头疼的不算头疼的链表,然后把特定的数据删除. *这次的主题不是在代码上,主要是关于创建链表时候的传参问题,嘿嘿,不相信你没遇到过 */#include "st ...
- NSUserDefault
NSUserDefault是Cocoa提供的默认应用程序状态保持接口.它提供了简化的plist文件持久化方法.通过NSUserDefault类,你可以把用户首选项保存到plist文件中.到应用程序结束 ...
- Libevent 事件管理和添加事件
/** 我们先来看一下事件的创建*/struct event * event_new(struct event_base *base, evutil_socket_t fd, short even ...
- KVM克隆 快照
原创博文安装配置KVM http://www.cnblogs.com/elvi/p/7718574.htmlweb管理kvm http://www.cnblogs.com/elvi/p/7718582 ...
- 自己动手编写IOC框架(二)
万事开头难,上篇已经起了一个头,之后的事情相对就简单了.上次定义了框架所需的dtd也就是规定了xml中该怎么写,有哪些元素.并且我们也让dtd和xml绑定在了一起,使dtd对xml的格式进行校验,并且 ...
- zzuli 1812: sort 排序
1812: sort Time Limit: 1 Sec Memory Limit: 128 MB Submit: 352 Solved: 216 SubmitStatusWeb Board De ...
- centos7.0安装教程
CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linu ...
- Linux中nginx手动安装
本分类下有一个环境一键安装.那这背后发生了什么呢?咱们手动使用源码进行安装. 1.首先保证有一个能联网的centos. 2.百度 ningx 官网 点download http://nginx. ...
- UWP ListView下模板宽度问题
在ListView中模板的宽度默认不会根据listView的宽度改变为改变,这需要更改一下设置,重点在于设置HorizontalContentAlignment的拉伸模式.注意是HorizontalC ...
- idea从git上拉取并管理项目
1:idea从git上拉取项目 (1)FILE --> New --> Project from Version Control --> Git (2):输入项目的Https SSH ...