So I have two separate models, one ‘items’ model, the second a ‘sites’
model… I’m using KO to bind this data to two separate elements on
the DOM (and working as needed), but I’ve found a need to replace one
of my bound columns on one of my models, with data from the other.
On my ‘items’ model, I have a site ID column, that I’d love to swap
with the actual ‘SiteName’ property on that model (simple name, value-
pair – SiteName, SiteId)… Does any one know a way to do this within
KO?
I really want to keep the model data itself in tact on the server side,
verses just creating a custom model on the server side that does it for me.
I’m sure I could give those elements a special class, and loop
through them and replace them manually with jQuery, but I thought that
KO might have an easier way of doing this.
Thanks!
I have tried something like this, but it doesn’t seem to work (yes, I know div tags within a table element are not standards based, I just wanted to see if it would work, and if it did, I’d transition from the table to another formatting option)
<tbody data-bind="foreach: items">
<tr data-bind="click: updateItem">
<td data-bind="text: ItemName"></td>
<div data-bind="foreach: sites">
<div data-bind="if: items.SiteId = sites.SiteId">
<td data-bind="text: sites.SiteName"></td>
</div>
</div>
The jQuery to do this using my returned model is:
Where .siteIdCell is the class I applied to the columns using this value, and allSites is the object array I’m receiving via JSON.
Not sure if I’ll get many responses, but I just figured I’d update this if anyone else has the same issue, and there’s no ability to do this in KO.