var mappingSetting = {
'user' : {
create: function(options.data){
options.data.isActive = options.data.isActive == 1 ? 'Yes' : 'No';
options.data.startDate = $.datepicker.format('dd/MM/yy', options.data.startDate)
return options.data;
}
}
};
var viewModel = {};
$.ajax({
url: '/api/users/GetAll',
.....
success: functions (data) {
viewModel = ko.mapping.fromJS(data, mappingSetting);
ko.applyBindings(viewModel);
},
});
$("#savebtn").click(function(){
var dataToSave = ko.toJS(viewModel);
// ajax call to save
});
ajax结果
[{username: 'users1', isActive:1, startDate:'2001-03-22T00:00:00CET'}, {username: 'users2', isActive:0, startDate:'2008-03-22T00:00:00CET'}]
我必须在 isActive 的基础上显示是/否,但在保存时我必须将其更改回 0 / 1。
我有很多领域,还有日期(当地文化格式)。