ノックアウトjsの問題に直面しており、解決策が見つかりませんでした。オブザーバブル配列に配置してテーブルに表示するオブジェクトのリストがあり、行をクリックして、適切なフィールドにオブジェクト属性を入力します.
フィールドデータが更新されると、すぐにテーブルに反映されますが、[保存] ボタンをクリックしたときよりも、データベース内のデータを ajax を介して更新できるように、テーブル内のデータを更新する必要があります。簡単なフィドルは次の場所で作成されます: jsfiddle
Javascriptは次のとおりです。
var myData = [{'Id':1,'UserName':'Imran',Active:true},{'Id':2,'UserName':'Khan',Active:false},{'Id':3,'UserName':'Balouch',Active:true}];
function User(id,name,active)
{
this.Id = ko.observable(id);
this.UserName = ko.observable(name);
this.Active = ko.observable(active);
}
var viewModel = function () {
var self = this;
self.AllUsers = ko.observableArray([]);
self.CurrentUser = ko.observable(new User(-1,'',false));
var mappedUsers = $.map(myData, function (item) {
return new User(item.Id, item.UserName, item.Active);
});
self.AllUsers(mappedUsers);
self.GetUser = function(item)
{
ko.utils.arrayForEach(self.AllUsers(), function (mainItem) {
if (item.Id == mainItem.Id)
self.CurrentUser(mainItem);
});
};
// I want this function to update the table and also send Ajax call.
self.UpdateRecord = function()
{
//ko.utils.arrayForEach(self.AllUsers(), function (mainItem) {
// if (self.CurrentUser().Id() == mainItem.Id())
// mainItem.UserName = self.CurrentUser().UserName();
//});
};
};
$(function () {
ko.applyBindings(new viewModel());
});
htmlは次のとおりです。
<table style="width:100%;">
<tbody data-bind="foreach:AllUsers">
<tr data-bind="click:$root.GetUser">
<td data-bind="text:Id"></td>
<td data-bind="text:UserName"></td>
<td data-bind="text:Active"></td>
</tr>
</tbody>
</table>
<br/>
<input type="text" data-bind="value:CurrentUser().UserName"/>
<br/>
<button data-bind="click:UpdateRecord">Save</button>
どんな助けでも大歓迎です、事前に感謝します