简体   繁体   English

Sencha touch Xtemplate中的Javascript问题

[英]Javascript issue in Sencha touch Xtemplate

Extracting data from XML I want to preprocess one before sending it as a variable to template. 从XML提取数据我想在将数据作为变量发送到模板之前对其进行预处理。 A stackoverflow fellow pointed me to Ext.Xtemplate and that is close to do the job. 一个stackoverflow研究员向我指出了Ext.Xtemplate,它已经接近完成这项工作。

In each of my records I have a value "coordinates" of this pattern : 2.342556,48.873802,0.000000 3 items separated with comma. 在我的每条记录中,我都有这个模式的值“坐标”:2.342556,48.873802,0.000000 3个项目,以逗号分隔。 I need the first and the second. 我需要第一和第二。

So I decided to split but It doesn't work. 因此,我决定拆分,但不起作用。

My attempt so far (trying to display only one value for testing purposes) 到目前为止,我的尝试(尝试仅显示一个值以进行测试)

Code: 码:

var tpl = new Ext.XTemplate(        
        '<div>{name}<br>{[ values.coordinates.split(',')[1] ]}</div>'
             );

This echo nothing. 这没有回声。 If I display the [0] column of the array it displays the whole string, unsplited. 如果显示数组的[0]列,它将显示未拆分的整个字符串。 This means that it didn't parse the comma. 这意味着它没有解析逗号。

If I split with something else it works (for example with an integer) 如果我用其他东西拆分(例如整数)

Code: 码:

var tpl = new Ext.XTemplate(        
'<div>{name}<br>{[ values.coordinates.split(6)[1] ]}</div>'
         );

I tried to split with the point ('.') but it throw an error ("unexpected string") 我试图与点('。')分开,但抛出错误(“意外字符串”)

Maybe I should use a template member function but the example in the doc are not very clear for me. 也许我应该使用模板成员函数,但是文档中的示例对我来说并不十分清楚。

Thanks for your help, 谢谢你的帮助,

Julius 朱利叶斯

You need to escape the single quotes or use double quotes like so: 您需要转义单引号或使用双引号,如下所示:

var tpl = new Ext.XTemplate(        
        '<div>{name}<br>{[ values.coordinates.split(\',\')[1] ]}</div>'
             );

Otherwise your call will be like this: 否则,您的通话将如下所示:

var tpl = new Ext.XTemplate(        
        '<div>{name}<br>{[ values.coordinates.split(',   ')[1] ]}</div>'
             );

IE you pass 2 string parameters instead of 1. IE,您传递2个字符串参数,而不是1个。

A similar problem would be with this: 与此类似的问题是:

alert( 'o' 'hi there' );

Except this would throw a syntax error because there is no coincidential comma in there. 否则会引发语法错误,因为其中没有巧合的逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM