`

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
});

 

分享到:
评论

相关推荐

    vue.js v2.5.17

    vue.js vue.min.js vue-router.js vue-router.min.js

    DM8-SQL语言详解及其数据管理和查询操作指南

    内容概要:本文档是关于DM8数据库系统的SQL语言使用手册,全面介绍了其SQL语言的基础特性、功能、语法规则及相关使用方法。手册首先概述了DM_SQL的特点和它支持的各种数据类型(例如:数值、字符串、日期时间类型等)及其对应的表达式。接下来深入探讨了一系列高级话题,涵盖数据定义语句-DDL、数据操纵语句-DML和数据控制语句,具体讲解了多种表类型(常规表、HUGE表、外部表)的创建与管理,以及索引机制(全文索引、位图连接索引等)。此外还提供了丰富的实例示范,确保读者能直观理解并应用于实际项目。同时,文档也阐述了各种系统级别的功能,如日志和检查点管理、MPP管理和统计信息生成等功能的使用方法。 适合人群:具有一定数据库基础知识并且有意深入了解DM8数据库系统特性的开发工程师、数据库管理人员或相关专业技术人员。 使用场景及目标:①指导开发人员掌握DM8中各类SQL命令的实际运用技巧;②帮助运维人员学会通过SQL来进行有效的数据维护与优化,从而提升数据库的整体性能。 其他说明:该手册不仅仅是SQL理论的讲述,而是通过大量的实例演示让使用者更加熟悉日常的工作任务。对于复杂的企业级应用场景尤其有

    1108_ba_open_report.pdf

    1108_ba_open_report

    anslow_02_0109.pdf

    anslow_02_0109

    以下是OpenCV在不同操作系统下的下载与安装教程

    opencv下载安装教程

    aronson_01_0707.pdf

    aronson_01_0707

    Designing Deep Learning Systems. A software engineer's guide - 2023.pdf

    Wang Chi, Szeto Donald - Designing Deep Learning Systems. A software engineer's guide

    基于豆瓣图书网站的图书数据分析与可视化

    使用Python语言对Django框架进行设计,选用豆瓣读书网站(https://book.douba n.com/)作为研究对象,基于用户的阅读行为数据,运用网络爬虫技术来抓取所需数据,随后对这些数据进行深度清理,存储到数据库中。借助ECharts的可视化工具,深入分析和直观展示,实现数据分析与可视化。

    barbieri_01_0108.pdf

    barbieri_01_0108

    brown_3ck_01_0718.pdf

    brown_3ck_01_0718

    基于Python的Django-vue学生选课系统实现源码-说明文档-演示视频.zip

    关键词:学生选课系统;Python语言;MySQL数据库 学生选课系统采用B/S架构,数据库是MySQL。网站的搭建与开发采用了先进的Python进行编写,使用了Django框架。该系统从三个对象:由管理员和学生、教师来对系统进行设计构建。主要功能包括:个人信息修改,对学生、教师信息、课程信息、课程分类、选择课程、班级、成绩通知、教室信息、系统管理等功能

    ganga_02_0909.pdf

    ganga_02_0909

    毕设-springboot大学生竞赛管理系统(免费领取)

    毕设-springboot大学生竞赛管理系统(免费领取)

    agenda_3cd_01_0716.pdf

    agenda_3cd_01_0716

    Swift语言教程:从入门到实践 Swift是苹果公司开发的一种多范式编程语言,用于iOS、macOS、watchOS和tvOS应用开发 它结合了C和Objective-C的优点,同时提供了现代编程语

    Swift语言教程:从入门到实践 Swift是苹果公司开发的一种多范式编程语言,用于iOS、macOS、watchOS和tvOS应用开发。它结合了C和Objective-C的优点,同时提供了现代编程语言的许多特性,如安全性、速度以及表达力。以下是从入门到实践的Swift语言教程。 一、Swift基础 1. Swift环境设置 Xcode安装:下载并安装最新版本的Xcode,这是开发Swift应用的集成开发环境(IDE)。 创建项目:在Xcode中创建一个新的Swift项目,了解项目结构。 2. 基本语法 变量与常量:使用var声明变量,使用let声明常量。 数据类型:整数(Int)、浮点数(DoubleFloat)、字符串(String)、布尔值(Bool)等。 类型安全:Swift是强类型语言,每个变量和常量在声明时都需要指定类型(尽管Swift也能自动推断类型)。 运算符:算术运算符、比较运算符、逻辑运算符等。 3. 控制流 条件语句:if、else if、else。 循环语句:for循环、while循环、repeat-while循环。 控制转移语句:break、continue

    【宝城期货-2025研报】钢材、铁矿石日报:关税扰动不断,钢矿弱势运行.pdf

    【宝城期货-2025研报】钢材、铁矿石日报:关税扰动不断,钢矿弱势运行.pdf

    anslow_05_0110.pdf

    anslow_05_0110

    c盘满了怎么清理.zip

    介绍了清理C盘空间的多种方法,包括使用系统工具、清理临时文件、卸载残留文件、移动用户文件夹、清理系统日志和虚拟内存等,旨在帮助用户有效释放C盘空间,提升电脑性能。

    达梦DM8分布式计算集群的技术架构与管理指南

    内容概要:本文档详细介绍达梦DM8分布计算集群(DMDPC)的设计理念、系统架构、关键技术及其实现。涵盖了系统原理、系统特性、关键技术、构建和配置步骤等内容。文档还提供了对计划生成、子任务划分、执行调度及链路通讯等机制的深入解析,确保使用者理解如何部署、管理和优化这个新型分布式数据库解决方案。 适合人群:本文档适用于熟悉数据库技术尤其是分布式数据库的专业人士,主要包括开发工程师、测试工程师、技术支持工程师、数据库管理员及相关管理人员。 使用场景及目标:①理解并掌握 DM8 分布计算集群的基本概念和技术原理;②指导如何正确配置、部署以及日常维护DM8分布式集群;③为解决复杂的大型企业级应用程序对高性能和高可靠的数据库需求提供技术支持。 其他说明:文档不仅讲述了技术层面的知识,还有详细的实例示范,便于用户根据自己的情况进行操作,有助于加快项目的实施进度。此外,文档还包括了丰富的配置参数和性能调优方面的内容,为优化系统性能提供了有价值的参考资料。

    dawe_01_0508.pdf

    dawe_01_0508

Global site tag (gtag.js) - Google Analytics