如何访问淘汰模板的每个属性?

时间:2014-01-13 20:35:39

标签: knockout.js

我有以下knockoutJS模板:

<tbody data-bind="template: { name: 'Rows', foreach: model.items }">  </tbody>

及其使用:

<script type="text/html" id="Rows">
<tr>
    <td><select data-bind="options: $parent.model.Reasons, value: ReissueReason"></select></td>
    <td><input data-bind="value: ReissueRoad" class="lookUpRoads"></td>
    <td><input data-bind="value: HoursReissued"></td>
    <td><input data-bind="value: TimeRate"></td>
    <td><input data-bind="value: ReissuePaymentAmount"></td>
    <td><input data-bind="value: Comment" style="width: 300px"></td>
</tr>

在Server模型类中,我有一个创建三个“项目”的列表,以便模板在屏幕上有3行。 我正在尝试访问单个行数据,例如,当用户在HoursReissued输入框中输入值时,我需要编写一个触发onBlur的函数,并捕获该项类的信息。 (C# - models.item [0]) 我还需要能够根据onblur函数更新行中的其他字段 - 例如:ReissuePaymentAmount应根据HoursReissued和TimeRate字段的值自动更新。

1 个答案:

答案 0 :(得分:0)

查看ko.mapping插件,以便为数据添加行为:

例如:

function ItemRow(data) {
    ko.mapping.fromJS(data, {}, this);
    //a computed will automatically update whenever any of it's dependencies change.
    this.ReissuePaymentAmount = ko.computed(function () {
        return this.HoursReissued() * this.TimeRate();
    }, this);
}
var mapping = {
    'items': {
        create: function(options) {
            return new ItemRow(options.data);
        }
    }
}
var viewModel = ko.mapping.fromJS(data, mapping);

另见这个堆栈溢出问题,它基本上是一回事。 Inserting a knockout computed observable in an observableArray that is generated with the ko.mapping plugin

相关问题