Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate observable on click
    primarykey
    data
    text
    <p>I am facing an issue in knockout js and couldn't find any solution to it, I have a list of objects which I place in an observable array and display them in a table, clicking on a row, populates the object attributes in appropriate fields.</p> <p>When any field data is updated, it is immediately reflected in table but I want than when I click on Save button than the data must be updated in table so that I can also update that data in database through ajax. A simple fiddle is created at: <a href="http://jsfiddle.net/ikbalouch/R5yNM/2/" rel="nofollow" title="jsfiddle">jsfiddle</a></p> <p>Javascript is as:</p> <pre><code>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()); }); </code></pre> <p>and the html is as:</p> <pre><code>&lt;table style="width:100%;"&gt; &lt;tbody data-bind="foreach:AllUsers"&gt; &lt;tr data-bind="click:$root.GetUser"&gt; &lt;td data-bind="text:Id"&gt;&lt;/td&gt; &lt;td data-bind="text:UserName"&gt;&lt;/td&gt; &lt;td data-bind="text:Active"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;br/&gt; &lt;input type="text" data-bind="value:CurrentUser().UserName"/&gt; &lt;br/&gt; &lt;button data-bind="click:UpdateRecord"&gt;Save&lt;/button&gt; </code></pre> <p>Any help will be appreciated, thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload