1

I am writing a filter to filter person names which are displayed in a column pertaining to a vaadin grid. I add a data provider with row objects as follows:

 ListDataProvider<Row> dataProvider = new ListDataProvider<>(dataSet.getRows());
        
        Grid.Column<Row, Object> colName =  grid.addColumn(row -> row.getValue("NAME")).setCaption("NAME");        
        grid.setDataProvider(dataProvider);
        HeaderRow filterRow = grid.appendHeaderRow();
        RowFilter filterObject = new RowFilter();
        dataProvider.setFilter(row -> filterObject.test(row, "NAME"));
         
        TextField nameField = new TextField();
        nameField.addValueChangeListener(event -> {
            filterObject.setName(event.getValue());
            dataProvider.refreshAll();
        });
        nameField.setValueChangeMode(ValueChangeMode.EAGER);
        filterRow.getCell(colName).setComponent(nameField);
        nameField.setSizeFull();
        nameField.setPlaceholder("Filter");
        nameField.getElement().setAttribute("focus-target", "");

The row filter looks as follows:

package com.example.vaadin.utils;

import org.apache.commons.lang3.StringUtils;

public class RowFilter {
    String name = "";
   
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

   
    public boolean test(com.example.vaadin.views.ContactView.Row row, String columnval) { 
        if (name.length() > 0 && !StringUtils
              .containsIgnoreCase(String.valueOf(row.getValue(columnval)),
                    name)) {
            return false;
        }

        return true;
    }
}

Here is my row class:

package com.example.vaadin.utils;

import org.apache.commons.lang3.StringUtils;

public class RowFilter {
    String name = "";   
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean test(com.example.vaadin.views.ContactView.Row row, String columnval) { 
        if (name.length() > 0 && !StringUtils
              .containsIgnoreCase(String.valueOf(row.getValue(columnval)),
                    name)) {
            return false;
        }

        return true;
    }
}

The problem is the following one:

When I want to add the attribute focus-target to the nameField like this - nameField.getElement().setAttribute("focus-target", ""); - but com.vaadin.ui.TextField does not seem to have a getElement method in vaadin 8. I have been looking for a workaround to get this problem fixed, but to no avail. This is why I’d like to ask here whether anybody can tell me if there is one.

Anna Koskinen
  • 1,362
  • 3
  • 22
Andferdd
  • 91
  • 3
  • 2
    Why do you want to add the attribute to the field element? – Leif Åstrand Aug 22 '21 at 19:00
  • I have found out that adding the aforementioned element is a sideshow problem. What is peculiar though is the following fact: In my grid, I add a data provider to the grid as follows: dataProvider.setFilter(row -> filterObject.test(row, "NAME")); The filterObject's test method is not called so that no filtering is done, and I don't know why. – Andferdd Aug 23 '21 at 07:03
  • That thing with filtering sounds like something that should be posted as a separate question since it's quite unrelated to the original question about setting element attributes. – Leif Åstrand Aug 23 '21 at 10:26

1 Answers1

1

getElement() was added in Vaadin 10 and is not included in Vaadin 8. There is no built-in way of setting arbitrary attributes in Vaadin 8, so you would instead have to take to low-level solutions such as building a simple client-side extension or using Javascript::execute.

Leif Åstrand
  • 7,820
  • 13
  • 19