`

ExtJs的XTemplate使用

 
阅读更多

  ExtJs目前还是很多企业web应用首选的js框架,ext本身提供了很多很好的控件给我们使用了,简单的应用已经能基本满足要求,但是,当我们深度使用Ext后,为了应对一些奇怪的需求,就要使用到一些Ext的秘笈了,其中一个就是XTemplate。什么?你还不知道XTemplate是个什么东西?好吧,let go!

 

  •   先介绍一下XTemplate

 

 

 

HIERARCHY

Ext.Template
Ext.XTemplate

 

 

XTemplate是Template的一个子类,他能做什么呢?请看docs里面的描述:

A template class that supports advanced functionality like:

  • Autofilling arrays using templates and sub-templates
  • Conditional processing with basic comparison operators
  • Basic math function support
  • Execute arbitrary inline code with special built-in template variables
  • Custom member functions
  • Many special tags and built-in operators that aren't defined as part of the API, but are supported in the templates that can be created
  上面的意思就是说,XTemplate就是个模板类,模板是什么?就是结合数据动态生成页面(html)的工具:模板+数据 = HTML。上面还提到这个模板类支持的功能,包括:支持数组的输出、支持条件判断、支持基础的数学运算、支持内建的模板变量....等等

  •   XTemplate的用法

  知道XTemplate是什么之后,我们来看看怎么来使用他吧

 

先准备例子数据:

 

 

var data = {
name: 'Tommy Maintz',
title: 'Lead Developer',
company: 'Sencha Inc.',
email: 'tommy@sencha.com',
address: '5 Cups Drive',
city: 'Palo Alto',
state: 'CA',
zip: '44102',
drinks: ['Coffee', 'Soda', 'Water'],
kids: [{
        name: 'Joshua',
        age:3
    },{
        name: 'Matthew',
        age:2
    },{
        name: 'Solomon',
        age:0
}]
};

 

 

数组处理

 

 

var tpl = new Ext.XTemplate(
    '<p>Kids: ',
    '<tpl for=".">',       // process the data.kids node
        '<p>{#}. {name}</p>',  // use current array index to autonumber
    '</tpl></p>'
);
tpl.overwrite(panel.body, data.kids); // pass the kids property of the data object
 

 

 上面就是单独使用XTemplate的过程。

 

 

条件判断

 

The tpl tag and the if operator are used to provide conditional checks for deciding whether or not to render specific parts of the template. Notes:

  • Double quotes must be encoded if used within the conditional
  • There is no else operator — if needed, two opposite if statements should be used.
<tpl if="age > 1 && age < 10">Child</tpl>
<tpl if="age >= 10 && age < 18">Teenager</tpl>
<tpl if="this.isGirl(name)">...</tpl>
<tpl if="id==\'download\'">...</tpl>
<tpl if="needsIcon"><img src="{icon}" class="{iconCls}"/></tpl>
// no good:
<tpl if="name == "Tommy"">Hello</tpl>
// encode " if it is part of the condition, e.g.
<tpl if="name == &quot;Tommy&quot;">Hello</tpl>

Using the sample data above:

var tpl = new Ext.XTemplate(
   
'<p>Name: {name}</p>',
   
'<p>Kids: ',
   
'<tpl for="kids">',
       
'<tpl if="age &gt; 1">',
           
'<p>{name}</p>',
       
'</tpl>',
   
'</tpl></p>'
);
tpl
.overwrite(panel.body, data);
 

基本数学运算

 

 

  • The following basic math operators may be applied directly on numeric data values:

    + - * /
    For example:
    var tpl = new Ext.XTemplate(
       
    '<p>Name: {name}</p>',
       
    '<p>Kids: ',
       
    '<tpl for="kids">',
           
    '<tpl if="age &gt; 1">',  // <-- Note that the > is encoded
               
    '<p>{#}: {name}</p>',  // <-- Auto-number each item
               
    '<p>In 5 Years: {age+5}</p>',  // <-- Basic math
               
    '<p>Dad: {parent.name}</p>',
           
    '</tpl>',
       
    '</tpl></p>'
    );
    tpl
    .overwrite(panel.body, data);
     

 

 


 

 

使用内建的模板变量

 

 

Anything between {[ ... ]} is considered code to be executed in the scope of the template. There are some special variables available in that code:

  • values: The values in the current scope. If you are using scope changing sub-templates, you can change what values is.
  • parent: The scope (values) of the ancestor template.
  • xindex: If you are in a looping template, the index of the loop you are in (1-based).
  • xcount: If you are in a looping template, the total length of the array you are looping.

This example demonstrates basic row striping using an inline code block and the xindex variable:

 

var tpl = new Ext.XTemplate(
   
'<p>Name: {name}</p>',
   
'<p>Company: {[values.company.toUpperCase() + ", " + values.title]}</p>',
   
'<p>Kids: ',
   
'<tpl for="kids">',
       
'<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
       
'{name}',
       
'</div>',
   
'</tpl></p>'
 
);
tpl
.overwrite(panel.body, data);

使用自定义函数

 

 

 

  • One or more member functions can be specified in a configuration object passed into the XTemplate constructor for more complex processing:

    var tpl = new Ext.XTemplate(
       
    '<p>Name: {name}</p>',
       
    '<p>Kids: ',
       
    '<tpl for="kids">',
           
    '<tpl if="this.isGirl(name)">',
               
    '<p>Girl: {name} - {age}</p>',
           
    '</tpl>',
             
    // use opposite if statement to simulate 'else' processing:
           
    '<tpl if="this.isGirl(name) == false">',
               
    '<p>Boy: {name} - {age}</p>',
           
    '</tpl>',
           
    '<tpl if="this.isBaby(age)">',
               
    '<p>{name} is a baby!</p>',
           
    '</tpl>',
       
    '</tpl></p>',
       
    {
           
    // XTemplate configuration:
            compiled
    : true,
           
    // member functions:
            isGirl
    : function(name){
               
    return name == 'Sara Grace';
           
    },
            isBaby
    : function(age){
               
    return age < 1;
           
    }
       
    }
    );
    tpl
    .overwrite(panel.body, data);
     

  • 结合控件使用XTemplate

XTemplate在以下的控件中都实现了Xtemplate的用法:

例如用于分组Grid中的自定义Group列头

Sample usage:

var grid = new Ext.grid.GridPanel({
    // A groupingStore is required for a GroupingView
    store: new Ext.data.GroupingStore({
        autoDestroy: true,
        reader: reader,
        data: xg.dummyData,
        sortInfo: {field: 'company', direction: 'ASC'},
        groupOnSort: true,
        remoteGroup: true,
        groupField: 'industry'
    }),
    colModel: new Ext.grid.ColumnModel({
        columns:[
            {id:'company',header: 'Company', width: 60, dataIndex: 'company'},
            // groupable, groupName, groupRender are also configurable at column level
            {header: 'Price', renderer: Ext.util.Format.usMoney, dataIndex: 'price', groupable: false},
            {header: 'Change', dataIndex: 'change', renderer: Ext.util.Format.usMoney},
            {header: 'Industry', dataIndex: 'industry'},
            {header: 'Last Updated', renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
        ],
        defaults: {
            sortable: true,
            menuDisabled: false,
            width: 20
        }
    }),

    view: new Ext.grid.GroupingView({
        forceFit: true,
        // custom grouping text template to display the number of items per group
        groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
    }),

    frame:true,
    width: 700,
    height: 450,
    collapsible: true,
    animCollapse: false,
    title: 'Grouping Example',
    iconCls: 'icon-grid',
    renderTo: document.body
});

 

分享到:
评论

相关推荐

    ExtJS基础学习

    ExtJS学习

    Extjs 计算器 javascript计算器

    用Extjs 的xtemplate写的网页使用计算器 模仿Window的简单计算器,运行index.html可以直接跑

    ExtJs入门实例

    11. ExtJs2.0学习系列(11)--Ext.XTemplate 12. ExtJs2.0学习系列(12)--Ext.TreePanel之第一式 13. ExtJs2.0学习系列(13)--Ext.TreePanel之第二式 14. ExtJs2.0学习系列(14)--Ext.TreePanel之第三式(可增删改的树) 15...

    Js的MessageBox效果代码

    在opera 里图层不能透明 对于页面内有iframe的也无法使用 在ie里无法遮住select的 MessageBox演示_软件开发网_www.jb51.net   普通演示     回调演示一     回调演示二     回调演示三  ...

    精通JS脚本之ExtJS框架.part2.rar

    第3章 开始使用ExtJS 3.1 ExtJS概述 3.1.1 ExtJS简介 3.1.2 ExtJS中的基本概念 3.1.3 ExtJS与常见JavaScript库的介绍与对比 3.2 创建第一个程序 3.2.1 下载ExtJS 3.2.2 部署到开发工具中 3.2.3 编写Hello...

    轻松搞定Extjs_原创

    三、Ext.XTemplate 38 四、小结 39 第七章:格式化 40 一、用户需要优秀体验的内容 40 二、Ext.util.Format类 40 三、再谈XTemplete 44 四、如果连Format都不能满足XTemplete的需要呢? 45 五、小结 45 第八章:...

    extjsExtjs学习笔记——多个图片XTemplate排版居中,自动缩放处理

    该资源通过一个代码实例授予对Ext处理图片的居中、排版问题陌生的开发人员。适用于初学者和有一定开发基础的人群,文件下载下来就可以用。

    精通JS脚本之ExtJS框架.part1.rar

    第3章 开始使用ExtJS 3.1 ExtJS概述 3.1.1 ExtJS简介 3.1.2 ExtJS中的基本概念 3.1.3 ExtJS与常见JavaScript库的介绍与对比 3.2 创建第一个程序 3.2.1 下载ExtJS 3.2.2 部署到开发工具中 3.2.3 编写Hello...

    ExtJS入门教程(超级详细)

    23、Ext.XTemplate类 ………………… 21 24、Ext.data.Connection类 ……………… 22 25、Ext.Ajax类 ………………………… 22 26、Ext.data.Record类 ………………… 23 27、Ext.data.DataProxy类 …………… 24 28...

    ExtJS 核心的抽奖程序(共享源代码)

    不过多亏ExtJS给我解决了各种难题,让我很快开发出来这套抽奖程序,现在程序圆满完成任务,并且达到了很好的现场气氛,心中很自豪。 由于时间仓促,代码没有布局整理。现在把程序共享出来。有兴趣想玩玩的可以下载...

    ExtJs4_笔记.docx

    三、在extjs控件上使用提示 59 第八章 Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件 61 一、滚轴控件 Ext.slider 61 二、进度条控件 Ext.ProgressBar 64 三、编辑控件 Ext.Editor 68 第...

    基于extjs的.NET3.5控件Coolite 1.0.0.34580(Preview预览版)

    基于extjs的.NET3.5控件Coolite 1.0.0.34580 (Preview预览版), *************************************************** * Version 1.0.0 升级日志 * *************************************************** 1. ...

    Ext Js权威指南(.zip.001

    8.1.4 超级模板:ext.xtemplate(包括ext.xtemplateparser和ext.xtemplatecompiler) / 393 8.1.5 模板的方法 / 396 8.2 组件的基础知识 / 396 8.2.1 概述 / 396 8.2.2 组件类的整体架构 / 397 8.2.3 布局类的...

    ext-locale:Ext JS的轻量级本地化

    另外,它可以在XTemplate中使用,并确保与视图绑定的紧密集成。 当前版本2.5.0的兼容性: Ext JS 6.5.0+ Sencha CMD 6.5+ *需要动态软件包加载器。 *对于旧版本,请尝试使用“旧”代码分支。 示例1-简单字符串...

    Ext 开发指南 学习资料

    8.3.3. 醍醐灌顶,功能强劲的模板引擎XTemplate。 8.4. Ext.data命名空间 8.4.1. proxy系列 8.4.1.1. 人畜无害MemoryProxy 8.4.1.2. 常规武器HttpProxy 8.4.1.3. 洲际导弹ScriptTagProxy 8.4.2. reader系列 8.4.2.1....

Global site tag (gtag.js) - Google Analytics