gpt4 book ai didi

javascript - knockout foreach 绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-11-27 22:51:37 25 4
gpt4 key购买 nike

我正在尝试让 Knockout foreach 绑定(bind)在我的代码中工作。我以前已经做过很多次了,但在这个特殊情况下,我似乎无法做到正确。我一定在这里做错了什么,但我看不到。我在这里创建了一个jsfiddle:https://jsfiddle.net/9Lc144jv/

JavaScript:

var ReportModel = function (parent) {
var self = this;
self.parent = parent;
self.filters = ko.observableArray([]);

self.addFilter = function () {
self.filters.push({ logicOperator: 0, columnName: null, comparisonOperator: 0, value: null });
};

self.removeFilter = function () {
self.filters.remove(this);
};
};

var ViewModel = function () {
var self = this;
self.reportModel = false;

self.init = function () {
self.reportModel = new ReportModel(self);
};
};

var viewModel;
$(document).ready(function () {
viewModel = new ViewModel();
ko.applyBindings(viewModel);
viewModel.init();
});

HTML:

<body>
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th>Column</th>
<th>Operator</th>
<th>Value</th>
<th></th>
</tr>
</thead>
<tbody>
<!-- ko foreach: reportModel.filters -->
<tr>
<td><input type="text" class="form-control" data_bind="value: logicOperator" /></td>
<td><input type="text" class="form-control" data_bind="value: columnName" /></td>
<td><input type="text" class="form-control" data_bind="value: comparisonOperator" /></td>
<td><input type="text" class="form-control" data_bind="value: value" /></td>
<td>
<button type="button" class="btn btn-danger" data-bind="click: $parent.removeFilter">
Remove
</button>
</td>
</tr>
<!-- /ko -->
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: right;">
<button type="button" class="btn btn-primary" data-bind="click: reportModel.addFilter">
Add
</button>
</td>
</tr>
</tfoot>
</table>
</body>

更新

一些注意事项:

  1. 我使用了错误的属性:“data_bind”而不是“data-bind”。我现在更正了它,更新的代码在这里:https://jsfiddle.net/9Lc144jv/2/

  2. 即使我进行了修复,它仍然无法正常工作

  3. 当我将其复制并粘贴到纯 .html 文件并运行它时,我可以在 Firebug 中看到一些有趣的东西:

enter image description here

如您所见,在命令窗口中运行以下脚本显示在单击“添加”之前集合中没有任何内容,然后显示了单击“添加”之后的内容:

JSON.stringify(viewModel.reportModel.filters());

所以新对象位于可观察数组中,但它没有绑定(bind)到 foreach block 中的表。但为什么?从我看来,一切看起来都很好……但也许我需要用新的眼光来看待这一点。有人请告诉我我在这里做错了什么......

最佳答案

您正在绑定(bind)后设置reportModel,而且我必须将函数调用添加到buttons()。

轻微变化:

    var ReportModel = function (parent) {
var self = this;
self.parent = parent;
self.filters = ko.observableArray([]);

self.addFilter = function () {
self.filters.push({ logicOperator: 0, columnName: null, comparisonOperator: 0, value: null });
};

self.removeFilter = function () {
self.filters.remove(this);
};
};

var ViewModel = function () {
var self = this;
self.reportModel = new ReportModel(self);

self.init = function () {
console.info(self);
//self.reportModel = new ReportModel(self);
};
};

var viewModel;
$(document).ready(function () {
viewModel = new ViewModel();
ko.applyBindings(viewModel);
viewModel.init();
});

HTML

<body>
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th>Column</th>
<th>Operator</th>
<th>Value</th>
<th></th>
</tr>
</thead>
<tbody>
<!-- ko foreach: reportModel.filters -->
<tr>
<td><input type="text" class="form-control" data_bind="value: logicOperator" /></td>
<td><input type="text" class="form-control" data_bind="value: columnName" /></td>
<td><input type="text" class="form-control" data_bind="value: comparisonOperator" /></td>
<td><input type="text" class="form-control" data_bind="value: value" /></td>
<td>
<button type="button" class="btn btn-danger" data-bind="click: $parent.removeFilter()">
Remove
</button>
</td>
</tr>
<!-- /ko -->
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: right;">
<button type="button" class="btn btn-primary" data-bind="click: reportModel.addFilter()">
Add
</button>
</td>
</tr>
</tfoot>
</table>
</body>

https://jsfiddle.net/vw2kngqq/

关于javascript - knockout foreach 绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38004253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com