1

我有一个包含 3 个部分的 div。

<div>
    <section> /--bind table Table.html template with HR Details--/</section>
    <section> /--bind table Table.html template with Developers--/</section>
    <section> /--bind table Table.html template with Managers--/</section>
</div>

我有一个表格模板,即 Table.html

<table id="emptable">
    <thead>
        <tr>
            <th>Name</th>
            <th>Designation</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: EmpDetails">
        <tr>
            <td style="width:25%" data-bind="text: Name"></td>
            <td style="width:25%" data-bind="text: Designation"></td>
        </tr>        
    </tbody>
</table>

我想在所有 3 个部分中使用同一张表,并使用不同的 observables。例如:我的 Obserevables 是 HRDetails、Develeopers、Managers。我想在具有不同观察值的所有部分中使用相同的表结构。请建议我如何在knockoutjs中做到这一点。

4

1 回答 1

2

怎么样?

function ViewModel() {
    var self = this;

    self.HRDetails = ko.observable(/* ... */);
    self.Develeopers = ko.observable(/* ... */);
    self.Managers = ko.observable(/* ... */);

    self.sections = [self.HRDetails, self.Develeopers, self.Managers];
}

<div data-bind="foreach: sections">
    <section data-bind="template: {name: 'yourTableTemplate'}">
    </section>
</div>
于 2013-05-27T10:45:45.137 回答