jQuery.loadTemplate客户端模板
jQuery.Template虽然用起来没有Mustache简洁和方便,还是学习了解一下,做个笔记。
模板可以定义在页面script标签,如下
<script type="text/html" id="template">
<div data-content="author"></div>
<div data-content="date"></div>
<img data-src="authorPicture" data-alt="author"/>
<div data-content="post"></div>
</script>
也可以定义到独立的html文件中,好处是可以使用浏览器缓存,例如:
<div style="margin:50;border:solid 1px red"> <div data-content-text="author"></div>
<div data-content="date"></div>
<img data-src="authorPicture" data-alt="author" />
<div data-content="post"></div>
</div> <hr />
在客户端调用,如果数组数据,模板自动循环重复输出
//$("#template-container").loadTemplate($("#template"),
// {
// author: 'Joe Bloggs',
// date: '25th May 2013',
// authorPicture: 'https://www.baidu.com/img/bd_logo1.png',
// post: 'This is the contents of my post'
// }); $("#template-container").loadTemplate("Templates/template.html",
[{
author: 'Joe Bloggs',
date: '25th May 2013',
authorPicture: 'https://www.baidu.com/img/bd_logo1.png',
post: 'This is the contents of my post'
},
{
author: 'Wilson就是看到看看',
date: '25th May 2013',
authorPicture: 'https://www.baidu.com/img/bd_logo1.png',
post: 'This is the contents of my post'
}
]);
使用jquery.template 输出table
html:
<table border="1">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
template:
<tr>
<td data-content="id"></td>
<td data-content="name"></td>
</tr>
javascript:
var data = []; for (var i = 0; i < 10; i++) {
data.push({ "id": i, "name": "user_name_" + i.toString() });
}
$("#tbody").loadTemplate("Templates/tbList.html", data);
参考:https://github.com/codepb/jquery-template
数据绑定属性
There are a number of different bindings and ways to bind the data. The following attributes are available: "data-innerHTML" (>= 1.4.5) - binds the value supplied to the content (innerHTML) of the element (uses $(elem).html(value))
"data-content" - alias for the newer "data-innerHTML"
"data-content-text" - binds the value supplied to the content of the element as text (uses $(elem).text(value))
"data-content-append" - appends the value to the end of the element (uses $(elem).append(value))
"data-content-prepend" - prepends the value to the beginning of the element (uses $(elem).prepend(value))
"data-id" - sets the id of the element to the value provided (uses $(elem).attr("id", value));
"data-href" - sets the href value of the element to the value provided (uses $(elem).attr("href", value));
"data-alt" - sets the alt value of the element to the value provided (uses $(elem).attr("alt", value));
"data-value" - sets the value attribute of the element to the value provided (uses $(elem).val(value))
"data-class" - sets the class attribute of the element to the value provided (uses $(elem).class(value))
"data-link" - sets the innerHtml of the element to be a link to the value provided (wraps the content in an <a> tag).
"data-link-wrap" - wraps the element in a link to the value provided. Same as "data-link", but the <a> tag wraps the element as well as the content.
"data-options" - adds options to a select box. The value for this should reference an array of strings, each option will be output as a separate option. The value will be the same as the displayed text for each option. For a more powerful version of this look at the data-template-bind option.
自定义格式化方法
$.addTemplateFormatter({
UpperCaseFormatter : function(value, template) {
return value.toUpperCase();
},
LowerCaseFormatter : function(value, template) {
return value.toLowerCase();
},
SameCaseFormatter : function(value, template) {
if(template == "upper") {
return value.toUpperCase();
} else {
return value.toLowerCase();
}
}
});
模板
<tr>
<td data-content="id"></td>
<td data-content="name" data-format="LowerCaseFormatter"></td>
</tr>
jQuery.loadTemplate客户端模板的更多相关文章
- MVC下的客户端模板技术
1.引言 在Web编程中,我们有时经常需要使用Ajax来访问服务端的接口,然后使用这些返回的数据(一般格式都是JSON)来展示客户端的相关信息.例如:在一个商品列表,我们点击某一样的商品,查看该商品的 ...
- javascript&&jquery编写插件模板
javascrpt插件编写模板 这里不分享如何编写插件,只留一个框架模板,使用面向对象的形式进行编写,方便管理 ;(function(window,document){ function FnName ...
- jquery jtemplates.js模板渲染引擎的详细用法第三篇
jquery jtemplates.js模板渲染引擎的详细用法第三篇 <span style="font-family:Microsoft YaHei;font-size:14px;& ...
- jquery jtemplates.js模板渲染引擎的详细用法第二篇
jquery jtemplates.js模板渲染引擎的详细用法第二篇 关于jtemplates.js的用法在第一篇中已经讲过了,这里就直接上代码,不同之处是绑定模板的方式,这里讲模板的数据专门写一个t ...
- jquery jtemplates.js模板渲染引擎的详细用法第一篇
jquery jtemplates.js模板渲染引擎的详细用法第一篇 Author:ching Date:2016-06-29 jTemplates是一个基于JQuery的模板引擎插件,功能强大,有了 ...
- jquery.tmpl.js 模板引擎用法
1.0 引入: <script src="/js/jquery.tmpl.min.js"></script> 2.0 模板: <script type ...
- AMQ学习笔记 - 05. 客户端模板化
概述 客户端编程模型中,大部分的步骤都是相同的.将相同的部分做成模板,将不同的部分预留接口,实现者就只需要针对不同的部分提供实现. 设计 类图 发送方客户端 说明: 基于模板的思想,SendTempl ...
- jquery判断客户端的类型
针对不同客户端下载链接的页面响应样式不一样,更人性点而已 //匹配客户端类型 var isAndroid = navigator.userAgent.toLowerCase().match(/andr ...
- jquery的一个模板引擎-zt
jQuery-jTemplate.js下载:http://jtemplates.tpython.com/ 一 , 简单介绍 它是一个基于jQuery开发的javascript模板引擎.它主要的作用如下 ...
随机推荐
- 输入n行整数,每行的个数不确定,整数之间用逗号分隔
/*===================================== 输入n行整数,每行的个数不确定. 每行内部两个数之间用逗号隔开. 例如输入数据如下: 6 1,3,5,23,6,8,14 ...
- docker 使用redis
1. 安装 centos 7 yum install docker 2. 启动 修改配置: nano /etc/sysconfig/docker 添加一下信息: OPTIONS='--selinu ...
- (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
关于多线程执行显示进度条的实例! 之前回答了一篇关于怎么在线程中操作进度条的帖子,估计有人看的不是很明白今天没事,写了一个小小的实例,很简单,就2个文件权当抛砖引玉,希望有更好解决方案的人发表一下意见 ...
- shopex 网店系统安装教程
centos上配置shopex环境(LNMP) 安装包地址: http://download.csdn.net/detail/nanmu1258/9109297 软件默认下载至在/opt/local ...
- Hadoop学习地址
hortonworks: http://zh.hortonworks.com/hdp/downloads/ http://zh.hortonworks.com/hadoop-tutorial/supe ...
- bzoj3034: Heaven Cow与God Bull
Description __int64 ago,there's a heaven cow called sjy...A god bull named wzc fell in love with her ...
- KindEditor得不到textarea值的解决方法----摘至天涯
以前有朋友遇到过这个问题,就是KindEditor在火狐下或者其他浏览器下都无法得到textarea文本框的值,点击表单提交按钮得到的是空白.昨天天涯PHP博客[http://blog.phpha.c ...
- 查询SQLServer 服务器,执行过的SQL语句耗时!
SELECT creation_time N'语句编译时间',last_execution_time N'上次执行时间',total_physical_reads N'物理读取总次数',total_l ...
- [Freescale]Freescale L3.14.52_1.1.0 yocto build
可参照:http://blog.csdn.net/wince_lover/article/details/51456745 1. Refer to <基于i.mx6处理器的Yocto项目及Lin ...
- C基础--函数指针的使用
之前在看代码的时候,看了函数指针的使用,大体分为如下几类: 做一个function list,通过指针索引调用,使得处理功能类似的函数看起来更加清晰: 函数指针作为另一个函数的参数,用作回调: lin ...