为了提取条件值,我将如何执行以下操作?
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 />'
},},
为了提取条件值,我将如何执行以下操作?
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 />'
},},
因此,代替您的 if:
(this.y ? 'Successful' : 'Failed')
创建单独的变量来存储消息:
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 />'
},},
你需要使用三元运算符
formatter: function() {return ' ' +
'<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
'<b>Volume: </b>' + (this.y? 'Successful' : 'Failed') + '<br />'
},
你可以这样做:
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 为真,则返回“成功”,否则返回“失败”
将 if 条件包装在一个函数中,您可以在其中解析条件并返回正确的字符串
var "first part" + SomeMethodWithIfLogic(val) + "last part";
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 />'