---
title: "extjs模板的使用"
author: "minzaipiao"
date: "2009-04-29T11:03:41Z"
tags: ["EXT", "Gmail", "json"]
categories: ["Web前端"]
slug: "extjs"
draft: false
lastmod: "2009-04-29T11:04:50Z"
id: 376555
url: "https://dengm1010.iteye.com/blog/376555"
---

\[code="js"] Ext.onReady(function(){ Ext.onReady(function(){ //定义模板 使用标签tpl和操作符for var tpl1 = new Ext.XTemplate( '', '', '', '', '', '

姓名年龄{name}{age}

' ); //通过自定义格式化函数解析json对象 {stature:this.parseJson()} var tpl2 = new Ext.Template( '', '', '', '', '', '', '', '

姓名{name}年龄{age}身高{stature:this.parseJson()}

' ); tpl2.parseJson = function(json){ return json.num + json.unit; } //数组索引与简单数学运算 {#} 每一项都加上序号 var tpl3 = new Ext.XTemplate( '', '', '', '', '', '

序号姓名工资{#}{name}{wage * .9}

' ); //自动渲染简单数组 使用特殊变量{.}可循环输出 var tpl4 = new Ext.XTemplate( '', '', '', '', '', '

序号姓名{#}{.}

' ); //基本的条件逻辑判断 这没有else的操作符 如需要，就要写两个逻辑相反的if的语句 //输出工资大于1000的员工信息 工资 * 0.9 var tpl5 = new Ext.XTemplate( '', '', '', '', '', '', '', '

序号姓名工资{#}{name}{wage * .9}

' ); //定义组合框模板 xindex：若是循环模板，这是当前循环的索引index（从1开始）。 // values：当前作用域下的值 var itemTpl = new Ext.XTemplate( '', '

', '{#} ：{\[this.check(values)]}

', '', { check : function (values) { if(values.value &gt; 2 ){ return ""+values.item+""; }else { return ""+values.item+""; } } } ) new Ext.form.FormPanel({ applyTo :'target', title:'在Extjs组件中使用模板', labelSeparator :'：',//分隔符 height : 100, frame : true, width : 350, items : \[ new Ext.form.ComboBox({ fieldLabel:'combo', displayField:'item', valueField:'value', tpl : itemTpl,//引入自定义模板 editable : false, mode: 'local', triggerAction: 'all', store : new Ext.data.SimpleStore({ fields: \['item','value'], data : \[\['条目1',1],\['条目2',2],\['条目3',3], \['条目4',4],\['条目5',5],\['条目6',6]] }) }) ] }); var productTpl = new Ext.XTemplate( '{0}{1}{2}' ); Ext.get('addProduct').on('click',function(){ var pname = Ext.get('productName').getValue(); var pnum = Ext.get('productNum').getValue(); var pprice = Ext.get('productPrice').getValue(); productTpl.append('product-table',\[pname,pnum,pprice]); }); //定义模板数据 var data1 = \[ {name:'张三',age:20}, {name:'李四',age:25}, {name:'王五',age:27}, {name:'赵六',age:26} ]; var data2 = { name : 'tom', age : 24, stature : { num : 170, unit : '厘米' } }; var data3= \[ {name : '张三',wage : 1000}, {name : '李四',wage : 1200}, {name : '王五',wage : 900}, {name : '赵六',wage : 1500} ]; var data4 = \['张三','李四','王五','赵六']; //模板值和模板进行组合并将新生成的节点插入到id为'tpl-table'的元素中 tpl1.append('tpl-1',data1); tpl2.append('tpl-2',data2); tpl3.append('tpl-3',data3); tpl4.append('tpl-4',data4); tpl5.append('tpl-5',data3); }); }); \[/code] tpl.html \[code="html"]

产品名称：  
产品数量：  
产品价格：

产品名称数量金额

\[/code]