2013-04-13 52 views
0

我将如何做以下事项,以拉动条件值?Javascript字符串连接,如果

formatter: function() {return ' ' + 
    '<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' + 
    '<b>Volume: </b>' + if (this.y) {'Successful';} else { 'Failed';} + '<br />' 
},}, 

回答

1

创建独立的可变存储信息:

var message = 'Failed'; 
if (this.y) { 
    message = 'Successful'; 
} 

或者使用?:操作:

formatter: function() {return ' ' + 
    '<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' + 
    '<b>Volume: </b>' + (this.y ? 'Successful' : 'Failed') + '<br />' 
},}, 
0

包裹,如果在一个函数的条件,你可以解决的条件,并返回正确的字符串

var "first part" + SomeMethodWithIfLogic(val) + "last part"; 
1

因此,代替你的,如果:

(this.y ? 'Successful' : 'Failed') 
1

你需要使用三元运算

formatter: function() {return ' ' + 
    '<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' + 
    '<b>Volume: </b>' + (this.y? 'Successful' : 'Failed') + '<br />' 
}, 
0
formatter: function() { 
    if (this.y) { 
     success = 'Successful' 
    } else { 
     success = 'Unsuccessful' 
    }; 
    return ' ' + 
    '<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' + 
    '<b>Volume: </b>' + success + '<br />' 
0

你可以这样做:

formatter: function() {return ' ' + 
    '<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' + 
    '<b>Volume: </b>' + ((this.y == true) ? "Successful": "Failed") + '<br />' 
},}, 

这三元运营商将允许你插入逻辑进入你的字符串。

打破: 如果this.y为真,则返回 “成功” 否则返回 “失败”

Some more information here