admin管理员组

文章数量:1389762

While trying to implement condition function in kendo grid column template, there is a problem happening, data from my grid are not shown, my function is

function material() {
  if (PCommonPortalMethods.GetSiteLanguage() == 'en') {
    if (data.Unit._Key) {
      Unit.UnitGlobalName
    }
    else ('')
  }
  else {
    if (data.Unit._Key) {
      Unit.UnitLocalName
    }
    else ('')
  }
}      

and I call it from template like : template:'#= material() #'

I tried something like that also:

template: "#if (PCommonPortalMethods.GetSiteLanguage() == 'en') {# if(data.Unit._Key) #=Unit.UnitGlobalName#  else(" ") #} else { # if(data.Unit._Key) #=Unit.UnitLocalName#  else(" ") #} #"

can someone help me? what am I doing wrong? Thanks

While trying to implement condition function in kendo grid column template, there is a problem happening, data from my grid are not shown, my function is

function material() {
  if (PCommonPortalMethods.GetSiteLanguage() == 'en') {
    if (data.Unit._Key) {
      Unit.UnitGlobalName
    }
    else ('')
  }
  else {
    if (data.Unit._Key) {
      Unit.UnitLocalName
    }
    else ('')
  }
}      

and I call it from template like : template:'#= material() #'

I tried something like that also:

template: "#if (PCommonPortalMethods.GetSiteLanguage() == 'en') {# if(data.Unit._Key) #=Unit.UnitGlobalName#  else(" ") #} else { # if(data.Unit._Key) #=Unit.UnitLocalName#  else(" ") #} #"

can someone help me? what am I doing wrong? Thanks

Share Improve this question asked Jun 29, 2015 at 7:56 AviatorAviator 6134 gold badges11 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You should use return inside of it, change your script like this

function material() {
  if (PCommonPortalMethods.GetSiteLanguage() === 'en') {
    if (data.Unit._Key) {
      return Unit.UnitGlobalName;
    }
    return '';
  }
  else {
    if (data.Unit._Key) {
        return Unit.UnitLocalName;
    }
    return '';
  }
}   

or we could change your script like this

function material() {
  var text = '';

  if(data.Unit._key) {
    text = PCommonPortalMethods.GetSiteLanguage() === 'en' ? 
              Unit.UnitGlobalName : Unit.UnitLocalName;
  }

  return text;
}

Simple yet still readable, if you like to make a script template it could be like this.

<script type="text/x-kendo-template" id="template">
    # if (PCommonPortalMethods.GetSiteLanguage() === 'en') { #
    #    if(data.Unit._Key) { #
    <span> #= Unit.UnitGlobalName # </span>
    #    } #
    #} else { # 
    #    if(data.Unit._Key) {#
    <span> #= Unit.UnitLocalName # </span>
    #    } #
    # } #
</script>

and use it like this

template: kendo.template($("#template").html()),

本文标签: javascriptNested if condition in kendo grid column templateStack Overflow