Simple Polymer 1.0 data table

拟墨画扇 提交于 2019-11-29 16:51:46

You have an interesting construction here!

  • I don't know why it doesn't work for you in the jsbin, it might have something to do with rawgit, I used polygit instead.
  • Formatting will work if you put the computation function on the column's ctor.prototype after Templatizing.
  • dom.bind is going to mess up the interior template, I would avoid it.

This one seems to work:

<!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>

I think one problem you have is the definition of columns. It is OUTSIDE of the properties object

It should look like this

Polymer({
  is: 'my-table',
  properties: {
    items: Array,
    columns: {
      type: Array,
      value: function(){return [];}
    }

  },
  ready: function() {
    this.columns = Polymer.dom(this).querySelectorAll('my-column');

    this.items = [
      {id: 1, name: 'John'},
      {id: 2, name: 'Jane'},
    ];
  },
});

The second issue is the use of <template> inside the <template is="dom-bind"> which is why it stops working. Not sure why you are doing that.

When you say local http-server how is that configured?. It is often necessary to make sure that the imports are referencing the right place when then have hrefs like ../polymer/polymer.html The tools that Polymer provides (such as polyserve and web-component-tester) all map bower_components is special ways so you can refer to the elements with the ../ style. Are you using one of their local http-server or something you crafted? You can do it with other web servers (like apache or ngnix) you just have to realise that you need to change the url mapping to file directory mapping so it works.

EDIT

Right at the bottom of your jsbin you do this

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

However your <my-table> element doesn't use the <content> tag which is what will pull that content in. I am not sure its necessary - I thought your top level element template for <my-table> already contains the columns.

The solution for question #2 (data binding does not work when the component is wrapped by dom-bind) is the following. You need to implement the following methods, as defined by the Templatizer behaviour:

  • _forwardParentProp: function(prop, value)
  • _forwardParentPath: function(path, value)
  • _showHideChildren: function(shouldHide)

You also need to set the _instanceProps to define any instance-related properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!