vaadin7 getting values from container - vaadin

Currently I load data from database to Table in vaadin7 by implementing LazyQueryContainer.
Here is my code:
final LazyQueryContainer container = new LazyQueryContainer(factory, /* index */null, /* batchSize */50, false);
for (Column c : columns) {
container.addContainerProperty(c.getCaption(), c.getType(), null, false, true);
}
table.setContainerDataSource(container);
It works as expected.
However I want to display the data not in table format but as a chart. I have already implemented Jfreechart so all I need to do is somehow retrieve the data from the container (or maybe from the table?)
I can iterate over container.getContainerPropertyIds() but I am not sure whether it is the right thing to do.
How can I get the content of one column of the table? For example the first column in my table is called "date" and I would like to get all date values in a list, or array,etc.
Any help is appreciated.

Related

Eclipse Scout Neon Delete empty row dot trigger save

In one form I have system OK button :
#Order(910.0)
public class SaveButton extends AbstractOkButton {
#Override
protected String getConfiguredLabel() {
return TEXTS.get("Save");
}
}
and I have tableField ...
If I add new empty row in this table execStore is called when pressing save button, but if I delete this empty row and press save button nothing is called, button only close form.
How to solve this? I would like to be able to delete empty row.
EDIT :
I figure it out that all rows where only smart fields are filled in, on delete it doesn't detect change. (not only empty rows...)
Problem is that checkSaveNeeded of the form return that nothing was changed in doOk method.
How do you add your row?
You can mark the row as inserted or not. See: addRow(newRow, markAsInserted); on the Table.
You second question is "How does a Table Field computes if the form needs to be stored or not"?
You can implement you custom logic on with execIsSaveNeeded(). See this question on the Scout forum:
https://www.eclipse.org/forums/index.php?t=msg&th=477037&goto=1042295#msg_1042295
Your last point is: when a row has the status ITableHolder.STATUS_NON_CHANGED in an editable table. When the value in a SmartColumn is changed with a SmartField, the status of the row is not changed to ITableHolder.STATUS_UPDATED.
I just tested it and it works as expected.
For row deletion, it depends on how your table field is configured. What is the value of the property isAutoDiscardOnDelete().
The doc here is still valid with Neon: Table > Delete a row from the table
With isAutoDiscardOnDelete() returning true (this is the case if getConfiguredAutoDiscardOnDelete() returns true), when you remove a row in your table field, it is discarded. There is nothing to save because there is no deleted row in the table field (Everything is explained in the wiki section I have mentioned)
If you want to persit a deletion on the server, you should not use discard the row but just delete it. This way you get the information in the FormData and from there you can work with the row status as described here: TableData (again old doc from our wiki, because it works like this since years. This it not new with Neon).

Vaadin - Table column order

Anybody know how to/or it is possible - create a Table with column specific order; configuration order which was before save - example in DB,
and uploaded at specific view on? also I wonder how to take generate this columns headers and content from POJOS class - beans.
Any good ideas?
setVisibleColumns
The Table::setVisibleColumns does double-duty:
Controls which columns are visible, and
Sets the order in which the columns appear.
Call Table::getVisibleColumns to see current ordering.
Doc
This is well described in:
Book of Vaadin > Table
Sampler > User Interface > Data Presentation > Table
Table API JavaDoc
Example Code
Basically, you need the code like this to control columns order and also set list of bean instances as datasource.
Code is not tested, just a demonstration. Valid for Vaadin 6, but I guess no significant changes comparing to Vaadin 7.
table = new Table();
// Wrap your beans collection into vaadin data container. There are many
// types of them , check Book of Vaadin.
BeanItemContainer<Bean> container = new BeanItemContainer<Bean>(Bean.class)
container.addBean(new Bean());
// Set collection of your beans as data source.
// Columns will be created for each property, vaadin uses reflection.
table.setContainerDataSource( container );
// You can select to display only properties you want, not all.
// Order matters. You can get columns list from any source - for example
// store in your DB.
table.setVisibleColumns( new Object[] {"prop1", "prop2"} );
// You can set column headers (by default vaadin will set them same as bean
// properties names). Order matters, should match above order.
table.setColumnHeaders( new String[] {"Property 1", "Property2"} );
The answer by Sergey Makarov is correct. This answer provides further information.
User’s Reordering
You may allow the user to drag-and-drop columns in a table to re-order them at runtime. To enable this feature, call isColumnReorderingAllowed.
You can use a listener to be informed when such a reorder event occurs.
The user’s re-ordering lasts only for this work session. If you want to maintain the user’s order in future work sessions, you must somehow persist their desired order and apply the order when instantiating the table again.
Losing The Order
If you replace the data source of the table, your column order will be reset. You can get the current order before the change, then restore. Example code follows.
// Visible status & ordering.
java.lang.Object[] visibleColumns = this.exampleTable.getVisibleColumns();
// ------| Fresh Data |--------------
// Put fresh data into the Table.
this.exampleTable.setContainerDataSource( freshBeanItemContainer );
// ------| Restore Config |--------------
// Visible status & ordering.
this.exampleTable.setVisibleColumns( visibleColumns ); // Assumes the table has the same properties.
By the way, as with visibility and order, being collapsed will also be reset with fresh data. You may want to track and restore column collapsing as well as ordering.

