Vaadin8 Grid - Add inline editor for double values

Hello,

I have created a grid, but I am not able to bind a TextField (which is for Strings) to the column. Only a Slider is working. Is there an easier way to add a “TextField” for double values to a grid column for editing?

val grid = Grid<TypeConfig>(TypeConfig::class.java).apply {

    [...]


    addColumn(TypeConfig::triggerDiff, NumberRenderer())
            .setEditorBinding(editor.binder.bind(
                Slider(),
                TypeConfig::triggerDiff.getter,
                TypeConfig::triggerDiff.setter
            ))
            .caption = "Trigger"

    [...]


    editor.isEnabled = true
    setSizeFull()
}

I also tried to create my own “TextField” which could be used for Double Values. This is working, but I can not validate if the input is an double only:

[code]
import com.vaadin.ui.Component
import com.vaadin.ui.CustomField
import com.vaadin.ui.TextField

class DoubleField : CustomField() {
private var value: Double = 0.0
private var textField = TextField()

override fun initContent(): Component {
    textField.value = value.toString()
    textField.addValueChangeListener { event ->
        this.value = event.value.toDouble() //Also typing non numbers is allowed
    }
    return textField
}

override fun getValue(): Double {
    return value
}

override fun doSetValue(value: Double) {
    this.value = value
}

}
[/code]Maybe I am doing something completely wrong?

Thanks!

Hi Kilian,

The most basic way to use a TextField for double values in the Grid is to do it the way you’d do it for the Binder alone. For example

Binder<MyType> binder = grid.getEditor().getBinder();
grid.addColumn(MyType::getDoubleValue, new NumberRenderer())
  .setEditorBinding(binder
    .forField(new TextField())
    .withConverter(new StringToDoubleConverter("Not a double"))
    .bind(MyType::getDoubleValue, MyType::setDoubleValue)
  );

This will instruct the Grid to use your new TextField, apply a string ↔ double conversion with the failure message “Not a double” and use the getter and setter from MyType to read and write the values.

//Teemu

Thanks! Thats the trick! Perfect! :slight_smile:

I had the same question,

Thanks Teemu…