0

I have a bootstrap-table which loads its data from a .json file. Everything works nicely but I am now trying to add a data-formatter="winLOSSFormatter"to one column and can't make that work.

Basically I am looking for it to be a simple if else statement that looks at the value and changes the color of the text for that field based on the value.

For example if the value includes the word "win," the text would be green.; the word "loss," the text would be red. Anything else the text would be standard black.

I believe this should be fairly simple to achieve with something along the lines of the script below but I can't seem to make it work.

Thanks!

function winLOSSFormatter(value) {
    if (value.indexof ('Win') > -1) {
                color = 'green';
                } else if (value.indexof('Loss') > -1 {
                'red';
                } else {
                 color = 'black';
                };
    return  '<div  style="color: ' + color + '">' + value.substring(1) + '</div>';
}
4

2 回答 2

0
function winLOSSFormatter(value) {
var color = '#99C262';

if (value.indexOf('W') > -1) {
            color = 'green';
            } else if (value.indexOf('L') > -1) {
            color = 'red';
            } else {
             color = 'black';
            }
return  '<div  style="color: ' + color + '; font-weight: 700;">' + value.substring() + '</div>';
}
于 2015-09-09T02:05:37.050 回答
0

您有一个未闭合的括号, } else if (value.indexof('Loss') > -1 {阻止了该函数的执行。下面是一个工作代码:

function winLOSSFormatter(value) {
    if (value.match(/.*Win.*/ig)) {
                var color = 'green';
                } else if (value.match(/.*Loss.*/ig)) {
                'red';
                } else {
                 color = 'black';
                };
    return  '<div  style="color: ' + color + '">' + value + '</div>';
}

我不确定你想如何处理价值ThisLossIsMyWinValue

于 2015-09-09T03:06:42.907 回答