javascript - How to concatenate all items in an Observable Array using Knockout? -
i have folowing object:
var transaction = function (data) { this.id = ko.observable(data.transaction.id); this.number = ko.observable(data.transaction.number); this.userid = ko.observable(data.transaction.userid); this.password = ko.observable(data.transaction.password); this.properties = ko.observablearray(data.property); }
the properties observable array looks this(in json):
[{"address1":"111 broadway","city":"brooklyn","state":"ny","zip":null}, {"address1":"222 broadway","city":"brooklyn","state":"ny","zip":null}]
in html have this:
<span>property</span> <span data-bind="foreach: properties"> <span data-bind="text: address1"></span> <span data-bind="text: city"></span> span data-bind="text: state"></span> <span data-bind="text: zip"></span> </span>
in browser looks like:
property 111 broadway brooklyn ny 222 broadway brooklyn ny
and want like:
111 broadway, brooklyn, ny 222 broadway, brooklyn, ny
any advice?
you create computed property in transaction
prototype properties
list view, simplify html markup associated it.
this.propertyview = ko.purecomputed(function() { return this.properties().map(function(prop) { var singleprop = ''; (var p in prop) if (prop[p]) singleprop += prop[p] + ', '; return singleprop.slice(0,-2); }); }, this);
<span>properties</span> <!-- ko foreach: propertyview --> <p data-bind="text: $data"></p> <!-- /ko -->
Comments
Post a Comment