I'm new to knockout.js for data binding. I have a simple example below using knockout.js to data bind a table, but when it comes to data binding nested tables with filter is hard for me to figure it out in knockout.js. If you look at the two sample tables below, one is using knockout.js to data bind a record in one table and the other one is a nested table using Razor codes with Where clause in the foreach statement to filter the replies of each comments. The second one with Razor codes is a nested table I used to display and databind comments and its replies. I'm trying to apply this in knockout.js for data binding. How do I do this in knockout.js? Thanks...
Table 1.
<table id="products1" data-bind="visible: Products().length > 0">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody data-bind="foreach: Products">
<tr>
<td data-bind="text: Id"></td>
<td data-bind="text: Name"></td>
<td data-bind="text: Category"></td>
<td data-bind="text: formatCurrency(Price)"></td>
<td>
<button data-bind="click: $root.edit">Edit</button>
<button data-bind="click: $root.delete">Delete</button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td>Total :</td>
<td data-bind="text: formatCurrency($root.Total())"></td>
<td></td>
</tr>
</tfoot>
</table>
Table 2.
<table id="mytable">
@foreach (var item in Model.Comments)
{
<tr >
<td class="tdstyle" >
<div style="font-weight:bold;"> @Html.DisplayFor(modelItem => item.name) </div>
<p class="comment more" style ="white-space: pre-line; margin-top:0px;margin-bottom:0px; border-radius: 4px 4px 4px 4px; max-width :500px; min-height :5px; display :block; background-color: #CCCCFF"> @Html.DisplayFor(modelItem => item.comment) </p>
<p style="margin-top:2px;margin-bottom:0px;height:3px"> <input type="button" id="like" name="like" value="Like" style="color:blue;border:0px;background-color:inherit;cursor:pointer" /> <input type="button" class ="Reply" name="Reply" value="Replie(s)" style="margin-bottom:0px;color:blue;border:0px;background-color:inherit;cursor:pointer" /></p>
<div id="divReply" class ="divrep" style=" position:relative;left:50px; overflow:auto;margin-top:0px;margin-bottom:0px">
<table>
@foreach (var item2 in Model.Replies.Where(r => r.idrep == item.Id))
{
<tr >
<td >
<div style="font-weight:bold;"> @Html.DisplayFor(modelItem => item2.namerep) </div>
<p class="comment more" style ="margin-top:0px;margin-bottom:0px;white-space:pre-line; border-radius: 4px 4px 4px 4px; max-width :445px; min-height :5px; display :block; background-color: #CCCCFF;">@Html.DisplayFor(modelItem => item2.reply) </p>
</td>
</tr>
}
</table>
</td>
</tr>
}
</table>
Model.Repliesdata into separate observable-arrays . later inner foreach should have conditional data based onId. cheers - super cool