layui.define(['table', 'laypage','jquery', 'element'], function(exports) { "use strict"; var MOD_NAME = 'card', $ = layui.jquery, element = layui.element, laypage = layui.laypage; var pearCard = function(opt) { this.option = opt; }; pearCard.prototype.render = function(opt) { var option = { // 构建的模型 elem: opt.elem, // 数据 url 连接 url: opt.url, // lineSize 每行的个数 lineSize: opt.lineSize ? opt.lineSize : 4, // 共多少个 pageSize: opt.pageSize ? opt.pageSize : 12, // 当前页 currentPage: opt.currentSize ? opt.currentSize : 0, // 完 成 函 数 done: opt.done ? opt.done : function() { alert("跳转页面"); } } // 根 据 请 求 方 式 获 取 数 据 if (option.url != null) { // 复制数据 option.data = getData(option.url).data; } // 根据结果进行相应结构的创建 var html = createComponent(option.data); $(option.elem).html(html); // 初始化分页组件 laypage.render({ elem: 'cardpage' ,count: 100 ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'] ,jump: function(obj){ console.log(obj) } }); return new pearCard(option); } function createComponent(data) { var html = "
" var content = createCards(data); var page = "
" content = content + page; html += content + "
" return html; } /** 创建指定数量的卡片 */ function createCards(data) { var content = "
"; $.each(data, function(i, item) { content += createCard(item); }) content += "
" return content; } /** 创建一个卡片 */ function createCard(item) { var card = '

'+item.title+'

'+item.remark+'
'+item.time+'
' return card; } /** 同 步 请 求 获 取 数 据 */ function getData(url) { $.ajaxSettings.async = false; var data = null; $.get(url, function(result) { data = result; }); $.ajaxSettings.async = true; return data; } exports(MOD_NAME, new pearCard()); })