Day12: 編寫日誌列表頁
MVVM模式不但可用於Form表單,在複雜的管理頁面中也能大顯身手。因此,在實現分頁顯示Blog的功能時候,依舊採用MVVM模式,先把後端代碼寫出來:
在apis.py中定義一個Page類用於儲存分頁資訊:
class Page(object): def __init__(self, item_count, page_index=1, page_size=10): self.item_count = item_count self.page_size = page_size self.page_count = item_count // page_size + (1 if item_count % page_size > 0 else 0) if (item_count == 0) or (page_index > self.page_count): self.offset = 0 self.limit = 0 self.page_index = 1 else: self.page_index = page_index self.offset = self.page_size * (page_index - 1) self.limit = self.page_size self.has_next = self.page_index < self.page_count self.has_previous = self.page_index > 1 def __str__(self): return 'item_count: %s, page_count: %s, page_index: %s, page_size: %s, offset: %s, limit: %s' % (self.item_count, self.page_count, self.page_index, self.page_size, self.offset, self.limit) __repr__ = __str__
Day 12 - 編寫日誌列表頁
閱讀: 3940
MVVM模式不但可用於Form表單,在複雜的管理頁面中也能大顯身手。例如,分頁顯示Blog的功能,我們先把後端代碼寫出來:
在apis.py中定義一個Page類用於儲存分頁資訊:
class Page(object):
def __init__(self, item_count, page_index=1, page_size=10): self.item_count = item_count self.page_size = page_size self.page_count = item_count // page_size + (1 if item_count % page_size > 0 else 0) if (item_count == 0) or (page_index > self.page_count): self.offset = 0 self.limit = 0 self.page_index = 1 else: self.page_index = page_index self.offset = self.page_size * (page_index - 1) self.limit = self.page_size self.has_next = self.page_index < self.page_count self.has_previous = self.page_index > 1def __str__(self): return 'item_count: %s, page_count: %s, page_index: %s, page_size: %s, offset: %s, limit: %s' % (self.item_count, self.page_count, self.page_index, self.page_size, self.offset, self.limit)__repr__ = __str__
在handlers.py中實現API:
def get_page_index(page_str): p = 1 try: p = int(page_str) except ValueError as e: pass if p < 1: p = 1 return p@get('/api/blogs')def api_blogs(*, page='1'): page_index = get_page_index(page) num = yield from Blog.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, blogs=()) blogs = yield from Blog.findAll(orderBy='created_at desc', limit=(p.offset, p.limit)) return dict(page=p, blogs=blogs)
管理頁面:
@get('/manage/blogs')def manage_blogs(*, page='1'): return { '__template__': 'manage_blogs.html', 'page_index': get_page_index(page) }
Vue模式下的MVVM頁面代碼:
manage_blogs.html:
{% extends '__base__.html' %}{% block title %}日誌{% endblock %}{% block beforehead %}<script>function initVM(data) { var vm = new Vue({ el: '#vm', data: { blogs: data.blogs, page: data.page }, methods: { edit_blog: function (blog) { location.assign('/manage/blogs/edit?id=' + blog.id); }, delete_blog: function (blog) { if (confirm('確認要刪除“' + blog.name + '”。刪除後不可恢複。')) { postJSON('/api/blogs/' + blog.id + '/delete', function (err, r) { if (err) { return alert(err.message || err.error || err); } refresh(); }); } } } }); $('#vm').show();}$(function() { getJSON('/api/blogs', { page: {{ page_index }} }, function (err, results) { if (err) { return fatal(err); } $('#loading').hide(); initVM(results); });});</script>{% endblock %}{% block content %} <div class="uk-width-1-1 uk-margin-bottom"> <div class="uk-panel uk-panel-box"> <ul class="uk-breadcrumb"> <li><a href="/manage/comments">評論</a></li> <li class="uk-active"><span>日誌</span></li> <li><a href="/manage/users">使用者</a></li> </ul> </div> </div> <div id="error" class="uk-width-1-1"> </div> <div id="loading" class="uk-width-1-1 uk-text-center"> <span><i class="uk-icon-spinner uk-icon-medium uk-icon-spin"></i> 正在載入...</span> </div> <div id="vm" class="uk-width-1-1"> <a href="/manage/blogs/create" class="uk-button uk-button-primary"><i class="uk-icon-plus"></i> 新日誌</a> <table class="uk-table uk-table-hover"> <thead> <tr> <th class="uk-width-5-10">標題 / 摘要</th> <th class="uk-width-2-10">作者</th> <th class="uk-width-2-10">建立時間</th> <th class="uk-width-1-10">操作</th> </tr> </thead> <tbody> <tr v-repeat="blog: blogs" > <td> <a target="_blank" v-attr="href: '/blog/'+blog.id" v-text="blog.name"></a> </td> <td> <a target="_blank" v-attr="href: '/user/'+blog.user_id" v-text="blog.user_name"></a> </td> <td> <span v-text="blog.created_at.toDateTime()"></span> </td> <td> <a href="#0" v-on="click: edit_blog(blog)"><i class="uk-icon-edit"></i> <a href="#0" v-on="click: delete_blog(blog)"><i class="uk-icon-trash-o"></i> </td> </tr> </tbody> </table> <div v-component="pagination" v-with="page"></div> </div>{% endblock %}