javascript - 在knockoutJs中使用父ID的嵌套菜单

标签 javascript jquery json knockout.js

我尝试使用客户端给定的 json 数据创建嵌套菜单。

数据:

var serverData = [
      {
        Id: "menuColorSearch",
        Text: "Color search"
      },
      {
        Id: "menuAncillaryProductMix",
        Text: "Ancillary product mix"
      },
      {
        Id: "menuDocuments",
        Text: "Documents"
      },
      {
        Id: "menuColorInfo",
        ParentId: "menuDocuments",
        Text: "Color info"
      },
      {
        Id: "menuReports",
        ParentId: "menuDocuments",
        Text: "Reports"
      },
      {
        Id: "menuMaintenance",
        Text: "Maintenance"
      },
      {
        Id: "menuPriceManagement",
        ParentId: "menuMaintenance",
        Text: "Price management"
      }
    ];

我正在尝试这样:

var Menu = function(dept, all) {
    var self = this;
    this.id = dept.Id;
    this.name = ko.observable(dept.Text);
    this.parentId = dept.ParentId;
    this.children = ko.observableArray();

    ko.utils.arrayForEach(all || [], function(menu) {

        if(menu.ParentId){
            if (menu.ParentId === self.id) {
                self.children.push(new Menu(menu, all));
            }
        }else{
            new Menu(menu, all)
        }
    });
};

var ViewModel = function(data) {
    this.root = new Menu(data[0], data);
};


$(function() {
    ko.applyBindings(new ViewModel(serverData));
});

模板:

<div data-bind="with: root">
    <ul data-bind="template: 'deptTmpl'">
    </ul>
</div>

<script id="deptTmpl" type="text/html">
    <li>
        <a data-bind="text: name"></a>
        <ul data-bind="template: { name: 'deptTmpl', foreach: children }">
        </ul>
    </li>
</script>

问题是它只有在第二个和第三个对象具有父 ID 时才起作用。我正在尝试类似的方法,它应该根据给定的 json 数据制作嵌套菜单。所以 id 对象上没有父 id,它应该添加到根上。如果对象有父 ID,则应根据父 ID 添加。

请帮助我更正我的代码,或者如果这是在 KnockoutJS 中执行此操作的另一种方法,请指导我。

谢谢

最佳答案

这应该对您有帮助 http://jsfiddle.net/MCNK8/3/ ,主要思想是通过将子级放置在父级中来重建主数据数组

HTML

<script id="nodeTempl" type="text/html">
    <li>
        <a data-bind="text: Text"></a>
        <ul data-bind="template: {name: nodeTemplate, foreach: children }"></ul>
    </li>
</script>

<script id="nodeLeafTempl" type="text/html">
    <li>
        <a data-bind="text: Text"></a>
    </li>
</script>

<ul data-bind="template: {name: nodeTemplate, foreach: children }"></ul>

Javascript(@see fiddle)

var serverData = [
      {
        Id: "menuColorSearch",
        Text: "Color search"
      },
      {
        Id: "menuAncillaryProductMix",
          ParentId: 'menuColorSearch',
        Text: "Ancillary product mix"
      },
      {
        Id: "menuDocuments",
        Text: "Documents"
      },
      {
        Id: "menuColorInfo",
        ParentId: "menuReports",
        Text: "Color info"
      },
      {
        Id: "menuReports",
        ParentId: "menuDocuments",
        Text: "Reports"
      },
      {
        Id: "menuMaintenance",
          ParentId: 'menuReports',
        Text: "Maintenance"
      },
      {
        Id: "menuPriceManagement",
        ParentId: "menuMaintenance",
        Text: "Price management"
      }
    ];



function getNestedMenu(index, all) {
    var root = all[index];

    if(!root){
        return all;
    }

    if(!all[index].children){
        all[index].children = [];
    }

    for(var i = 0; i < all.length; i++){
        //<infinity nesting?>

        //put children inside it's parent
        if(all[index].Id == all[i].ParentId){
            all[index].children.push(all[i]);
            all[i].used = true;
        }

        //this is needed for each item, to determine which template to use        
        all[index].nodeTemplate = function(node) {
            return node.children.length > 0 ? 'nodeTempl' : 'nodeLeafTempl';
        }
        //</infinity nesting?>
    }

    return getNestedMenu(++index, all);
};

function getModel(data) {
    var items = getNestedMenu(0, data);

    //<remove duplicates, for infinity nesting only>   
    for(var i = 0; i < items.length; i++){
        if(items[i].used){
            items.splice(i, 1);
            i--;
        }
    }
    //</remove duplicates, for infinity nesting only>
    //<build root item>
    var model = {};
    model.children     = ko.observableArray(items);
    model.nodeTemplate = function(node) {
        return node.children.length > 0 ? 'nodeTempl' : 'nodeLeafTempl';
    }
    //</build root item>
    console.log(items);
    return model;
};


(function() {
    //new ViewModel(serverData);
    ko.applyBindings(getModel(serverData));
})();

关于javascript - 在knockoutJs中使用父ID的嵌套菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19695129/

相关文章:

javascript - 如何在我的网站中用户缩放(CTRL +'+')时修复字体大小

javascript - jQuery 查找每个子项并访问子项

javascript - 如何测试这个组件/模板? :)

javascript - 为什么我在 Canvas 上创建的矩形没有放在正确的位置?

jquery - 表单和输入未显示在 Bootstrap 弹出窗口中

javascript - 执行另一台服务器上的脚本

php - 无法在javascript中取消转义JSON内容

java - 如何按照 JSR-353 (JSON-P) 规范克隆 JsonObject(阴影和深度)

json - json 的 Angular 5 [object Object] 错误

javascript - Visual Studio 任务运行器 "SyntaxError: Use of const in strict mode."