javascript - Simple Polymer 1.0数据表

标签 javascript html templates polymer polymer-1.0

我正在尝试创建一个简单的 Polymer 组件,该组件根据数据数组呈现表格。所述组件的预期用途示例如下:

<my-table data="{{someArray}}">
  <my-column header="Id"><template>{{item.id}}</template></my-column>
  <my-column header="Name"><template>{{item.name}}</template></my-column>
</my-table>

渲染应该是这样的:

enter image description here

但是,在创建半成品原型(prototype)后,事情就变得复杂了。原型(prototype)可以在这里找到:http://jsbin.com/sirutusupu/edit?html,console,output . 免责声明:除非您下载它并通过本地http-server 运行它,否则它不会工作。

我的第一个问题:为什么原型(prototype)只能通过本地http-server 工作?

我的第二个问题:在本地运行时以及当我用dom-bind 包装自定义元素时,它也会停止工作。本地代码(也不起作用):

<template is="dom-bind">
  <my-table>
    <my-column header="Id"><template>{{item.id}}</template></my-column>
    <my-column header="Name"><template>{{item.name}}</template></my-column>
  </my-table>
</template>

我的第三个问题:使用函数格式化输出不起作用。考虑这个扩展示例:

<script>
  function concat(a, b) {
    return a + "-" + b;
  }
</script>
<my-table>
  <my-column header="Id"><template>{{item.id}}</template></my-column>
  <my-column header="Name"><template>{{item.name}}</template></my-column>
  <my-column header="Computed"><template>{{concat(item.id, item.name)}}</template></my-column>
</my-table>

产生的错误是 polymer.html:1660 [undefined::_annotatedComputationEffect]: compute method 'concat' not defined

有没有办法在不定义 Computed 绑定(bind)的情况下解决这个问题?否则单元格值的自定义格式是不可能的。

最佳答案

你这里有一个有趣的结构!

  • 我不知道为什么它在 jsbin 中对你不起作用,它可能有 与 rawgit 有关,我用 polygit 代替。
  • 如果将计算函数放在 模板化后列的 ctor.prototype
  • dom.bind 会弄乱内部 template,我会避免

这个似乎有效:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>My table</title>      
  <base href="//polygit.org/components/">
  <link rel="import" href="polymer/polymer.html" >     
</head>
<body>
  <my-table>
    <my-column header="Id"><template>{{item.id}}</template></my-column>
    <my-column header="Name"><template>{{item.name}}</template></my-column>
    <my-column header="Computed"><template>{{concat(item.id, item.name)}}</template>
  </my-table>
  
  <dom-module id="my-table">
    <template>
      <table border="1">
        <tr>
          <template is="dom-repeat" items="{{columns}}" as="column">
            <th>{{column.header}}</th>
          </template>
        </tr>
        <template is="dom-repeat" items="{{items}}" as="row">
          <tr>
            <template is="dom-repeat" items="{{columns}}" as="column">
              <td>
                <my-cell column="{{column}}" row="{{row}}"></my-cell>
              </td>
            </template>
          </tr>
        </template>
      </table>
    </template>
  </dom-module>
  
  <script>
    Polymer({
      is: 'my-table',
      ready: function() {
        this.columns = Array.prototype.slice.call(Polymer.dom(this).querySelectorAll('my-column'));
        this.items = [
          {id: 1, name: 'John'},
          {id: 2, name: 'Jane'},
        ];
      }
    });

    Polymer({
      is: 'my-column',
      properties: {
        header: String
      },
      ready: function() {
        this.templatize(Polymer.dom(this).querySelector('template'));
        this.ctor.prototype.concat = function(id, name) {
          return name + '(' + id + ')';
        }
      },
      stampCell: function(row) {
        return this.stamp({item: row});
      },
      behaviors: [Polymer.Templatizer]
    });

    Polymer({
      is: 'my-cell',
      ready: function() {
        Polymer.dom(this).appendChild(this.column.stampCell(this.row).root);
      }
    });
  </script>    
</body>
</html>

关于javascript - Simple Polymer 1.0数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35343213/

相关文章:

javascript - 在 php 页面上如何使用 mysql 在第 n 行之后插入行

尽管提到了 Lib,jQuery 脚本仍无法正常工作

c++ - 用于重新排序无符号整数的 Constexpr 变量模板

html - HTML5 主要元素的用途

html - 修改列表 CSS 代码

c++ - 模板参数推导/替换失败,lambda 作为函数指针

c++ - 在 Boost Phoenix 表达式中转换函数体

javascript - 从 Javascript 检测 flash 插件

javascript - 在按钮集中删除和添加类

javascript - 使用动态参数类型调用函数