In this doc : http://knockoutjs.com/documentation/foreach-binding.html iteration is achieved using the 'foreach' binding :
<tbody data-bind="foreach: people">
Is it possible to access the size of this binding via javascript/jquery ?
Something like : alert('People size is '+people.size);
I need to access the size in order to do a simple validation check.
Do you mean within the foreach itself? You can call the parent in the loop, then access the observable array again:
$parent.people().length
Or anywhere you have bound your view model, you can call:
people().length
Or you can add a computed observable to your view model. Inside your view model code, assign this to a var named self, then:
var peopleCount = ko.computed(function()
{
return self.people().length;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With