我有一个带有可观察项目和一个按钮的 observableArray。当我单击按钮启动 ajax 请求时,我需要更新按钮相同行上的 velues。
<div id="list" data-bind="foreach: Values">
Name <span data-bind="text: Name"></span>
Category <span data-bind="text: Category"></span>
Index <span data-bind="text: $index"></span>
Price <span data-bind="text: Price"></span>
<a id="button" href="#" data-bind="visible: Price > 0, click: $parent.read">read data</a><br />
</div>
<script type="text/javascript">
var TRow = function () {
this.Id = ko.observable(0);
this.Price = ko.observable(0);
this.Name = ko.observable("");
this.Category = ko.observable("");
}
var TList = function () {
var self = this;
this.Values = ko.observableArray([new TRow()]);
this.read = function (data) {
readdata(self, data);
}
}
var List = new TList();
ko.applyBindings(List, document.getElementById("list"));
function readdata(vm, row) {
$.ajax({
type: "GET",
url: "/api/test/" + row.Id,
contentType: "application/json;charset=utf-8",
dataType: 'json',
success: function (data, textStatus, xhr) {
//How I can update Id, Price...???
//It's possible to use "ko.mapping.fromJS(data.Value, {}, vm);" ???
...
},
error: function (xhr, textStatus, errorThrown) {
console.log("Error: " + textStatus + " - " + errorThrown + " - " + xhr.responseText);
}
});
}
</script>
谢谢