js 分页列表组件

<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<div class="news">
			<!-- <div style="border-bottom:1px solid black">
				<span>题目:xxxxxx</span>
				<span><img src="//cdn-img.easyicon.net/png/11931/1193161.gif" width="100" height="100" alt="mathematics">	</span>
				<span>
					<img src="//cdn-img.easyicon.net/png/5576/557603.gif" width="48" height="48" alt="Lime heart love">
					<img src="//cdn-img.easyicon.net/png/5576/557603.gif" width="48" height="48" alt="Lime heart love">
					<img src="//cdn-img.easyicon.net/png/5576/557603.gif" width="48" height="48" alt="Lime heart love">
				</span>
				<span>王巍老师</span>
				<span>2017-6-29 20:17:15</span>
			</div> -->
		</div>
			
		<script src = "https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
		<script type="text/javascript">

			var	decoration = {
				'heart' : '<img src="//cdn-img.easyicon.net/png/5576/557603.gif" width="48" height="48" />',
				'dog' : '<img src="//cdn-img.easyicon.net/png/12083/1208383.gif" width="48" height="48" />',
				'check_right' : '<img src="//cdn-img.easyicon.net/png/11847/1184723.gif" width="48" height="48" alt="Badge tick">'
			}

			var Page = function () {
				this.base = '<div style="border-bottom:1px solid black">\
							<span>{title}:xxxxxx</span>\
							<span><img src="{img}" width="100" height="100" alt="mathematics"></span>\
							<span>\
								{hot}\
							</span>\
							<span>{teacher}</span>\
							<span>{ctime}</span>\
						</div>';


				this.setTemplate = function (template) {
					this.base = template;
				}

				this.createListElement = function (obj) {
					for (this.i in obj) {
						this.key = this.i.split('|')[0];
						this.decoration = this.i.split('|')[1];
						this.decorations = '';

						if (this.decoration) {
							this.decoration = decoration[this.decoration];
							for (this.j = 0 ; this.j < obj[this.i]; this.j++) {
								this.decorations += this.decoration;
							}

							this.subTmp = '{' + this.key + '}';
							this.base = this.base.replace(this.subTmp, this.decorations);
						} else {
							this.subTmp = '{' + this.key + '}';
							this.base = this.base.replace(this.subTmp, obj[this.i]);
						}
					}

					return this.base;
				}

				this.render = function (dom, content) {
					$(dom).html(content);
				}
			}

			Page.setPage = function (page) {
				Page.pageSize = 5;
				Page.start = (page - 1) * Page.pageSize;
				Page.end = page * Page.pageSize;
				return {
					startSize : Page.start,
					endSize : Page.end
				};
			}

			var data = Page.setPage(50);

			$.ajax({
	             type: "GET",
	             url: "test.json",
	             data: data,
	             dataType: "json",
	             success: function(data){
                 	
                 },
                 error: function (data) {
                 	var data='[\
	                 	{\
	                 		"title":"Math xxx","img":"//cdn-img.easyicon.net/png/11931/1193161.gif","hot|heart":"3","teacher":"aaa","ctime":"2017-6-29 20:49:06"\
	                 	},\
	                 	{\
	                 		"title":"Math 2xxx","img":"//cdn-img.easyicon.net/png/11931/1193161.gif","hot":"3","teacher":"aaa","ctime":"2017-6-29 20:49:06"\
	                 	},\
{\
	                 		"title":"Math 3xxx","img":"//cdn-img.easyicon.net/png/11931/1193161.gif","hot|heart":"2","teacher":"aaa","ctime":"2017-6-29 20:49:06"\
	                 	},\
{\
	                 		"title":"Math 4xxx","img":"//cdn-img.easyicon.net/png/11931/1193161.gif","hot|dog":"3","teacher":"aaa","ctime":"2017-6-29 20:49:06"\
	                 	},\
{\
	                 		"title":"Math 5xxx","img":"//cdn-img.easyicon.net/png/11931/1193161.gif","hot|check_right":"3","teacher":"aaa","ctime":"2017-6-29 20:49:06"\
	                 	}\
                 	]';

                 	data = JSON.parse(data);

                 	var content = '';
                 	for (i in data) {
                 		var page = new Page();
         
                 		// content += '<div style="border-bottom:1px solid black">\
						// 	<span>' + data[i].title + '</span>\
						// 	<span><img src="' + data[i].img + '" width="100" height="100" alt="mathematics">	</span>\
						// 	<span>\
						// 		<img src="//cdn-img.easyicon.net/png/5576/557603.gif" width="48" height="48"\ alt="Lime heart love">\
						// 		<img src="//cdn-img.easyicon.net/png/5576/557603.gif" width="48" height="48" alt="Lime heart love">\
						// 		<img src="//cdn-img.easyicon.net/png/5576/557603.gif" width="48" height="48" alt="Lime heart love">\
						// 	</span>\
						// 	<span>' + data[i].teacher + '</span>\
						// 	<span>' + data[i].ctime + '</span>\
						// </div>';
                 		
                 		content += page.createListElement(data[i]);
                 	}

                 	page.render('.news', content);
                 }
         });

	
		</script>
	</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值