Ext.XTemplate

1. template 是什么

template 是一个 HTML 片段的模板,它可以进行预编译从来提升性能。

2. Xtemplate

Xtemplate 继承自 template,Xtemplate 支持以下这些功能:

1.数组

1) 如果给定的是个数组,他会自动填充,为数组总每一项重复 template 中的 block。
2) for=“.” ,会从根节点开始重复,for=“record” ,会从 record 节点开始重复。

'<tpl for="score" >' +
'<span class="values"></span>' +
'</tpl>' +
例如,数组score,其中每一个值为values。

2.处理基本的比较运算符

注:大于和小于号要使用html实体字符。

'<tpl if="price &amp;&amp; discount">' +

'<div class="item-price"><span>{price}</span><span>{discount}</span></div>' +

'</tpl>'

例如,price && discount 为true时,显示div。

3.支持基本的数学函数

支持+ - * /等

<span class="report-blue-number">{[parseInt((values.rightNum/values.totalNum).toFixed(2) * 100)]}%</span>
<span class="report-blue-number">{[values.totalNum-values.rightNum]}</span>

4.执行有内置模板变量的代码

任何在{[ ]}中的代码都会在 template 的作用域中被执行。生成的结果包括计算表达式和结果,其中代码中有一些特殊的变量如下:

  • out:template 附加的输出数组
  • values:当前作用域下的值,你可以通过改变作用域来改变子 template
  • parent:祖先的作用域
  • xindex:如果是循环 template,index 以 1 开始
  • xcont: 如果是循环 template,会循环 template 的所有的数据。


另外, 在 "{% ... %}"中的代码会被直接插入到 template 生成的代码中, 这些代码块没有输出,它们可以做一些简单的操作,例如,在循环中 break/continue,或者,控制构造函数或方法的调用(要求它们没有输出)。

 

5 自定义成员函数

自定义函数需要写在 Xtemplate 内部,写在html代码之后,用逗号隔开。

tpl: new Ext.XTemplate(
html code,
{
starClass: function (values) {},
isPassed: function (defeatNum) {}
}
)

html行间属性调用该函数时使用class=“this.startClass(vaules)”,内容显示时{variable},函数输出内容时{[fu]}。

'<p class="this.setPriceCls(values.transaction_date)">{[Formatter.toFixed(values.price,1)]}</p>'
 

6 可以定义一些 API 中没有定义的特殊标记或内置运算符。

 

3.我遇到的问题及解决方法总结

 

3.1 我遇到的问题:


1)template 中出现空的 div 效果如图:


‘<div >’,
‘<tpl if = “entryType”=="money_fund"”>’,’<tpl>’,
‘</div >’,


原因:先声明了 DIV,后进行的判断,所以无论 IF 是否为真,都会有一个 DIV 块。
将 DIV 块放入 tpl 中即可,代码如下:


‘<tpl if = “entryType”=="money_fund"”>’,
‘<div >’, ‘</div >’,
’<tpl>’,


效果如下:
2)tpl 中断句时什么时候用“+”,什么时候用“,”
“+”:
没有“[]“时,断句要用“+”,如下:


tpl: '<div class="investment-portfolio-buysellpoint-head">' +
'</div>',


“,”:
有”[]“时,断句可以用“,”,如下:

itemTpl: [
'<div>',
'</div>'
],

3)设置tpl时,需要同时设置data,否则会造成数据不渲染。
4)使用tpl+data形式是,不是使用updateData方法,否则会造成数据不渲染。

 

3.2 总结:

1)参数的调用:
<tpl>{notifyingAt}</tpl>
其中:notifyingAt 为一个参数
参数可以做简单的四则样式,比如有一个 age 参数,我要算一算他 3 年以后是几岁的话:

<tpl>{age+3}</tpl>

2)函数的调用:

<tpl>{[Ext.Date.format(new Date(),"Y-m-d")]}</tpl>

3)函数中参数的调用:
<tpl>{[Ext.Date.format(value.notifyingAt,"Y-m-d")]}</tpl>

4)if else:

<tpl if="this.isBought(values.isBought)">
    // if
<tpl else>
    //else
</tpl>

转载于:https://www.cnblogs.com/jun3101s/p/5823955.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ExtJS 中的 ComboBox 组件是一个下拉框组件,用于从已知的预定义列表中选择一个选项。如果要实现带搜索功能的多选下拉框,可以使用 ExtJS 的 ComboBox 组件和 MultiSelect 组件结合使用。 首先,需要创建一个 ComboBox 组件,设置其 `multiSelect` 属性为 true,这样可以允许多选。然后,使用 `tpl` 属性指定下拉框的模板,为其添加一个搜索框,并使用自定义的函数来处理搜索框中的输入值。 接着,使用 `listConfig` 属性来配置下拉框列表的相关属性,例如:`loadingText`、`emptyText`、`itemSelector`、`itemTpl` 等等。其中,`itemTpl` 属性可以设置列表项的模板,用于显示下拉框中的选项。 最后,使用 `queryMode` 属性设置 ComboBox 组件的查询模式,可以选择 local 或 remote。如果选择 local 模式,那么数据源需要提前加载到客户端;如果选择 remote 模式,那么每次输入都会向服务器发送请求,获取匹配的数据。 下面是一段示例代码,展示了如何在 ExtJS 中实现带搜索功能的多选下拉框: ```javascript Ext.create('Ext.form.field.ComboBox', { fieldLabel: 'Choose State', multiSelect: true, queryMode: 'local', displayField: 'name', valueField: 'abbr', store: Ext.create('Ext.data.Store', { fields: ['abbr', 'name'], data: [{ "abbr": "AL", "name": "Alabama" }, { "abbr": "AK", "name": "Alaska" }, { "abbr": "AZ", "name": "Arizona" }, { "abbr": "AR", "name": "Arkansas" }] }), tpl: Ext.create('Ext.XTemplate', '<tpl for=".">', '<tpl if="xindex == 1">', '<div class="search-box">', '<input type="text" class="search-input" placeholder="Search..." />', '</div>', '</tpl>', '<tpl if="xindex > 1">', '<div class="item-wrap">', '<div class="item-name">{name}</div>', '<div class="item-abbr">{abbr}</div>', '</div>', '</tpl>', '</tpl>' ), listConfig: { loadingText: 'Loading...', emptyText: 'No matching states found.', itemSelector: '.item-wrap', itemTpl: Ext.create('Ext.XTemplate', '<div class="item-name">{name}</div>', '<div class="item-abbr">{abbr}</div>' ) }, listeners: { afterrender: function(combo) { var inputEl = combo.getEl().down('.search-input'); inputEl.on('keyup', function(e) { var value = e.target.value; if (value) { combo.store.filterBy(function(record) { var name = record.get('name'); return (name.toLowerCase().indexOf(value.toLowerCase()) !== -1); }); } else { combo.store.clearFilter(); } }); } } }); ``` 在上面的代码中,我们首先创建了一个 ComboBox 组件,并设置其 `multiSelect` 属性为 true。然后,使用 `tpl` 属性定义了下拉框的模板,其中添加了一个搜索框,并使用 `itemTpl` 属性定义了列表项的模板。 接着,我们使用 `listConfig` 属性配置了下拉框列表的相关属性,例如:`loadingText`、`emptyText`、`itemSelector`、`itemTpl` 等等。 最后,我们在 ComboBox 组件的 `afterrender` 事件中为搜索框添加了一个键盘输入事件监听器,当用户输入时,会根据输入值过滤数据源。这里使用了 store 的 `filterBy` 方法来实现过滤功能。 希望这段代码可以帮助你实现一个带搜索功能的多选下拉框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值