我从数据库中检索表格数据。由于该表是在非友好标签中设计的,因此我必须添加一个新对象(HeaderName)来存储从另一个引用中检索的友好名称。
我正在尝试将 ko.observable 中的原始数据替换为我之前为表格列显示检索到的标题。
是否可以使用 ko.mapping 函数对其进行映射?没有循环它?我想知道 ko.mapping ,关键功能..试图理解它。
使用此示例 -> ( http://jsfiddle.net/nEaSJ/ ) 数据通过“孩子”进行映射:
在我的情况下,我有 prepName 需要映射到 ColumnName
下面是我认为应该进行操作的功能中的 2 个。在我在 UI 上迭代它之前
var label = function (data) {
this.label = ko.observable(data);
//console.log(data); //sample value = Cnv_Id
var labeldesc = ko.mapping.fromJS(data, {}, vm.columns);
console.dir(labeldesc); //Should return me "Count Numeral Variable Identification"
}
//vm.columns (ko.observable)
//vm.columns=[{"$id":"1","ColumnName":"Id","system_type_id":56,"primaryCol":1,"__ko_mapping__":{"ignore":[],"include":["_destroy"],"copy":[],"observe":[],"mappedProperties":{"$id":true,"ColumnName":true,"system_type_id":true,"primaryCol":true},"copiedProperties":{}},"isVisible":true,"HeaderName":"Id","SystemField":-1,"Visible":true}
//,{"$id":"2","ColumnName":"Cnv_Id","system_type_id":231,"primaryCol":0,"__ko_mapping__":{"ignore":[],"include":["_destroy"],"copy":[],"observe":[],"mappedProperties":{"$id":true,"ColumnName":true,"system_type_id":true,"primaryCol":true},"copiedProperties":{}},"isVisible":true,"HeaderName":"Count Numeral Variable Id","SystemField":-1,"Visible":true},
//{"$id":"3","ColumnName":"Cox_No","system_type_id":175,"primaryCol":0,"__ko_mapping__":{"ignore":[],"include":["_destroy"],"copy":[],"observe":[],"mappedProperties":{"$id":true,"ColumnName":true,"system_type_id":true,"primaryCol":true},"copiedProperties":{}},"isVisible":true,"HeaderName":"Cox No","SystemField":-1,"Visible":true},
//{"$id":"4","ColumnName":"CX_Name","system_type_id":175,"primaryCol":0,"__ko_mapping__":{"ignore":[],"include":["_destroy"],"copy":[],"observe":[],"mappedProperties":{"$id":true,"ColumnName":true,"system_type_id":true,"primaryCol":true},"copiedProperties":{}},"isVisible":true,"HeaderName":"CX Name","SystemField":-1,"Visible":true}]
//data input for bindSelectedModel
//data = [{"propName":"$id","propValue":"2"},{"propName":"Id","propValue":5421},{"propName":"Cnv_Id","propValue":"CV0001-1-1"},
{"propName":"Cox_No","propValue":"12"},{"propName":"CX_Name","propValue":"Exz"}]
function bindSelectedModel(data) {
var labelmapping = {
'propName': {
create: function (options) {
console.log('mapping:', options.data);
return new label(options.data);
}
}
}
vm.selectedItemModel = ko.mapping.fromJS(data, labelmapping);
vm.selectedItemHeader = vm.columns();
}