Sencha touch在除了提供一些常用的組件之外還有很多很多的組件,Ext.String就是其中用來專門對字符串進行處理的,所在在使用原生js對字符串進行處理之外,還可以使用Ext.String的組件來進行字符串的處理。
常用的大概是讓多餘出來的部分變成省略號的ellipsis函數,其使用方法如下:
Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length.
'<tpl if="intro">',
'<div class="wellcourse_item_intro">{intro}</div>',//正常輸出
'<div class="wellcourse_item_intro">{[Ext.String.ellipsis(data.intro, 10, false)]}</div>',//使得多餘的部分變成縮略號
'</tpl>'
其中參數中的10,是包括了三個省略號在內的,也就是說只能顯示7個漢字~~
上面的實現也可以採用XTemplate的內部成員函數的方法來進行實現:
var tpl = new XTemplate(
'<tpl>',
'<p>{[this.shorten(data.intro)]}</p>',
'</tpl>',//下面用函數來實現此功能
{
shorten: function(name){
return Ext.String.ellipsis(name,10,false);
}
}
);
Ext.String中貌似能夠經常用到的函數不是很多,而且很多都可以用js去直接完成的。