text pattern highlighting in vaadin table


I’m hooked on a problem concering text pattern highlighting in a table. This table contains search results from a database query. The search parameter is set by the user in a text field. Similar to the Google search in the browser, I’d like to highlight the search pattern in the result set (in any cells of the table). It can be bold or another color (font or background). Only the specific serach pattern must be highlighted. I use an IndexedContainer as the ContainerDataSource for the table.
What’s the best way to go?

I would expect having to implement Table.ColumnGenerator interface.
There you have a method called “generateCell” y which you can return any type of components. One of this components can be a vaadin Label object using HTML content mode.
Within this html you could use “your highlighted string” to change the font weight.
Something like:

public Component generateCell(Table source, Object itemId, Object columnId) { Property prop = source.getItem(itemId).getItemProperty(columnId); if (prop.getType().equals(String.class)) { Label label = new Label(prop.getValue(), ContentMode.HTML); ... here you insert the highlighted text in between <b></b> label.setValue(......); return label; } return null; } Also, if you need this only for a certain column, please see that you also have the columnId as a parameter in the method
Hope it helps

I tried this. Somehow this method is never called. I implemented ColumnGenerator:

public abstract class AbstractCodeValueWebTable extends Table implements ColumnGenerator {

and overrode generateCell:


  • {@inheritDoc}
    public Object generateCell(Table source, Object itemId, Object columnId) {
    [/code]But it’s never called (while debugging).

I’m using IndexedContainer to “fill” the Table (setContainerDataSource).

And are you adding this generated columns to the table?

table.addGeneratedColumn("mypropertyId", AbstractCodeValueWebTable());

You’ve got examples in the Book of Vaadin:


Got it, thanks a lot :slight_smile:

Glad it helped!