I would like to create a simple HTML table in Vaadin flow, but the component is not present anymore (used to be available as com.vaadin.ui.Table). The table is meant to show the detailed properties (key-value pairs) of an item selected in a Grid.
What Vaadin Flow component can I use to implement this? And why was the table removed in Vaadin Flow in the first place?
Table was actually removed already in Vaadin8. https://vaadin.com/blog/-/blogs/mission-rip-table-migrate-to-grid-intro
For implementing a table in Flow there are a couple choices.
One is to use the Element API and one is to create Components for table.
For the element API version it could be something like:
Element table = new Element("table");
For(item : item rows to add) {
Element tr = new Element("tr");
table.appendChild(tr);
For(int i = 0; i < dataColumns; i++) {
Element td = new Element("td");
// could perhaps append a span with text context.
td.setText(item text for column i);
tr.appendChild(td);
}
}
For the Component approach the basic case would then perhaps be to implement the 3 elements as something like:
#Tag("table")
public class Table extends Component implements HasComponents {
public Row addRow() {
Row row = new Row();
add(row);
return row;
}
public Row getRow(int row) {
final Optional<Component> rowOptional = getElement().getChild(row)
.getComponent();
if(rowOptional.isPresent())
return (Row) rowOptional.get();
return null;
}
}
#Tag("tr")
public class Row extends Component {
public void add(Cell cell) {
getElement().appendChild(cell.getElement());
}
public int getRow() {
return getElement().getParent().indexOfChild(getElement());
}
}
#Tag("td")
public class Cell extends Component {
public int getCol() {
return getElement().getParent().indexOfChild(getElement());
}
public int getRow() {
return ((Row) getParent().get()).getRow();
}
public void setText(String text) {
getElement().setText(text);
}
}
There can be multiple use cases for Table component. There are couple of alternative's in Vaadin's Directory of community components.
Table showing list of data (similar to Grid, but more light weight approach)
https://vaadin.com/directory/component/beantable
Table as layout component, which supports row-span, col-span etc. and you populate each cell individually.
https://vaadin.com/directory/component/html-table
As these usecases are quite different , they are better catered by different Java API, although the HTML DOM structure they produce is very similar. Neither of these add-ons attempt to reproduce API of the Vaadin 7 Table component.
There is also a recipe in Cookbook, how to generate Table in TemplateRenderer of the Grid details row.
https://cookbook.vaadin.com/grid-details-table
Related
I was searching for answears but I couldn't find it. It might be a beginner question, anyhow I am stuck.
What I am trying to write is a test in Apex. Basically the Apex code gets field names from one specific object. Each fieldname will be shown in a picklist, one after the other (that part is a LWC JS and HTML file).
So, only want to test the Apex for the moment.
I don't know how to check that a list contains 2 parameters, and those parameters are object and field. Then the values are correctly returned, and I don't know how to continue.
Here's the Apex class with the method, which I want to test.
public without sharing class LeadController {
public static List <String> getMultiPicklistValues(String objectType, String selectedField) {
List<String> plValues = new List<String>();
Schema.SObjectType convertToObj = Schema.getGlobalDescribe().get(objectType);
Schema.DescribeSObjectResult objDescribe = convertToObj.getDescribe();
Schema.DescribeFieldResult objFieldInfo = objDescribe.fields.getMap().get(selectedField).getDescribe();
List<Schema.PicklistEntry> picklistvalues = objFieldInfo.getPicklistValues();
for(Schema.PicklistEntry plv: picklistvalues) {
plValues.add(plv.getValue());
}
plValues.sort();
return plValues;
}
}
I welcome any answers.
Thank you!
This might be a decent start, just change the class name back to yours.
#isTest
public class Stack73155432Test {
#isTest
public static void testHappyFlow(){
List<String> picklistValues = Stack73155432.getMultiPicklistValues('Lead', 'LeadSource');
// these are just examples
System.assert(!picklistValues.isEmpty(), 'Should return something');
System.assert(picklistValues.size() > 5, 'At least 5 values? I dunno, whatever is right for your org');
System.assert(picklistValues[0] < picklistValues[1], 'Should be sorted alphabetically');
System.assert(picklistValues.contains('Web'), 'Or whatever values you have in the org');
}
#isTest
public static void testErrorFlow(){
// this is actually not too useful. You might want to catch this in your main code and throw AuraHandledExceptions?
try{
Stack73155432.getMultiPicklistValues('Account', 'AsdfNoSuchField');
System.assert(false, 'This should have thrown');
} catch(NullPointerException npe){
System.assert(npe.getMessage().startsWith('Attempt to de-reference a null object'), npe.getMessage());
}
}
}
I'm using two tables, the first one contains "Teams", the second one "Team members" and is populating based on the first table selection. I'm also showing various stats depending on the selection, be it a team or a specific member. If no member is selected, team stats are showed, otherwise member stats are showed.
I'm using ItemChangeListeners on the tables to redraw the stats, but this prevents me to click on an already selected team to "deselect" a selected member from that team, since no event is triggered in that circumstance. As a solution I'm also using a ClickListener on the Team table, but it seems to work only if I click on the word (instead of working on the whole cell).
teamsTable.setClickListener("name", new Table.CellClickListener() {
#Override
public void onClick(Entity item, String columnId) {
if (teamsDs.getItem() == item) {
teamsDs.setItem(null);
teamsDs.setItem((Team) item);
} else {
teamsDs.setItem((Team) item);
teamsTable.setSelected((Team) item);
}
}
});
Is there a better way to catch a click on a table cell? Or is there a better way to approach the problem altogether?
Since CUBA Table is a wrapper of Vaadin table you can use ItemClickListener from Vaadin with CUBA table:
public class DemoScreen extends AbstractWindow {
#Inject
private Table<User> usersTable;
#Override
public void init(Map<String, Object> params) {
super.init(params);
com.vaadin.ui.Table vTable = usersTable.unwrap(com.vaadin.ui.Table.class);
vTable.addItemClickListener((ItemClickEvent.ItemClickListener) event ->
showNotification("Item " + event.getItemId())
);
}
}
It will be fired each time you click on a table cell.
In my current project I would like to be able to create new objects when searching for a reference object. This happens in several places of the application.
For example, let's assume we have a City Entity and a Country Entity. The City entity has a mandatory reference to the Country entity.
In my use case, I would like to create a new City. When I do this, I will have to assign a Country to the new City. When I click on the lookup icon, I get the selection dialog with all existent countries. But if I don't have the Country I want, I have to abort the operation, get back to the countries list and create the new one I'd like to assign to my new city.
Would it be possible to create that new Country from the selection dialog with all countries?
If it is possible, is the country being added to the list right after it has been created?
Would it be possible to one define a range for the countries list? For example, showing only countries in Europe, if the user is in Europe.
I could imagine, that this would be a lot to ask from the framework. But I am just giving a shot and perhaps also giving a new feature idea, which would be nice to have.
Customization of the LOV dialog :
You can easily customize the LOV dialog by creating your own class of the LOV action that is installed next to the reference fields.
Adding a new action in the dialog (the create action) :
public class LovActionWithCreate<E, F, G> extends LovAction<E, F, G> {
private IDisplayableAction createAction;
#Override
protected void feedContextWithDialog(IReferencePropertyDescriptor<IComponent> erqDescriptor,
IQueryComponent queryComponent, IView<E> lovView, IActionHandler actionHandler,
Map<String, Object> context) {
super.feedContextWithDialog(erqDescriptor, queryComponent, lovView, actionHandler, context);
List<IDisplayableAction> defaultLovDialogActions = (List<IDisplayableAction>) context.get(
ModalDialogAction.DIALOG_ACTIONS);
defaultLovDialogActions.add(1, getCreateAction());
}
/**
* Gets create action.
*
* #return the create action
*/
protected IDisplayableAction getCreateAction() {
return createAction;
}
/**
* Sets create action.
*
* #param createAction
* the create action
*/
public void setCreateAction(IDisplayableAction createAction) {
this.createAction = createAction;
}
}
The key point is to override the feedContextWithDialog method in order to install the new action into the dialog.
Next step is to install your new LOV action. You can either do it globally for whole application or per reference view :
replacing the LOV action globally is just a matter of declaring an action named 'lovAction' into your application frontend.groovy, i.e. :
action('lovAction', parent: 'lovActionBase', class:'test.LovActionWithCreate',
custom: [createAction_ref:'theCreateAction']
)
replacing the LOV action on a certain reference field in a form can be done by using the referencePropertyView (in a form or in a table) and its 'lovAction' property, e.g. :
action('lovActionWithCreate', parent: 'lovActionBase', class:'test.LovActionWithCreate',
custom: [createAction_ref:'theCreateAction']
)
form('ACertainForm'){
fields {
...
referencePropertyView name:'country', lovAction:'lovActionWithCreate'
...
}
}
Creating an entity in the LOV dialog :
In the next step, we create the action that will be responsible for opening an extra dialog in order to create the new entity, persist it and, if successful, add it to the LOV result view. This is a little more complicated but not that much.
First of all, we have to open a new dialog.
For doing this, we will inherit the built-in EditComponentAction. The goal of this action is to edit a model in a modal dialog. The only difficulty here is that our model is only known at runtime. No problem though as we will use the dynamic nature of Jspresso.
public class CreateEntityFromLOVAction<E, F, G> extends EditComponentAction<E,F,G> {
#Override
protected Object getComponentToEdit(Map<String, Object> context) {
IEntityFactory entityFactory = getBackendController(context).getEntityFactory();
IQueryComponent lovQueryComponent = (IQueryComponent) context.get(IQueryComponent.QUERY_COMPONENT);
Class<IEntity> entityToCreateContract = lovQueryComponent.getQueryContract();
IEntity entityInstance = entityFactory.createEntityInstance(entityToCreateContract);
setActionParameter(Arrays.asList(entityInstance), context);
return entityInstance;
}
#Override
protected IViewDescriptor getViewDescriptor(Map<String, Object> context) {
IEntityFactory entityFactory = getBackendController(context).getEntityFactory();
IQueryComponent lovQueryComponent = (IQueryComponent) context.get(IQueryComponent.QUERY_COMPONENT);
Class<IEntity> entityToCreateContract = lovQueryComponent.getQueryContract();
IComponentDescriptor<?> entityToCreateDescriptor = entityFactory.getComponentDescriptor(entityToCreateContract);
BasicComponentViewDescriptor formViewDescriptor = new BasicComponentViewDescriptor();
formViewDescriptor.setModelDescriptor(entityToCreateDescriptor);
return formViewDescriptor;
}
}
If you look at the code above, our new action takes care of the following :
Get the type of entity to create from the context. For this, we are just exploring the query component which is the model of the LOV dialog.
Create the entity instance and set it as action parameter in the context for the chain to continue working on it (save, close dialog).
Create a form to display in the creation dialog.
Points 1 and 2 are handled by the getComponentToEdit method and point 3 by the getViewDescriptor method.
Next, when the user clicks Ok, we have to save the entity, add it to the LOV result list and close the creation dialog.
For this, we will create a new action and chain it to the saveAction and closeDialogAction built-in actions.
public class CreateEntityFromLOVPersistAction<E, F, G> extends FrontendAction<E,F,G> {
#Override
public boolean execute(IActionHandler actionHandler, Map<String, Object> context) {
if (super.execute(actionHandler, context)) {
IQueryComponent lovQueryComponent = (IQueryComponent) context.get(IQueryComponent.QUERY_COMPONENT);
List<IEntity> createdEntityInstance = getActionParameter(context);
lovQueryComponent.setQueriedComponents(createdEntityInstance);
return true;
}
return false;
}
}
And the final wiring in SJS frontend.groovy:
action('createEntityFromLovOkAction', parent: 'okDialogFrontAction',
class:'test.CreateEntityFromLOVPersistAction',
wrapped: 'saveBackAction', next: 'closeDialogAction')
action('createEntityFromLovAction', parent: 'editComponentAction',
class: 'test.CreateEntityFromLOVAction',
name:'add.name', custom: [
okAction_ref: 'createEntityFromLovOkAction'
]
)
action('lovAction', parent: 'lovActionBase',
class:'test.LovActionWithCreate',
custom: [createAction_ref:'createEntityFromLovAction']
)
A long answer for less than 100 lines of code, but now you have a fully generic LOV action where the user can create any missing master data without leaving his current screen.
Presetting some data in the LOV filter depending on the user context :
For this, we generally use the initialization mapping that allows for setting some restrictions (either static or dynamic) on a reference property when it is queried in a LOV. For instance, let's consider the following use case :
You have 2 entities, Contract and Tariff, that are linked together through a 1-N relationship, i.e. a Contract is linked to 1 Tariff.
Contract and Tariff both have a country property and a Tariff can be assigned to a Contract if and only if they belong to the same country.
Tarrif has a status property and can only be used in a Contract if its status is ACTIVE.
You can simply enforce these rules in the LOV by setting the initialization mapping on the reference property the following way :
Entity('Contract', ...) {
...
reference 'tariff', ref: 'Tariff',
initializationMapping: [
'country': 'country',
'status': 'ACTIVE'
]
...
}
Thinking about it, this kind of behavior might very well find its way to the framework, so please, feel free to ope an enhancement request in the Jspresso GitHub.
As part of Selenium Web-driver learning I came across a scenario. Please let me know the professional approach to proceed.
I am testing a eCommerce application where while I click on Mobile link all mobile phones are getting displayed.I want to check whether they are sorted based on name and price. So basically I need to get Name & price of all elements in the result page.
So My Question is there any way I can map html elements to java value objects ? Any API already available for doing this mapping for me ? Something similar to gson api for converting java objects to their corresponding Json representation ?
Deepu Nair
//Get all the mobile phones links into a list before sorting
List<WebElement> mobilelinks=driver.findElements(("locator"));
Map maps = new LinkedHashMap();//use linked hash map as it preserves the insertion order
for(int i=0;i<mobilelinks.size();i++){
//store the name and price as key value pair in map
maps.put("mobilelinks.get(i).getAttribute('name')","mobilelinks.get(i).getAttribute('price')" );
}
/*sort the map based on keys(names) store it in a separate list
sort the map based on values(prices) store it in a separate list
*/
/* Using webdriver click the sort by name and compare it with the list which we got after sorting
and also click sort by prices and compare it with the list*/
To catch an assertion and continue with the test after assertion failures override the Assertion class and create your own CustomAssertion or use SoftAssertions
CustomAssertion.java
public class CustomAssertions extends Assertion {
private Map<AssertionError, IAssert> m_errors = Maps.newLinkedHashMap();
#Override
public void executeAssert(IAssert a) {
try {
a.doAssert();
} catch(AssertionError ex) {
onAssertFailure(a, ex);
System.out.println(a.getActual());
System.out.println(ex.getMessage());
m_errors.put(ex, a);
}
}
public void assertAll() {
if (! m_errors.isEmpty()) {
StringBuilder sb = new StringBuilder("The following asserts failed:\n");
boolean first = true;
for (Map.Entry<AssertionError, IAssert> ae : m_errors.entrySet()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(ae.getKey().getMessage());
}
throw new AssertionError(sb.toString());
}
}
}
Instead of using Assertions class to verify the tests use CustomAssertions class
Ex:
//create an object of CustomAssertions class
CustomAssertions custom_assert=new CustomAssertions();
cust_assert.assertTrue(2<1);
cust_assert.assertEquals("test", "testing");
//and finally after finishing the test in aftersuite method call
cust_assert.assertAll();
Hope this helps you if you have any doubts kindly get back...
I am developing an ecommerce platform and came across a difficulty. Basically, I have in my scenario a Product, Option and OptionValue. A product might have multiple options which might have multiple values. The problem is how to store it in a way that is easy to create and edit.
The problem is I've a direct reference to the OptionValue, which is mutable. I need to keep immutable information about the Option and OptionValue (for example, if a order was made and the color was green, even if this option is changed to lime green, the order must keep showing as green). In that case, I need to save some properties of Option (the option name - "Colors" for example) and of each OptionValue (the value of each option - "red" for example). The way I thought, it would require a structure very similar to the existing structure: a new class ItemOption referencing Option and a ItemOptionValue referencing OptionValue.
So, this was my attempt:
To create a form to display my options I have:
class OptionSelectorType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($options['product']->getOptions() as $option) {
$builder->add($option->getId(), 'choice', array('choice_list' => new ObjectChoiceList($option->getValues());));
}
}
}
I'm using a DataTransformer to convert a collection of OptionValue in a collection of OrderItemOption:
class OrderItemOptionToOptionValueTransformer implements DataTransformerInterface
{
public function transform($lineOptions)
{
if(!$lineOptions) {
return array();
}
$values = array();
foreach($lineOptions as $lineOption) {
$lineOption->getOption()->getId();
$values[$id] = array();
foreach($lineOption->getValues() as $lineOptionValue) {
$values[$id][] = $lineOptionValue->getOptionValue();
}
}
return $values;
}
public function reverseTransform($values)
{
$collection = new ArrayCollection();
foreach($values as $optionId => $optionValues) {
if(!$optionValues) {
continue;
}
$lineOption = new OrderItemOption();
$optionValues = is_array($optionValues) ? $optionValues : array($optionValues);
foreach($optionValues as $optionValue) {
$lineOptionValue = new OrderItemOptionValue();
$lineOptionValue->setOptionValue($optionValue);
$lineOption->addValue($lineOptionValue);
}
$lineOption->setOption($optionValue->getOption());
$collection->add($lineOption);
}
return $collection;
}
}
Finally, my OrderItemType form:
class OrderItemType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
$builder->create('options', 'option_selector', array('options' => $options['options']))
->addModelTransformer(new OrderItemOptionToOptionValueTransformer())
);
}
}
It works but doesn't seem to me a good approach, once the OptionValue will be always recreated, never updated.
How would you do it?
I will keep it simple. You can stick with your structure but instead of transforming things on the fly, keep them at the same state.
What do I mean?
Whenever an Option is created, create the corresponding ItemOption. The same goes with the OptionValue and ItemOptionValue. The relation between the two is a one-to-one connection, whereas the Option and OptionValue don't know the connected ItemOption and ItemOptionValue.
Now if a change occurs to the OptionValue, you can query for the connected ItemOptionValue and change the things you need to change (depends on internal structure).
How to store the connection?
Use whatever persistence method you already use. For the case of a database, store the connection in one table like this:
CREATE TABLE item_option_to_option (
optionID INT(10) NOT NULL,
itemOptionID INT(10) NOT NULL,
UNIQUE KEY (optionID, itemOptionID)
);
If possible I would use Foreign Keys to link the both columns to the corresponding columns in the tables item_option and option. It works the same with OptionValue and ItemOptionValue.
How to handle the change?
Whenever the controller for the OptionValue change is called, simply update the Item* models as well.
If an Option or OptionValue is deleted, it is up to you, if you delete the ItemOption or ItemOptionValue as well.