Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I have tried following
function fill() {
  this.months = ko.observableArray(['Jan', 'Feb', 'Mar', 'etc']);

  this.KpiTilesBobMetadata = ko.observableArray([
    { Name: 'Spend', Url: '' },
    { Name: 'Budget Paused', Url: 'Alerts tab in BoB' }
  ]);

  this.enable = ko.computed(function () {
    if (this.KpiTilesBobMetadata.Url() == '')
      return false;
    else
      return true;
  });
}

ko.applyBindings(fill());

<ul data-bind="foreach: KpiTilesBobMetadata">
    <li>
        <a data-bind="attr: {href : Url}, text : Name, if: enable"></a>
    </li>
</ul>


What I'm trying to do is, If Url is blank then the tag will be not displayed.

May I know where I'm going wrong?
Posted
Updated 20-May-14 22:24pm
v2
Comments
Kornfeld Eliyahu Peter 21-May-14 4:26am    
See the second example here: http://knockoutjs.com/documentation/if-binding.html

1 solution

Hi Prashant,

Please try following:
JavaScript
{ Name: 'Spend', Url: null }

<ul data-bind="foreach: KpiTilesBobMetadata">
    <li data-bind="if: Url">
        <a data-bind="attr: {href : Url}, text : Name, if: enable"></a>
    </li>
</ul>


This <ul><li> will only show when it contains values</li></ul>
 
Share this answer
 
Comments
dhage.prashant01 21-May-14 5:00am    
When I try to debug the code, it give me error saying cannot find Url in
this.KpiTilesBobMetadata.Url()
Kornfeld Eliyahu Peter 21-May-14 5:03am    
That because this.KpiTilesBobMetadata is an array.
It should be something like this.KpiTilesBobMetadata[0].Url().
But that will not help you anyway...
Take your time and try to understand Sunasara's solution - also take the sample I posted in the comment. Learn it and twist it as you wish...
dhage.prashant01 21-May-14 8:36am    
TQ it has worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900