Knockout js 添加一个新列表
Knockout js add a new list
我正在尝试创建一个带有列表的表单,该列表可以动态添加列表!
但是,我只是无法让我的添加函数工作,并且它一直抱怨我的列表未定义。
这是我在 jave 脚本中的视图模型
function DeliveryCategoryViewModel() {
var self = this;
self.DeliveryCategory = ko.observable(new DeliveryCategory(1, "new Name", [new DeliveryOption("first"), new DeliveryOption("second")]));
self.addDeliveryOptions = function () {
self.DeliveryOptions.push(new DeliveryOption("new Option"));
}
self.removeDeliveryOptions = function (option) {
self.DeliveryCategory.remove(option);
}
}
这些是保存数据的实际模型
function DeliveryCategory(id, name, option) {
this.Id = id;
this.Name = name;
this.DeliveryOptions = ko.observableArray(option);
}
function DeliveryOption(name) {
this.Id = "2";
this.Name = name;
}
$(document).ready(function () {
ko.applyBindings(new DeliveryCategoryViewModel());
});
这是我的表格
<div id="newDelCategory">
<input data-bind="value:DeliveryCategory().Id" type="hidden" />
<label class="newDelCategoryLabel">New delivery category name: </label>
<input type="text" data-bind="value:DeliveryCategory().Name" class="newDelCategoryText" id="newDelCategoryText" placeholder="Delivery category name" />
</div>
<div id="addOption">
<a id="addOptionLink" href="#" data-bind="click:addDeliveryOptions" class="link">+Add delivery option</a>
</div>
<div id="deliveryOptionContent" data-bind="foreach: DeliveryCategory().DeliveryOptions">
<div class="newDelOption">
<input data-bind="value:$data.Id" type="hidden" />
<div class="divider"></div>
<label class="newDelOptionLabel">New delivery option name: </label>
<input type="text" data-bind="value:$data.Name" class="categoryName" id="newDelOptionText" placeholder="Delivery option name" />
<a id="removeOptionLink" data-bind="click:$parent.removeDeliveryOptions" class="link removeOptionLink">Remove</a>
</div>
</div>
当我尝试单击click:addDeliveryOptions时,它会返回Firebug控制台。
TypeError: self.DeliveryCategory.DeliveryOptions is undefined
self.DeliveryCategory.DeliveryOptions.push(new DeliveryOption("new Option"));
我尝试了不同的东西,例如click:$root.addDeliveryOptions,并且还尝试添加addDeliveryOptions函数作为原型(例如DeliveryCategory.prototype.addDeliveryOptions(...)),但仍然得到Typeerror...
是因为交付选项未初始化吗?我预计这将是当交付类别从交付类别视图模型()初始化时...
知道吗?谢谢!
小监督。轻松修复。
您调用了push
并从视图模型中remove
了可观察数组,但它不作为视图模型的直接成员存在。
这是因为您永远不会将可观察数组直接添加到此视图模型。使用构造函数创建要与 DeliveryCategory
一起观察的对象。该对象上的属性之一是可观察数组DeliveryOptions
。若要从此范围访问可观察数组,必须在运行任何数组方法之前评估DeliveryCategory
以访问其属性DeliveryOptions
。所以,取而代之的是:
self.addDeliveryOptions = function () {
self.DeliveryOptions.push(new DeliveryOption("new Option"));
}
self.removeDeliveryOptions = function (option) {
self.DeliveryCategory.remove(option);
}
解决方案:
self.addDeliveryOptions = function() {
self.DeliveryCategory().DeliveryOptions.push(new DeliveryOption("new Option"));
}
self.removeDeliveryOptions = function(option) {
self.DeliveryCategory().DeliveryOptions.remove(option);
}
请参阅下面的代码片段
function DeliveryCategoryViewModel() {
var self = this;
self.DeliveryCategory = ko.observable(new DeliveryCategory(1, "new Name", [new DeliveryOption("first"), new DeliveryOption("second")]));
self.addDeliveryOptions = function() {
self.DeliveryCategory().DeliveryOptions.push(new DeliveryOption("new Option"));
}
self.removeDeliveryOptions = function(option) {
self.DeliveryCategory().DeliveryOptions.remove(option);
}
}
function DeliveryCategory(id, name, option) {
this.Id = id;
this.Name = name;
this.DeliveryOptions = ko.observableArray(option);
}
function DeliveryOption(name) {
this.Id = "2";
this.Name = name;
}
$(document).ready(function() {
ko.applyBindings(new DeliveryCategoryViewModel());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="newDelCategory">
<input data-bind="value:DeliveryCategory().Id" type="hidden" />
<label class="newDelCategoryLabel">New delivery category name:</label>
<input type="text" data-bind="value:DeliveryCategory().Name" class="newDelCategoryText" id="newDelCategoryText" placeholder="Delivery category name" />
</div>
<div id="addOption">
<a id="addOptionLink" href="#" data-bind="click:addDeliveryOptions" class="link">+Add delivery option</a>
</div>
<div id="deliveryOptionContent" data-bind="foreach: DeliveryCategory().DeliveryOptions">
<div class="newDelOption">
<input data-bind="value:$data.Id" type="hidden" />
<div class="divider"></div>
<label class="newDelOptionLabel">New delivery option name:</label>
<input type="text" data-bind="value:$data.Name" class="categoryName" id="newDelOptionText" placeholder="Delivery option name" />
<a href="#" id="removeOptionLink" data-bind="click:$parent.removeDeliveryOptions" class="link removeOptionLink">Remove</a>
</div>
</div>
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- javascript处理一个对象数组以获得一个新的对象数组
- 为什么我们在ES2015中需要一个新的for循环结构,而我们已经有了for、forEach
- 在Twitter BootStrap嵌套下拉列表中托管一个选择列表(IE 9特定)
- 如何通过Primefaces3.5使用JSF2.0验证并在成功的情况下打开一个新的选项卡
- 如何拆分一个整数和产生的所有数字来创建一个新数字
- 一个按钮,点击后会生成一个新的文本框?-Javascript
- 如何打开一个新窗口或选项卡,并将其提供给javascript执行
- window.open:是否可以通过修改DOM来打开一个新窗口
- 返回一个新函数,而不是工厂中的对象
- Knockout js 添加一个新列表
- 在 jQuery 中迭代数组时,每隔一个元素创建新列表
- 创建一个新的网站列表页面后,我如何向它添加值
- 如何刷新我的项目列表在winapp当我插入一个新的数据在sqlite (javascript &html)
- 单击list item将创建一个具有原始列表项'文本内容的新列表项
- 如何删除选择标签的所有选项,并在javascript中分配一个新的项目列表来选择选项
- 我如何从url's的列表中打开一个随机的url在一个新的时间每次点击它
- 通过id字段过滤数组列表到一个新变量
- 自定义Javascript在drupal中的加载顺序;向列表中添加一个新的JS
- jQuery:我应该如何构建一个新的列表's由jQuery生成