【问题标题】:Knockout Json Nested Children - Changing Select Menu to Radio/CheckboxesKnockout Json Nested Children - 将选择菜单更改为单选/复选框
【发布时间】:2017-01-07 06:32:37
【问题描述】:

我有一个工作版本的剔除代码,我在其中使用父选择菜单来确定子值应该是什么。

以下是示例: https://jsfiddle.net/mujaji/6zk5crn8/2/

我试图做的是将父级别变成收音机,将子级别变成复选框。

这是我尝试过的: 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"));

有人对从选择转换为单选/复选框有什么建议吗?

有没有更好的方法来做我正在尝试的事情?

【问题讨论】:

  • 您希望复选框如何实际工作?您将它们绑定到单个可观察对象,您是否希望它们看起来像复选框但仍然像单选按钮一样起作用?还是希望它们像多选一样发挥作用:将所有选中的值添加到数组中?
  • 啊!我明白你的意思了。理想情况下,我想将所有检查值添加到数组中
  • 这是已解决的收音机的更新小提琴:jsfiddle.net/mujaji/tju15joe/5
  • 为了进一步解释我的意图...如果选择了父收音机,则将子收音机显示为所选收音机的复选框
  • 查看我的更新答案

标签: jquery json knockout.js


【解决方案1】:

当使用foreach 绑定时,您正在创建一个新的绑定上下文(您可能还听说过它称为“作用域”)。后退时请务必使用$parent$root

你有一个看起来像这样的无线电绑定

<input type="radio" name="firstLevel" data-bind="checkedValue: value, checked: selectedLevelOne">

问题是selectedLevelOne 在父级上。您可以像这样修复此绑定:

<input type="radio" name="firstLevel" data-bind="checkedValue: value, checked: $parent.selectedLevelOne">

这将解决单选按钮。复选框是一个不同的问题,因为您要求进行多项选择。在这里,你可以使用array-binding behavior ofchecked绑定

<input type="checkbox" name="nextLevel" data-bind="checked: $parent.selectedLevelTwo, checkedValue: $data">

这是一个working fiddle,显示了单选按钮选择和填充数组的复选框。

一个问题是当父单选更改时,子选择数组不会自动更新。解决这个问题的一种方法是订阅(这在小提琴中)。

this.selectedLevelOne.subscribe(function(newValue) {
    this.selectedLevelTwo([])
}.bind(this))

【讨论】:

  • 所以我丢失的部分是 'checkedValue: $data'??
  • 您还缺少$parent
  • @JosephSjoblom 我用一个小错误修复了小提琴,用于更换收音机
  • 快速提问,我想在加载时进行选择并显示选择的文本。我让它工作了,但它给了我价值,而不是文字:jsfiddle.net/mujaji/tju15joe/9
  • 那是因为你绑定了这个值。如果需要,您可以绑定对象。检查这个小提琴。 jsfiddle.net/tju15joe/10。如果您需要对此进行解释,请提出另一个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 2016-10-26
  • 2011-11-05
  • 2023-03-12
  • 2013-05-05
  • 2017-04-18
相关资源
最近更新 更多