semenchikus
semenchikus

Reputation: 760

Vaadin 7: fill new data into Table dynamically

I'm looking for right way to create a table that could upload new rows dynamically. As a DataSource I'm using SQLContainer with TableQuery. There could be much of data which should be uploaded quickly.

Anyway, my current realization is following:

Table messagesList = new Table();
...............................
messagesList.setCacheRate(0.1d);
messagesList.setContainerDataSource(messagesContainer);
messagesList.setSelectable(true);        
messagesList.setImmediate(true);
messagesList.setSizeFull();
new InitializerThread().start();
...............................

Data is uploading using refreshRowCache method and Vaadin Push tecknology in another thread:

class InitializerThread extends Thread {
        @Override
        public void run() {
            while (!Thread.interrupted()) {
                try {
                    Thread.sleep(refreshMessagesPeriod);
                } catch (InterruptedException e) {
                }
                access(new Runnable() {
                    @Override
                    public void run() {
                        if (messagesList != null && !messagesList.getItemIds().isEmpty()) {
                            messagesList.refreshRowCache();
                            messagesList.focus();
                        }
                    }
                });
            }
        }
}

This approach has many disadvantages: 1. If there are many rows in the table, it is very inefficient way to refresh all row's cache in the table everytime. 2. Scroll bar jumps to the top of page in the table when the row's cache is refreshing. I didn't find the way to save the scroll's position and set the previous scroll position after refresh. 3. If I select some text in a cell of the table, the selection dissapears after row's cache refresh.

I hope that there is a lightweight and more nice technique to fill new data into Table dynamically. I use Vaadin 7.1.15 and it is allowed to change version of Vaadin type of table (instead com.vaadin.ui.Table) if necessary.

Upvotes: 0

Views: 2716

Answers (1)

semenchikus
semenchikus

Reputation: 760

I found a better solution - control container content manually. Using IndexedContainer as a data source instead TableQuery and periodically checking a new data using sql queries. Vaadin Push helps me to visualise new rows. To prevent a scroll bar jump (which is the result of calling refreshRowCache) I call the private method Table.setCurrentPageFirstItemId(int, boolean) with following parameters: a new row ID, false (do not call refreshRowCache).

Upvotes: 0

Related Questions