我是Knockout JS的新手,我一直试图用Ajax请求的结果填充表没有成功。我遵循了一些教程和许多堆栈溢出问题,但是仍然没有得到所需的结果。
我现在遇到的错误是未定义我的可观察数组。这是我的JS代码:
<ul>
<li>
<NavLinkto={'/home'} activeClassName="selected">Home</NavLink>
</li>
<li>
<NavLinkto={'/story'} activeClassName="selectedSec">About us</NavLink>
</li>
</ul>
“未定义”变量是self.features可观察数组。
这是我要填写的HTML表格:
function FeatureRequest(data) {
if(data != null){
// console.log("Feature Request");
// console.log(data);
this.title = ko.observable(data.title);
this.description = ko.observable(data.description);
this.client_id = ko.observable(data.client_id);
this.client_priority = ko.observable(data.client_priority);
this.product_area_id = ko.observable(data.product_area_id);
this.user_id = ko.observable(data.user_id);
this.target_date = ko.observable(data.target_date);
this.ticket_url = ko.observable(data.ticket_url);
this.date_finished = ko.observable(data.date_finished);
}
}
function FeatureRequestViewModel() {
// Data
var self = this;
self.features = ko.observableArray();
console.log("Sending requests...");
$.getJSON("/api/obscure/request", function(response) {
var mappedFeatures = $.map(response, function(item) {
return new FeatureRequest(item)
});
self.features(mappedFeatures);
});
}
ko.cleanNode($("body")[0]);
var viewModel = new FeatureRequestViewModel();
ko.applyBindings(viewModel);
答案 0 :(得分:2)
在foreach: features
中,内部HTML的绑定数据是features
数组的元素。
通过从表格单元格中删除features.
前缀来固定代码。
答案 1 :(得分:2)
在foreach中:功能不使用功能。[属性],但仅使用[属性]。
<tbody id="featuresTable" data-bind="foreach:features">
<tr>
<td data-bind="text: title"> </td>
<td data-bind="text: description"> </td>
<td data-bind="text: client_id"> </td>
<td data-bind="text: client_priority"> </td>
<td data-bind="text: product_area_id"> </td>
<td data-bind="text: target_date"> </td>
<td data-bind="text: ticket_url"> </td>
<td data-bind="text: user_id"> </td>
<td class="single line">
<a class="edit ui icon violet button" value="id">
<i class="edit icon"> </i>
</a>
<a class="finish ui icon green button" value="id">
<i class="check icon"> </i>
</a>
<a class="delete ui icon red button" value=".id">
<i class="delete icon"> </i>
</a>
</td>
</tr>
</tbody>