私は親の選択メニューを使って子の値を決定するノックアウトコードの作業バージョンを持っています。 https://jsfiddle.net/mujaji/6zk5crn8/2/ノックアウトJsonネストされた子供 - ラジオ/チェックボックスにメニューを変更する
私は何をしようとしてることはラジオやチェックボックスに子レベルに親レベルを回すです:
は、ここでの例です。ここで
は、私が試したものです: はhttps://jsfiddle.net/mujaji/tju15joe/4/
function ViewModel(items) {
this.levelOne = ko.observableArray(items);
this.selectedLevelOne = ko.observable();
this.selectedLevelTwo = ko.observable();
function getById(items, value) {
if(!value) {
return [];
}
var result = ko.utils.arrayFirst(items, function(item) {
return item.value === value;
});
return result && result.childItems || [];
}
this.levelTwo = ko.computed(function(){
var items = this.levelOne();
var id = this.selectedLevelOne()
return getById(items, id);
}, this);
}
var items = [
{text: "Marvel Superhero", value: "1", childItems: [
{text: 'Captain America', value: '01'},
{text: 'Ironman', value: '02'},
{text: 'Wolverine', value: '03'},
{text: 'Nightcrawler', value: '04'},
{text: 'Spiderman', value: '05'},
{text: 'Hulk', value: '06'},
{text: 'Thor', value: '07'},
{text: 'Hawkeye', value: '08'},
{text: 'Silver Surfer', value: '09'},
{text: 'The Thing', value: '10'},
{text: 'Mr Fantastic', value: '11'}
]},
{text: "Marvel Villian", value: "2", childItems: [
{text: 'Dr. Doom', value: '01'},
{text: 'Magneto', value: '02'},
{text: 'Juggernaut', value: '03'},
{text: 'The Blob', value: '04'},
{text: 'Galactus', value: '05'}
]},
{text: "DC Superhero", value: "3", childItems: [
{text: 'Superman', value: '01'},
{text: 'Batman', value: '02'},
{text: 'Robin', value: '03'},
{text: 'Wonder Woman', value: '04'},
{text: 'Raven', value: '05'},
{text: 'Aquaman', value: '06'}
]},
{text: "DC Villian", value: "4", childItems: [
{text: 'Lex Luthor', value: '01'},
{text: 'Joker', value: '02'},
{text: 'Grundy', value: '03'},
{text: 'The Riddler', value: '04'},
{text: 'Lobo', value: '05'}
]}
];
var module = {};
module.sampleViewModel = new ViewModel(items);
ko.applyBindings(module.sampleViewModel, document.getElementById("override-more"));
は誰でもラジオ/チェックボックスを選択からの変換に何かアドバイスはありますか?
私が試していることを行うには良い方法はありますか?
どのようにチェックボックスを実際に動作させますか?単一の観測値に対してバインドしていますが、ラジオボタンのように機能しますが、チェックボックスのように見えますか?または、それらをmutli-selectのように機能させたいのですか?チェックされたすべての値を配列に追加しますか? – Tyrsius
ああ!私はあなたが何を意味するかを見ます。理想的には、すべてのチェック値を配列に追加したいと思います –
ラジオで更新されたフィドルがあります:https://jsfiddle.net/mujaji/tju15joe/5/ –