bansi bansi - 1 month ago 16
Javascript Question

kendo ui grid template else if

The following code is not working. Can anyone help me write and

else if
condition here?

function getMyColumns() {
return [{
field: "xxx",
title: "1st",
width: "75px",
}, {
field: "ChoiceCode",
title: "2nd",
width: "75px",
template: "#if(ChoiceCode == null) {# #} else If(mychoice == ChoiceCode) {#" +
"{ #<div style='background-color:lightgreen'><span>#=ChoiceCode#</span></div>#}" +
"else {# <span>#=secondChoiceCode#</span> #}" +
"#}#"
}, {
}];
}


In the choice field if it's null I want to display an empty string and if it has a value and it matches my choice I want to highlight that cell with green color if it doesn't match my choice don't highlight it.

Answer

If you put your template inline, it will be this:

#if(ChoiceCode == null) {# #} else If(mychoice == ChoiceCode) {# { #<div style='background-color:lightgreen'><span>#=ChoiceCode#</span></div>#}else {# <span>#=secondChoiceCode#</span> #} #}#

Or better yet(purposely without #):

if(ChoiceCode == null) { } 
else If(mychoice == ChoiceCode) {
    { <div style='background-color:lightgreen'><span>#=ChoiceCode#</span></div>
}
else { 
    <span>#=secondChoiceCode#</span> } 
}

Noticed something wrong ? I think it should be this:

if(ChoiceCode == null) { } 
else If(mychoice == ChoiceCode) {
    <div style='background-color:lightgreen'><span>#=ChoiceCode#</span></div>
}
else { 
    <span>#=secondChoiceCode#</span>
}

Inline version:

# if(ChoiceCode == null) { # # } else if (mychoice == ChoiceCode) { #<div style='background-color:lightgreen'><span>#=ChoiceCode#</span></div> # } else { # <span>#=secondChoiceCode#</span> # } #

Code version:

template: "#if(ChoiceCode == null) {# #} else if(mychoice == ChoiceCode) {#" +                                
          "<div style='background-color:lightgreen'><span>#=ChoiceCode#</span></div>#}" +
          "else {# <span>#=secondChoiceCode#</span> #}#";