Recursive template with knockout js

后端 未结 4 510
逝去的感伤
逝去的感伤 2020-12-09 02:03

Is it possible to create a recursive template only with knockout js?

I have a knockout object:

function FormElementNode(children, text, value) {
   v         


        
4条回答
  •  我在风中等你
    2020-12-09 02:12

    This post was a great help to me. I am always finding new ways to use knockout. I just wanted to add one useful modification which is doing exactly what nemesv proposed only using the ko.mapping plugin.

    //Nested javascript object:
    var formElementNode = {
        children: [{
            children: [],
            text: 'Child1',
            value: 'Value1'
        }, {
            children: [{
                children: [{
                    children: [],
                    text: 'Child2.1.1',
                    value: 'Value2.1.1'
                }],
                text: 'Child2.1',
                value: 'Value2.1'
            }],
            text: 'Child2',
            value: 'Value2'
        }, {
            children: [],
            text: 'Child3',
                value: 'Value3'
        }],
        text: 'Main',
        value: 'MainValue'
    };
    
    //Use ko.mapping to generate viewModel:
    var viewModel = ko.mapping.fromJS(formElementNode);
    ko.applyBindings(viewModel);
    

    As demonstrated in this jsFiddle.

提交回复
热议问题