`
endual
  • 浏览: 3509168 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ExtJs 4 – XTemplates

 
阅读更多

ExtJs 4 – XTemplates

If you’re an avid ExtJs developer like me, you’re bound to reach a point where you need to display an HTML view with dynamic variables. There are many scenarios like this. The most common example is a tabular grid or data view, but can range from a simple panel to an image gallery and beyond. So how does all this work?

A super simple example XTemplate looks like this: [view sandbox online]

var myTemplate = new Ext.XTemplate( '<div class="special-window">', '<div class="title">{title}</div>', '<div class="content">{content}</div>', '</div>');

Right now, we have an XTemplate defined, but we’re not all the way there yet. The XTemplate is missing data, and it needs to be rendered before it can be used. Here’s one way that we can do this:

var myHtmlTemplate = myTemplate.apply({ title: 'Special Window Title', content: 'Example window body text'});

At this point, we’ve rendered the template and stored it in a variable. The HTML produced will look something like this:

<div class="special-window"> <div class="title">Special Window Title</div> <div class="content">Example window body text</div></div>

Ok, this is cool.. but what other tricks does it know? Well, the above code was an entry-level example to the world of the Ext.XTemplate. Rest assured this is merely the tip of an enormous ice berg. A more advanced implementation is needed when you have to deal with a multidimensional dataset. Let’s say, a user signature: [view sandbox online]

var mySignature = { name: 'Hans Doller', department: 'Development', phoneNumbers: [ {number:'555-111-2222',rel:'office'}, {number:'555-111-2223',rel:'cellular'}, {number:'555-111-2224',rel:'home'} ], emailAddresses: [ 'i-hate@spam.com', 'wouldntyou@liketo.know' ]};

As you can see, we’re dealing with some nested arrays, objects and string variables.. With normal Javascript and HTML, it would be a PITA to display. Never fear, XTemplate is here! Here’s what the template could look like:

var mySignatureTemplate = new Ext.XTemplate( '<div class="signature">', '<div class="name">{name}</div>', '<div class="department">{department}</div>', '<ul class="phone">', '<tpl for="phoneNumbers">', '<li class="number">{number} ({rel})</li>', '</tpl>', '</ul>', '<ul class="email">', '<tpl for="emailAddresses">', '<li class="address"><a href="mailto:{.}">{.}</a></li>', '</tpl>', '</ul>', '</div>');

To render the template to a string, once again we do:

var myHtmlSignature = mySignatureTemplate.apply(mySignature);

And voilà, the resulting HTML should look something like this:

<div class="signature"> <div class="name">Hans Doller</div> <div class="department">Development</div> <ul class="phone"> <li class="number">555-111-2222 (office)</li> <li class="number">555-111-2223 (cellular)</li> <li class="number">555-111-2224 (home)</li> </ul> <ul class="email"> <li class="address"><a href="mailto:i-hate@spam.com">i-hate@spam.com</a></li> <li class="address"><a href="mailto:wouldntyou@liketo.know">wouldntyou@liketo.know</a></li> </ul></div>

This article covers the most basic parts of XTemplate usage, there are a lot of features that were not covered in this article. For a full list of features, check out the online XTemplate documentation. If you have any specific questions, feel free to comment on this article, and I’ll help out where I can.

分享到:
评论

相关推荐

    MyEclipse安装ExtJS插件–Spket IDE

    MyEclipse安装ExtJS插件–Spket IDE,可以实现快速开发,点点开始吧

    EXTJS4自学手册

    EXTJS4自学手册——EXT文件目录,本地加载文档,命名规范 EXTJS4自学手册——EXT基本方法、属性(onReady、define、create) EXTJS4自学手册——EXT基本方法、属性(apply、applyIf、constructor) EXTJS4自学手册...

    Extjs4的demo

    Extjs4的demo 很不错的例子

    包含各种类型的extjs小图标,Extjs4小图标

    包含各种类型的extjs小图标,Extjs4小图标

    extJS4升级至extJS6.6所遇问题及解决方案

    项目进行前端框架升级——extJS 4升级至extJS6.6所遇的一些问题及相对应的解决方案建议

    extJs3升级extjs4方案

    extJs3升级extjs4方案, extJs3升级extjs4需要修改大量代码

    Extjs4添加tooltip

    Extjs4的textfield控件竟然没有tooltip悬浮提示,这应该算是个bug吧。网上查了很多,也没有方案。后来自己通过查询并整理出这个方案,终于实现了。 思想如下: 在textfield渲染时注册mouseover事件,然后在其中调用...

    EXT JS ExtJS 4

    我很自豪能代表Sencha和ExtJS团队和大家成功的分享了ExtJS 4的预览版、3个beta版和今天发布的正式版等5个版本。 ExtJS 4从创建之初,就以最全面现代化为目标,它采用了改进的架构和加入了许多新的特性,从而使你可以...

    ExtJS4中文教程2 开发笔记 chm

    ExtJS4学习笔记(九)---ExtJS4 EditGrid(可编辑的Grid) ExtJS4学习笔记(二)---HBox的使用 ExtJS4学习笔记(五)---Grid分页 ExtJS4学习笔记(八)---Grid多选 全选 ExtJS4学习笔记(六)---多表头Grid ExtJS4学习笔记(十)--...

    extjs4.x学习笔记

    从开发大型的web后台或者企业管理系统来看,ExtJs3的缺陷还是比较明显的,从ExtJs4 开始引入了MVC架构,从而能够从容的组织系统的JS文件了。 从ExtJs5开始则引入了MVVC架构。 从网上资料来看,多数都是停留在ExtJs3...

    ExtJs 4 API中文

    ExtJs 4 API 中文

    extjs4 中文 API

    extjs4 中文 API

    Extjs4的FormPanel从后台load json数据的要点

    Extjs4 表单从数据库读取数据映射到对应的字段中显示

    ExtJS4之初体验

    ExtJS4之初体验,中文版的文档,Getting started with extjs的中文版

    Extjs4学习指南源代码(JAVA后台)

    个人《Extjs4学习指南》电子文档过程中整理的Myeclipse工程,有以下修改:1、数据库改为Mysql;2、前四章后台改为servlet;3、最后一章后台改为SSH架构。 使用说明:1、BeginExtjs4目录为前四章的例子代码;2、SSH_...

    ExtJS4下拉树组件

    ExtJS4下拉树组件 ExtJS4下拉树组件

    Extjs4 grid使用例子

    基于Extjs4官方的例子,做了一个关于MVC和Grid增删改的小例子

    extjs4 treeGrid实例

    用extjs4 TreeGrid做的report报表

    extjs4-教程

    extjs4-教程搭建 ExtJS 入门学习、可视化开发环境、布局详解 、文档阅读

    Extjs4详解.pdf

    Extjs4详解,详细介绍Extjs4开发技术,自是不必多说!

Global site tag (gtag.js) - Google Analytics