【问题标题】:content inside dom-repeat in Polymer 1.0?Polymer 1.0 中 dom-repeat 中的内容?
【发布时间】:2015-10-23 17:01:57
【问题描述】:

我的问题是我找不到使用<content> 标签绑定数据的方法。我想制作一个表格元素,您只需在其中传递一个标题数组和表示要使用的数据的对象数组。

我显示数据的代码如下:

<template is="dom-repeat" items="{{rows}}">
    <td>{{item.Id}}</td><td>{{item.Descripcion}}</td>
</template>

但如果我使用这种方式,我的表格元素只有在行具有 Id 或 Descriptcion 属性时才会起作用。
我想做这样的事情:

<template is="dom-repeat" items="{{rows}}">
    <content></content><!--dynamic item-->
</template>

所以,当我调用我的表格元素时,应该是这样的:

<my-table titles="[arrayOfTitles]" rows="[arrayOfRows]">
    <td>{{item.propertyOne}}</td><td>{{item.propertyTwo}}</td><td>{{item.propertyN}}</td>
</my-table>

&lt;td&gt;&lt;/td&gt; 将在其中包含要表示的数据。
这听起来很疯狂?有可能吗?

我尝试过使用getDistributedNodes 函数,但我不知道如何使用它来做我想做的事。对不起我的英语不好。谢谢!任何帮助都会很棒!

编辑

当我使用我的元素时,如下所示:

<my-table api-url="../../api/feeApi"
          headers='[{"title":"Id"},{"title":"Descripción"},{"title":"Abreviatura"},{"title":"Tipo"},{"title":"Monto($)"},{"title":"Cobrar a"}]'
          keys='["Id","Descripcion","ShortName","FeeType","Monto","NivelesEscolares"]'
          number-visible-rows="10">
</my-table>

现在,问题是如何使用更复杂的对象,当我使用对象或数组时,它们显示如下:
如您所见,当我使用对象或数组显示 [object Object] 时,您知道如何构造我的表声明以指示它是否有对象吗?我想做这样的事情:

我的&lt;tbody&gt;就像@so_confused_说的:

        <tbody>
            <template is="dom-repeat" items="{{visibleRows}}" id="tableRow">
                <tr on-tap="rowSelected" class$="{{getClass(item.active)}}">
                    <template is="dom-repeat" items="{{item.row}}" id="tableData">
                        <td>{{item}}</td>
                    </template>
                </tr>
            </template>
        </tbody>

【问题讨论】:

  • 您可以尝试获取 'item' 的值并绑定到该值
  • 我该怎么做@GlennVandeuren?

标签: javascript dom data-binding polymer


【解决方案1】:
        <tbody>
            <template is="dom-repeat" items="{{visibleRows}}" id="tableRow" as="row">
                <tr on-tap="rowSelected" class$="{{getClass(row.active)}}" style="cursor:cell;">
                    <template is="dom-repeat" items="{{headers}}" id="tableRow2" as="column">
                        <template is="dom-if" if="{{areEquals(column.type, 'object')}}">
                            <td>{{getObjectValue(column,row)}}</td>
                        </template>
                        <template is="dom-if" if="{{areEquals(column.type, 'array')}}">
                            <td>
                                <template is="dom-repeat" items="{{getDataArray(column,row)}}">
                                    <li>{{getArrayValue(item,column)}}</li>
                                </template>
                            </td>
                        </template>
                        <template is="dom-if" if="{{areEquals(column.type, 'text')}}">
                            <td>{{getValue(column,row)}}</td>
                        </template>
                    </template>
                </tr>
            </template>
        </tbody>

根据你调用相应函数的列类型。

【讨论】:

    【解决方案2】:

    如果您不是一直使用相同的键进行渲染,您可以将输入用作数组数组吗?

    如果你有一个数组:arr = [["one","two","three","four"],["one","two"],["one","two","three"]]; 那么你可以通过迭代嵌套数组来渲染不同长度的行

    <template is="dom-repeat" items="{{arr}}">
      <tr>
        <template is="dom-repeat" items="{{item}}>
          <td>{{item}}</td>
        </template>
      </tr>
    </template>
    

    【讨论】:

    • 实际上,这是我用来解决问题的第一种方法,但并没有完全说服我,所以我转向这个网站寻求帮助。谢谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多