Load more...
by ckissi
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://unpkg.com/vue"></script>
<div id="app">
<div v-for="article in sortedArticles">
<div> {{ article.title }} | {{ article.date }}</div>
</div>
<button v-if="articles.length > 4 && articlesShown < articles.length"
@click="loadMore">
Load more articles
</button>
</div>
</body>
</html>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
Vue
new Vue({
el: '#app',
data: {
articles: [
{title: '01', date: new Date('2015-03-25')},
{title: '02', date: new Date('2016-05-07')},
{title: '03', date: new Date('2015-07-25')},
{title: '04', date: new Date('2015-04-21')},
{title: '05', date: new Date('2014-03-25')},
{title: '06', date: new Date('2019-03-25')},
{title: '07', date: new Date('2012-03-25')},
{title: '08', date: new Date('2015-08-31')},
{title: '09', date: new Date('2018-03-25')},
{title: '10', date: new Date('2011-03-25')},
{title: '11', date: new Date('2013-03-25')}
],
articlesShown: 4
},
computed: {
sortedArticles () {
const sortedArticles = this.articles.slice(0,this.articlesShown).sort(this.compare)
return sortedArticles
}
},
methods: {
loadMore () {
this.articlesShown *= 2
},
compare(a, b) {
if (a.date > b.date) return -1
else if (a.date < b.date) return 1
else return 0
}
}
})