dynamic columns in vaadin-grid

Dear all

i’m trying to use your cool vaadin-grid element, but i’d like to use is creating dynamic grid based on the JSON obtained from the server.

So i’m trying to create both cols and items with javascript (this is the first step, then i’ll use JSON result instead of static arrays) but is not working:

[code]

[/code]The same code works properly if cols element are static:

[code]

[/code]Any help/idea?

Thanks to all in advance!

Hi

Currently the

element configurator is meant for static config only. We’ve been experimenting with dynamically replacing/mutating the table element but with templates especially things might get tricky.

If you need to modify the columns dynamically, easiest way now is to change the grid.columns array (preferably trough Polymer APIs) or use grid.addColumn/removeColumn APIs.

Tomi

Thanks!

ps: where can i found the Polymer APIs of the element?

Thanks

sorry… found!

https://vaadin.com/docs/-/part/elements/vaadin-grid/columns.html

Thanks!

I’ve have a very similar requirement but have been unable to create a working dynamic equivalent of the static code above. I can add columns to an already-rendered grid with .addColumn() but I can’t create a completely empty grid and then create all its columns dynamically. I also still don’t understand how to change grid.columns via the Polymer APIs or how the link above relates to that. I’d really appreciate any extra info or working code - especially if it tied in with iron-ajax!

thanks

Matt

UPDATE: cracked it now, late in my timezone but will post the example I was looking for tomorrow - anyone reading this, please chase me if I’ve forgotten :slight_smile:

Matt

Can you please share your code how you remove the columns? The docs doesn’t seems to have it in…

Thanks in advance

Stephan

Hi Stephan,

you can use the removeColumn(id) method to remove columns. It is documented in the vaadin-grid API documentation here:
https://cdn.vaadin.com/vaadin-core-elements/latest/vaadin-grid/#vaadin-grid:method-removeColumn

Thank you. Was hoping for something that works directly on the name though…

When I try to add a column, it shows up as an empty column – that is, there is space for it but the header is empty and the data is empty. Can you provide an example of how to do it properly? Thanks.

The templates apparently work for headers but not the rows.

[font=courier new]


      <!-- FIXME: This shows blank columns... -->
      <!--<template is="dom-repeat" items="[[_columns]

]" as=“_col”>–>

      <col name="last_name">
      <col name="first_name">
      <col name="email">
      <col name="phone">
    </colgroup>
    <thead>
      <tr>
        <template is="dom-repeat" items="[[_columnHeaders]

]" as=“_hdr”>





[[_hdr]
]


[/font]

Has there been any news on this, I’m also trying to do something similar. To add the cols dynamically i’ve just added the HTML old school, is there any reason your avoiding this method?

[code]
_createGrid: function() {
var div = document.createElement(‘div’);
var h = ‘’;
h += ‘’;
h += ‘

’;
h += ‘’;
h += this._addCols();
h += ’ ';
h += this._addHeaders();
h += ‘
’;

    div.innerHTML = h;
    this.$.holder.appendChild(div);
    return Promise.resolve();
  },

  _addHeaders: function() {
    var ths = '';
    for(var i = 0; i < this.selectedForm.inputs.length; i++) {
      var item = this.selectedForm.inputs

[/code][i]

[code]
;
var label = item.label;
ths += ‘’ + label + ‘’;
}
return ths;
},

  _addCols: function() {
    var cols = '';
    for(var i = 0; i < this.selectedForm.inputs.length; i++) {
      var item = this.selectedForm.inputs

[/code][i]

;
          var key = item.$key;
          cols += '<col name="' + key + '"/>';
        }
        return cols;
      },

And i’ve just added this to my Promise based page load routine after I have the data.
[/i]
[/i]

Hi Dale,

I don’t really see a problem in your approach, if you’re just doing that once after the data is available. The problem with manipulating the lightDOM is that it will bootstrap the whole grid again is anything changes in it. So techinically it works, but it’s going to be slow, and might have some issues.

We just merged
this feature for the upcoming 2.0 version
, though, so you might want to give that a try instead. So install
vaadin-grid#2.0-dev
for the version, as the fix is not yet in any tagged release.