knockout.js - Knockout select list value on change show/hide second select list -
i using mvc 5 , knockout create page names listed , against each name there selection list (uniform data rows). on 'applybinding' visible-binding , css-binding works on subsequent change of selection of list item modified rows not respect either binding. script is...
function names(data){ var self = this; self.id = ko.observable(data.id); self.name = ko.observable(data.name); self.reqstatus = ko.observable(data.reqstatus); self.rejreason = ko.observable(data.rejreason); self.showreason = ko.observale(false); self.reqstatus.subscribe(function(newvalue){ if (newvalue == 2){ self.showreason = true; } }); self.statuscss = ko.computed(function(){ if (self.reqstatus() == 2) { return "rejected"; }else if (self.showreason() == true){ return "rejected"; }else{ return "accepted"; } }); } function reason(data){ var self = this; self.id = ko.observable(data.id); self.text = ko.observable(data.text); } var reasons = ko.observablearray([]); $.getjson(....gets reason json....) function status(data){ var self = this; self.id = ko.observable(data.id); self.title = ko.observable(data.title); } var listitems = ko.observablearray([]); $.getjson(....gets status json....)
is used populate listitems array
function viewmodel(data){ var self = this; self.names = ko.observablearray(data); $getjson(...gets names json .....) - fetch , map data self.save = function(){ // code save data. jsonarray = ko.tojson(this.names); // here can see select list value change has been effected in observablearray of 'names'! } self.acceptcount = ko.computed(function(){ var acc = 0; ko.utils.arrayforeach(.... value); }); }; ko.applybindings(new viewmodel());
my html...
<div> <p> total accepted :<span data-bind="text: acceptcount"></span></p> <table> <tbody data-bind="foreach: names"> <tr data-bind="css: statuscss"> <td><span data-bind="text: name"></span></td> <td><select data-bind="options: listitems, optiontext: 'title', optionvalue:'id', value: reqstatus"></select> </td> <td><select data-bind="options: reasons, optiontext: 'text', optionvalue:'id', value: rejreason, visible: showreason" ></select></td> </tr> </tbody> </table> </div>
when load page; visible-binding , css-binding works great. 1 - problem when selected list item changed required visible-binding , css-binding not happen. however, when click on save button , convert array json, can see changes in selected list item there why not binding happening? 2 - when change select list item (the first one); there error if select value cause css change...
javascript runtime error: function expected
this error raised here
self.statuscss = ko.computed(function(){ if (self.reqstatus() == 2) { return "rejected"; }else if (self.showreason() == true){ // <--- error here return "rejected"; }else{ return "accepted"; } }
i have fixed problem minor mistake...in computed property accessing observed property more once , reason not returning proper values , script error raised. modified code is...
self.statuscss= ko.computed(function () { var selectedval = self.reqstatus(); if (selectedval) { if (selectedval === 2) { return "rejected"; } else if (selectedval === 7) { return "late"; } }; });
Comments
Post a Comment