Get data from an editable table (Scala code)

Hello all,

I’m a greenhorn on Vaadin and need some help.

My application provides a little table. If the user clicks on the checkbox, the table is in editable mode and the fields are open. So far, so good.

If the user - after changing some fields - clicks on the save button, I will receive all the rows from the table to check the changes and save the new values into the database.

Here the source code

package com.mycompany

import com.vaadin.Application
import com.vaadin.data.{Container, Property}
import com.vaadin.ui._

@SuppressWarnings(Array("serial"))
@SerialVersionUID(1L)
class TableSample extends Application   {

  private var mainWindow: Window = null

  override def init()  {

    mainWindow = new Window("TableSample")

    val table = new Table("Nobel Prize for Literature")

    table.addContainerProperty("First name", classOf[String]
, null)
    table.addContainerProperty("Last name", classOf[String]
, null)
    table.addContainerProperty("Year", classOf[String]
, null)

    val array = new scala.Array[java.lang.Object]
(3)

    array(0) = "Thomas"; array(1) = "Mann"; array(2) = "1929"
    table.addItem(array, 1)

    array(0) = "W.B. "; array(1) = "Yeats"; array(2) = "1923"
    table.addItem(array, 2)

    array(0) = "Günter"; array(1) = "Gass"; array(2) = "1999"
    table.addItem(array, 3)

    table.setSelectable(true)
    table.setMultiSelect(true)

    table.setImmediate(true)
    val current = new Label("Selected: - ")

    table.addListener(new Property.ValueChangeListener() {
      def valueChange(p1: com.vaadin.data.Property.ValueChangeEvent) {
        current.setValue("Selected: " + table.getValue)
      }
    })

    val saveButton = new Button("Save")
    saveButton.setVisible(false)

    var switchEditable = new CheckBox("Editable");
    switchEditable.addListener(new Property.ValueChangeListener() {
      def valueChange(event: com.vaadin.data.Property.ValueChangeEvent) {
        table.setEditable(event.getProperty.getValue.toString.toBoolean)
        if (table.isEditable) {
          saveButton.setVisible(true)
        } else                {
          saveButton.setVisible(false)
        }
      }
    });

    saveButton.addListener(new Button.ClickListener() {
      def buttonClick(event: com.vaadin.ui.Button#ClickEvent) {
        //
        //[b]
 TODO get table data - how receive the rows from the table?
        // 
[/b]
      }
    });

    switchEditable.setImmediate(true);
    mainWindow.addComponent(switchEditable);
    mainWindow.addComponent(saveButton)
    mainWindow.addComponent(table)
    mainWindow.addComponent(current)

    setMainWindow(mainWindow)

  }
}

How can I receive the rows from the table? Is it necessary to implement a TableFieldFactory? If yes, how I can implement a TableFieldFactory in my scala application?

Thanks in advance!

Pongo

Hi,

I found a solution: databinding with the BeanContainer.

package com.mycompany

import com.vaadin.Application
import com.vaadin.data.Property
import com.vaadin.ui.Button.ClickListener

import scala.collection.JavaConverters._
import java.util.Collection
import com.vaadin.ui._
import com.vaadin.data.util.BeanContainer

@SuppressWarnings(Array("serial"))
@SerialVersionUID(1L)
class TableSample extends Application {

  private var mainWindow: Window = null

  override def init() {

    mainWindow = new Window("TableSample")

    val persons: BeanContainer[Int, Person]
 = new BeanContainer[Int, Person]
(classOf[Person]
)

    persons.setBeanIdProperty("id")
    persons.addBean(new Person("Thomas", "Mann", 1929, 123123))
    persons.addBean(new Person("W. B.", "Yeats", 1923, 643454))
    persons.addBean(new Person("Günter", "Grass", 1999, 743523))

    val table: Table = new Table("Nobel Prize for Literature", persons)

    table.setVisibleColumns(Array("id", "firstName", "lastName", "year"))

    table.setColumnHeader("lastName", "Nachname")
    table.setColumnHeader("firstName", "Vorname")
    table.setColumnHeader("year", "Jahr")

    table.setSelectable(true)
    table.setMultiSelect(true)

    table.setImmediate(true)
    val current = new Label("Selected: - ")

    table.addListener(new Property.ValueChangeListener() {
      def valueChange(p1: com.vaadin.data.Property.ValueChangeEvent) {
        current.setValue("Selected: " + table.getValue)
      }
    })

    val saveButton = new Button("Save")
    saveButton.setVisible(false)

    val allSelectedButton = new Button("All rows")

    val deleteButton = new Button("Delete")
    deleteButton.addListener(new ClickListener {
      def buttonClick(event: com.vaadin.ui.Button#ClickEvent) {
        val deleteList = table.getValue().asInstanceOf[Collection[Int]
].asScala.toList
        for (deleteId <- deleteList) {
          println("delete for id ===> " + deleteId)
        }
      }
    })

    val switchEditable = new CheckBox("Editable");
    switchEditable.addListener(new Property.ValueChangeListener() {
      def valueChange(event: com.vaadin.data.Property.ValueChangeEvent) {
        table.setEditable(event.getProperty.getValue.toString.toBoolean)
        if (table.isEditable) {
          saveButton.setVisible(true)
        } else {
          saveButton.setVisible(false)
        }
      }
    });

    saveButton.addListener(new Button.ClickListener() {
      def buttonClick(event: com.vaadin.ui.Button#ClickEvent) {
        println("save button")
        println("IDs ===> " + persons.getItemIds)
        val changeList = persons.getItemIds.asInstanceOf[Collection[Int]
].asScala.toList
        for (id <- changeList) {
          val person = persons.getItem(id)
          println("id ===========> " + person.getBean.getId)
          println("firstName ====> " + person.getBean.getFirstName)
          println("lastName =====> " + person.getBean.getLastName)
        }

      }
    });

    allSelectedButton.addListener(new ClickListener {
      def buttonClick(event: com.vaadin.ui.Button#ClickEvent) {
        table.setValue(table.getItemIds());
      }
    })

    switchEditable.setImmediate(true);
    mainWindow.addComponent(switchEditable);
    mainWindow.addComponent(table)
    mainWindow.addComponent(current)
    mainWindow.addComponent(saveButton)
    mainWindow.addComponent(deleteButton)
    mainWindow.addComponent(allSelectedButton)

    setMainWindow(mainWindow)

  }
}

Maybe not perfect, but it works.

Thanks anyway!

Cheers,
Pongo