Each row will have an 'Edit' button. On clicking this,
It should fill the row values in the respective textboxes below the table. eg: 'productName' will fill in txtName, etc.
Editing the text in the textbox will reflect the change in the table.
But the foll. code is not working :
<td>
<button data-bind="click: $parent.editProduct">Edit</button>
</td>
function Product(Name,Qty) {
pname = ko.observable(Name);
qty = ko.observable(Qty);
}
var ViewModel = function () {
var self = this;
self.products = ko.observableArray([{ pname: 'Mobile', qty: 5 },
{ pname: 'Car', qty: 1}]);
self.SelectedItem = ko.observable(new Product());
self.editProduct = function (item) {
self.SelectedItem(item);
};
};
ko.applyBindings(new ViewModel());
If I correctly understood your question. You need to implement editing array of data. I sketched for you an example:
var ViewModel = function() {
var $scope = this;
$scope.array = ko.observableArray([]);
$scope.array.push({ name: ko.observable('Ben'), lastName: ko.observable('Afleck'), editMode: ko.observable(false) });
$scope.array.push({ name: ko.observable('Tom'), lastName: ko.observable('Cruse'), editMode: ko.observable(false) });
$scope.toggleEdit = function(data) {
if (data.editMode()) {
data.editMode(false);
console.log(data.name());
console.log(data.lastName());
}
else
data.editMode(true);
};
return $scope;
};
var vm = new ViewModel();
ko.applyBindings(vm);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments