knockout.jsknockout-templating

creating knockout template for table row


I'm trying to create a knockout template that should generate a table row for each row in the knockout array. When I add the below code it tells me "element script cannot be nested inside element table"

<table>
<tr>
    <th>ID#</th>
    <th>Name</th>
</tr>
<tr data-bind="template: { name: 'EmployeeTemplate', foreach: EmployeesArray }"></tr>
<script type="text/html" id="EmployeeTemplate">
    <td>234567899874</td>
    <td>Mr. Test </td>
</script>

How can I resolve this using knockout templates? I'm basing it off this link.


Solution

  • <script type="text/html" id="templateName">
        <td data-bind="text: Property"></td>
        <td data-bind="text: Property"></td>
    </script>   
    
    <tbody data-bind="foreach: data">
      <tr data-bind="template: { name: 'templateName', data: $data }"></tr>
    </tbody>
    

    This should work.