vaadin-grid Selection

Selecting rows

Data rows can be selected either through UI interaction (by clicking, tapping or hitting space on a highlighted cell) or alternatively by using the selection JavaScript API.

var grid = grid || document.querySelector("vaadin-grid"); HTMLImports.whenReady(function() { // code grid.selection.deselect(0); grid.selection.select(1); // end-code });

Selection modes

You can toggle between multi and all modes by clicking the checkbox in vaadin-grid header.

Name Surname Activity
var grid = grid || document.querySelector("vaadin-grid"); HTMLImports.whenReady(function() { grid.items = employees; // code // Set the selection mode to "multi" grid.selection.mode = "multi"; // end-code });

Processing the selections as arrays

You can simply ask vaadin-grid for all the selected row indexes with selection.selected() function which returns an array of integers for you to process further.

This works fine on most cases with single and multi selection modes, but if your grid contains tons of data and the selection mode is set to all, you're better off with selection.deselected().

var grid = grid || document.querySelector("vaadin-grid"); HTMLImports.whenReady(function() { grid.items = employees; // code // Log the selections / deselection to console on select event grid.addEventListener("selected-items-changed", function() { console.log("Selected: " + grid.selection.selected()); console.log("Deselected: " + grid.selection.deselected()); }); // end-code });

Iterating the selections

Another option for processing the selections is to iterate trough them. The selection.selected() and selection.deselected() functions accept an iterator function as the first argument so you can loop trough all the selections without ever populating an array.

If however your iterator function returns a value, then it's considered a mapper function meaning that the selection API function will return an array of pre-mapped values.

var grid = grid || document.querySelector("vaadin-grid"); HTMLImports.whenReady(function() { grid.items = employees; // code // Log selected designers list on select event grid.addEventListener("selected-items-changed", function() { console.log("Selected designers:"); grid.selection.selected(function(index) { var data = employees[index]; if (data[2] === "Design") { console.log("-" + data[0] + " " + data[1]); } }); }); // end-code });

Processing selections in batches

The selection.selected() and selection.deselected() functions also accept startIndex and endIndex as the second and third argument. These can be used to process the selections in batches. selection.size property comes in handy here.

All the three parameters (iterator/mapper, startIndex, endIndex) have default values so any of them can be omitted (or set as undefined).

var grid = grid || document.querySelector("vaadin-grid"); HTMLImports.whenReady(function() { grid.items = employees; // code // Log selected indexes in batches of 5 grid.addEventListener("selected-items-changed", function() { for (var i = 0; i < grid.selection.size; i += 5){ console.log(grid.selection.selected(undefined, i, i+4)); } }); // end-code });