Polymer 1.0,如何实现无限嵌套dom-repeat

时间:2016-07-11 12:05:39

标签: javascript json polymer

我有以下JSON obj,

    categoryList = [{
    "title": "Computers",
    "categories": 
    [
      {
        "title": "Laptops",
        "categories": 
        [
          {
            "title": "Ultrabooks",
            "categories": 
            [
              {
                "title": "Lenovo",
                "categories": 
                [
                  {
                    "title": "i5 intel"
                  }
                ]
              }
            ]
          },
          {
            "title": "Dell"
          },
          {
            "title": "Macbooks",
            "categories": 
            [
              {
                "title": "Air"
              }, 
              {
                "title": "Pro"
              }
            ]
          }
        ]
      }, 
      {
        "title": "Desktops"
      }, 
      {
        "title": "Tablets",
        "categories": 
        [
          {
            "title": "Apple"
          }, 
          {
            "title": "Android"
          }
        ]
      }, 
      {
        "title": "Printers"
      }
    ]
  }]

正如您所看到的,每个类别都可以有一个子类别,因此它可以永远持续下去。我试图展示所有这些,但我无法弄清楚我将如何做到这一点。

这是我到目前为止所有这一切(显然这只是每个类别中的第一个孩子):

<template is="dom-repeat" items="{{categoryList}}" as="category">  
  <template is="dom-if" if="{{_hasData(category.categories)}}">
      <div>{{category.title}}</div>
      <template is="dom-repeat" items="{{category.categories}}" as="item">
         <div>{{item.title}}</div>
      </template>
  </template>
</template>

1 个答案:

答案 0 :(得分:1)

您可以在此处使用recursive code之类的内容。 Here's一个同样的掠夺者。

&#13;
&#13;
<script src="https://polygit.org/components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="https://polygit.org/components/polymer/polymer.html">
<dom-module id="infinite-repeat">
  <template>
    <template is="dom-repeat" items={{categories}} as="category">
      <div>{{category.title}}</div>
      <template is="dom-repeat" items="{{category.categories}}" as="item">
        <div>{{item.title}}</div>
        <template is="dom-if" if="{{_hasData(item.categories)}}">
          <infinite-repeat categories={{item.categories}}></infinite-repeat>
        </template>
      </template>
    </template>
  </template>
</dom-module>
<script>
  Polymer({
    is: 'infinite-repeat',
    properties: {
      categories: {
        type: Array,
        value: function() {
          return [{
            "title": "Computers",
            "categories": [{
              "title": "Laptops",
              "categories": [{
                "title": "Ultrabooks",
                "categories": [{
                  "title": "Lenovo",
                  "categories": [{
                    "title": "i5 intel"
                  }]
                }]
              }, {
                "title": "Dell"
              }, {
                "title": "Macbooks",
                "categories": [{
                  "title": "Air"
                }, {
                  "title": "Pro"
                }]
              }]
            }, {
              "title": "Desktops"
            }, {
              "title": "Tablets",
              "categories": [{
                "title": "Apple"
              }, {
                "title": "Android"
              }]
            }, {
              "title": "Printers"
            }]
          }];
        }
      }
    },
    _hasData: function(item) {
      return item && item.length > 0;
    }
  })
</script>

<infinite-repeat></infinite-repeat>
&#13;
&#13;
&#13;