Riposte
Riposte

Reputation: 51

Vaadin-Grid Polymer Element migration from cell renderer

In Polymer 1.0 and Vaadin-grid v1 I was using a cell renderer along the following lines to add an icon based on the value of the data:

grid.columns[6].renderer = function(cell) {
      if (cell.data < 0){
        cell.element.innerHTML =  Math.abs(cell.data) + '<iron-icon icon="arrow-downward" style="color: red"/>';
      }
      else if (cell.data > 0) {
        cell.element.innerHTML = cell.data + '<iron-icon icon="arrow-upward" style="color: green"/>';
      }
      else {cell.element.innerHTML = '<iron-icon icon="settings-ethernet" style="color: #ffcc00"/>';}
    };

Of course the migration to vaadin-grid 2 means no more renderer function and recommendation to use templates instead.What would be the most efficient way to achieve this?

Any help appreciated - I'm kind of learning this stuff as I go along and the examples on the vaadin site assume a bit more expertise than I have !

Upvotes: 1

Views: 103

Answers (1)

Riposte
Riposte

Reputation: 51

Tomi Virkki over on the Vaadin forum was kind enough to post me a simple example using computed bindings.Seems obvious now it has been explained .. gotta learn somehow I guess! Thanks Tomi

 <vaadin-grid items="[[users.result]]">

    <vaadin-grid-column width="50px" flex-grow="0">
      <template class="header">#</template>
      <template>
        <iron-icon icon="[[_itemIcon(item)]]" style$="[[_itemIconStyles(item)]]"/>
      </template>
    </vaadin-grid-column>

    <vaadin-grid-column>
      <template class="header">First Name</template>
      <template>[[item.firstName]]</template>
    </vaadin-grid-column>

    <vaadin-grid-column>
      <template class="header">Last Name</template>
      <template>[[item.lastName]]</template>
    </vaadin-grid-column>

  </vaadin-grid>


</template>
<script>
  window.addEventListener('WebComponentsReady', () => {
    class XGrid extends Polymer.Element {

      static get is() {
        return 'x-grid';
      }

      _itemIcon(item) {
        return item.firstName > 'N' ? 'arrow-upward' : 'arrow-downward';
      }

      _itemIconStyles(item) {
        return `color: ${item.firstName > 'N' ? 'green' : 'red'}`;
      }

    }
    customElements.define('x-grid', XGrid);

Upvotes: 1

Related Questions