0

为了提取条件值,我将如何执行以下操作?

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 />'
},},
4

6 回答 6

1

因此,代替您的 if:

(this.y ? 'Successful' : 'Failed')
于 2013-04-13T00:47:13.967 回答
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 />'
},},
于 2013-04-13T00:47:41.483 回答
1

你需要使用三元运算符

formatter: function() {return ' ' +
    '<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
    '<b>Volume: </b>' + (this.y? 'Successful' : 'Failed') + '<br />'
},
于 2013-04-13T00:47:49.717 回答
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 为真,则返回“成功”,否则返回“失败”

更多信息在这里

于 2013-04-13T00:54:13.430 回答
0

将 if 条件包装在一个函数中,您可以在其中解析条件并返回正确的字符串

var "first part" + SomeMethodWithIfLogic(val) + "last part";
于 2013-04-13T00:46:49.990 回答
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 />'
于 2013-04-13T00:48:10.730 回答