ko.components.register('my-input', {
viewModel: InputModel,
template: {
element: 'input-template'
}
});
ko.components.register('my-select', {
viewModel: InputModel,
template: {
element: 'select-template'
}
});
ko.components.register('my-mu', {
viewModel: InputModel,
template: {
element: 'mu-template'
}
});
function InputModel(params) {
return params;
}
function Model() {
records = ko.observableArray([
[{
type: "input",
id: "Date",
size: "100px",
value: ko.observable()
}, {
type: "select",
id: "Weather",
size: "100px",
value: ko.observable(),
options: [{
optId: "w1",
optTxt: "Cloudy"
}, {
optId: "w2",
optTxt: "Sunny"
}, {
optId: "w3",
optTxt: "Rainy"
}, {
optId: "w4",
optTxt: "Snowy"
}, {
optId: "w5",
optTxt: "Foggy"
}]
}, {
type: "input",
id: "Lat",
size: "80px",
value: ko.observable()
}, {
type: "input",
id: "Long",
size: "80px",
value: ko.observable()
}],
[{
type: "input",
id: "Date",
size: "100px",
value: ko.observable()
}, {
type: "select",
id: "Temperature",
size: "120px",
value: ko.observable(),
options: [{
optId: "t0",
optTxt: "<-10"
}, {
optId: "t1",
optTxt: "]-10 : 0]"
}, {
optId: "t2",
optTxt: "]0 : 20]"
}, {
optId: "t3",
optTxt: "]20 : 40]"
}, {
optId: "t4",
optTxt: ">40"
}]
}, {
type: "select",
id: "Wind",
size: "70px",
value: ko.observable(),
options: [{
optId: "wind1",
optTxt: "Strong"
}, {
optId: "wind2",
optTxt: "Weak"
}]
}],
[{
type: "input",
id: "Date",
size: "100px",
value: ko.observable()
}, {
type: "select",
id: "Weather",
size: "100px",
value: ko.observable(),
options: [{
optId: "w1",
optTxt: "Cloudy"
}, {
optId: "w2",
optTxt: "Sunny"
}, {
optId: "w3",
optTxt: "Rainy"
}, {
optId: "w4",
optTxt: "Snowy"
}, {
optId: "w5",
optTxt: "Foggy"
}]
}, {
type: "input",
id: "Lat",
size: "80px",
value: ko.observable()
}, {
type: "input",
id: "Long",
size: "80px",
value: ko.observable()
}],
[{
type: "input",
id: "Date",
size: "100px",
value: ko.observable()
}, {
type: "select",
id: "Temperature",
size: "120px",
value: ko.observable(),
options: [{
optId: "t0",
optTxt: "<-10"
}, {
optId: "t1",
optTxt: "]-10 : 0]"
}, {
optId: "t2",
optTxt: "]0 : 20]"
}, {
optId: "t3",
optTxt: "]20 : 40]"
}, {
optId: "t4",
optTxt: ">40"
}]
}, {
type: "input",
id: "Humidity",
size: "70px",
value: ko.observable(),
options: [{
optId: "wind1",
optTxt: "Strong"
}, {
optId: "wind2",
optTxt: "Weak"
}]
}, {
type: "mu",
id: null,
value: '%'
}
]
]);
}
var myModel = new Model();
ko.applyBindings(myModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<template id="select-template">
<select data-bind="style: {width: size}, value: value, options: options, optionsText: 'optTxt', optionsValue: 'optId'"></select>
</template>
<template id="input-template">
<input type="text" data-bind="style: {width: size}, value: value" />
</template>
<template id="mu-template"> <span data-bind="text: value"></span>
</template>
<div data-bind="foreach: records">
<div data-bind="foreach: $data">
<label data-bind="text: id"></label>
<!-- ko component:{name:'my-'+type, params:$data} -->
<!-- /ko -->
</div>
</div>