Hi all,
i do have a requirement that to perform an operation when checkbox is clicked using knockout js. In my scenario initially i am displaying 8 lines of design code as same by creating 8 objects to that corresponding entity.
so when ever user enteres 1st line data i need to get those values and placed into remaining 7 lines fields respectively. so for that i have taken one checkbox at the top and when it is clicked i have written corresponding code in the click event of checkbox.
it is working fine but when ever again checked i need to remove the data from all 7 lines where ever the data is present. my code is as follows.
Design Code:-
<input type="checkbox" id="checkboxid" data-bind="click: ApplyToAll,enable: EnableApplyAll" />
In corresponding Model:-
var self = this;
self.chkValue = ko.observable(false);
self.lines = ko.observableArray([new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine()]);
self.EnableApplyAll = ko.computed(function () {
var h = self.lines()[0].Height();
var l = self.lines()[0].Length();
return h != "" && l != "";
});
self.ApplyToAll = function () {
self.chkValue(!self.chkValue());
if (self.chkValue()) {
var h;
var l;
var t;
var m;
$.each(self.lines(), function (ind, ln) {
if (ind == 0) {
h = ln.Height();
l = ln.Length();
t = ln.strThick();
m = ln.strUnits();
}
else if (ind != 0) {
ln.Height(h);
ln.Length(l);
ln.strThick(t);
ln.strUnits(m);
if (ind == 7) {
self.chkValue(!self.chkValue());
}
}
});
}
else {
$.each(self.lines(), function (ind, ln) {
if (ind != 0) {
ln.Height("");
ln.Length("");
ln.strUnits("");
ln.strThick("");
}
});
self.chkValue(!self.chkValue());
}
};
};
but when ever again checked it is not coming into else part, and the data is not removing. please help me to solve this, or did i write any mistake in my code, plz let me know.