How to select all row in vaadin table?

HI,
I am having one check box and one table and table has 10 rows .If user selects the check box then all the 10 rows in the vaadin table should need to select but i don't know how to achieve this functionality.Can anyone tell me how to achieve this? If possible provide me some code snippet.
Table.getValue() takes either the ID of a single item or a collection of several item ID's, and Table.getItemIds() returns the ID's of all items in the table. This means that you can select all items in the table simply by:
yourTable.setValue(yourTable.getItemIds());
Note that this will cause performance troubles if there are a lot of items in the table's container. Should work in a simple case like yours, though.
Make sure the table has yourTable.setMultiSelect(true) and then just iterate the ID's got from yourTable.getItemIds() and call yourTable.select(id) for all id's.
This is one way.
In Vaadin 7 when you have table with container data source you can do this:
table.setValue(container.getItemIds());
In Vaadin 6 works this for me:
public void selectAll() {
int size = table.getItemIds().size();
for(int i = 0; i < size; i++) {
table.select(i);
}
table.requestRepaint();
}
And of course in both Vaadin versions don't forget to these lines:
table.setSelectable(true);
table.setMultiSelect(true);
You can simply do it by
Table table = new Table();
table.setValue(table.getItemIds());
It should not cause performance troubles instead you have few hundreds rows. In case you have - bad architecture.
Also you can just iterate through the list (Vaadin does the same)
Here you can find how to reverse selected list using simple iteration.
In two words:
Collection<Object> toSelect = new ArrayList<Object>();
for (Iterator<?> it = simpleTable.getItemIds().iterator(); it.hasNext(); ) {
Object next = it.next();
if (!((Collection<?>) simpleTable.getValue()).contains(next))
toSelect.add(next);
}
simpleTable.setValue(toSelect);

Copy data from a table to another one has different structure

I have two tables with different structure:
TableSource:
intId(not null), txtSummary, strDetail
TableDesc:
guidId(not null), guidFK(not null), Name, Detail
I want to migrate data from two fields of tableSource(txtSummary, strDetail) to
two fields of tableDesc(Name, Detail).
guidId is auto generation and guidFK should be assigned to a fixed value.
I'v tried writing some t-sql code lines but not success. Could anyone help me?
Something like:
INSERT into TableDesc (Name, Detail)
SELECT txtSummary, strDetail
FROM TableSource
should work, if the other fields are auto-generated on insert like you said.
Which database is this using?

How to use SqlDataReader if I'm having more than one select statements in a stored procedure

I have coded three select statements in stored procedure in Microsoft SQL Server 2005. Both select statements return multiple number of records and table list for select statements is different. One select records from a master table and the other from a child table. In C# code I want to get all these records and put all the data in one object. I am using SqlDataReader. Is it possible with it or should i do something else.
You use the NextResult method on the datareader to navigate with multiple results from a query.
To loop through all data you would do something like this:
var moreResults = true;
while (moreResults)
{
while (reader.Read())
{
...
}
moreResults = reader.NextResult();
}
So with that as a background, and assuming the master resultset comes first, populating master and detail objects could be done like this:
First, build up a dictionary of the Master records:
var masters = new Dictionary<int, Master>();
var idOrdinal = reader.GetOrdinal("id");
while (reader.Read())
{
var id = reader.GetInt32(idOrdinal);
masters.Add(id, new Master{Id=id, ....});
}
Next, move on to detail records and add those to their corresponding master:
reader.NextResult();
var masterIdOrdinal = reader.GetOrdinal("masterId");
while (reader.Read())
{
var masterId = reader.GetInt32(masterIdOrdinal);
var master = masters[masterId];
master.Details.Add(new Detail{....});
}
You should obviously replace column names with what you have in your data as well as supply the full initialization of Master and Detail objects.
If the detail resultset is sorted on master id, the last loop could be optimized to only lookup each master once from the dictionary. If the resultsets are small though, the gain would not be that huge.
...one select records from master table
and other from child table .in c# code
i want to get all this record and put
all this data in one object...
Peter's solution works to solve the basic problem of retrieving multiple results with a single DataReader. However, If you want to save your data to an object which replicates the relationship between the Master-Details tables, you should be using a DataSet instead.
DataSets can contain multiple DataTables and provide full support for inherent relationships between the tables by allowing creation of DataRelations between the tables. Then you can get related records for each scenario by calling GetChildRows() or GetParentRows() from the Master or Details tables respectively.
There are probably many samples online that illustrate how to do this. Here's one discussion thread from my Group where I have listed the steps and provided some code to demonstrate the procedure.

Resources