In this document: http://knockoutjs.com/documentation/foreach-binding.html iterati...">

How to access the size of the "foreach:" binding in js knockout>

In this document: 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 using javascript / jquery?

Something like: alert('People size is '+people.size);

I need to access the size in order to perform a simple validation check.

+6
source share
1 answer

Do you mean inside the foreach itself? You can call the parent in a loop and then access the observable array again:

 $parent.people().length 

At any place where you have tied your view model, you can call:

 people().length 

Or you can add a computed observable model of your view. Inside the view model code, assign this to the var self variable, and then:

 var peopleCount = ko.computed(function() { return self.people().length; } 
+12
source

All Articles