How handle click event of remove item button in multiselect combobox vaadin 14 - combobox

Would like to show confirmation dialog when any of the selected items is removed from multi select combo box. We have this token remove button and its click event I would like to know how we can handle its click event in java ?

There's no Java API for the click listener of the items. However, if you want to add a confirmation step for deselection, you can use a workaround with a selection listener, as the selection event knows the previous selection set and the new selection set:
MultiselectComboBox<String> multiselectComboBox = new MultiselectComboBox();
multiselectComboBox.setLabel("Select items");
multiselectComboBox.setItems("Item 1", "Item 2", "Item 3", "Item 4");
multiselectComboBox.addSelectionListener(event -> {
if (!event.isFromClient()) {
return;
}
Set<String> removedSelection = event.getRemovedSelection();
if (removedSelection.isEmpty()) {
return;
}
String itemsAsString = String.join(",", removedSelection);
ConfirmDialog confirmDialog = new ConfirmDialog();
confirmDialog.setHeader("Are you sure you want to remove " + itemsAsString);
confirmDialog.setConfirmButton("Ok", null);
confirmDialog.setCancelButton("Cancel", e -> {
multiselectComboBox.select(event.getOldSelection());
});
confirmDialog.open();
});
This won't preserve the order of the selection, but that might be doable as well with a little bit of effort.

Related

Grid with LitTemplate not updating checkbox consistently

We are updating our Grid forms to use LitTemplates for rendering. However we have run into an issue with a checkbox state not being updated even though the underlying data for the state has been updated. The following code demonstrates the issue:
HorizontalLayout hlTest = new HorizontalLayout();
List<String> selected = new ArrayList<>();
Grid<String> testGrid =new Grid<>();
testGrid.setWidth("250px");
testGrid.setHeight("250px");
List<String> dataList = new ArrayList<>(List.of("AAAAA", "BBBBB", "CCCCC", "DDDDD", "EEEEE"));
ListDataProvider<String> dataProvider = new ListDataProvider<>(dataList);
testGrid.setItems(dataProvider);
String template = """
<vaadin-checkbox #click="${handleCheckClick}" ?checked="${item.rowSelected}"></vaadin-checkbox>
<div>${item.text}</div>
""";
LitRenderer<String> lit = LitRenderer.of(template);
lit.withProperty("rowSelected", item -> {
boolean rc = selected.contains(item);
System.out.println("Item selected: " + item + " => " + rc);
return rc;
});
lit.withProperty("text",item -> item);
lit.withFunction("handleCheckClick", (item) -> {
if(selected.contains(item))
selected.remove(item);
else
selected.add(item);
});
testGrid.addColumn(lit);
Button bSelect = new Button("Select");
bSelect.addClickListener(buttonClickEvent -> {
if(selected.size() > 0)
{
dataList.clear();
dataList.addAll(selected);
selected.clear();
dataProvider.refreshAll();
}
});
hlTest.add(testGrid, bSelect);
With the given grid, click a few checkboxes and click 'Select'. The list is reduced to the selected rows, but the checkboxes are still ticked even though 'selected' is empty. The logging within 'rowSelected' is correctly returning the state.
If I remove the column and re-add to grid, then all is displayed correctly. Also, if I select all items in the grid then the select also works correctly.
Currently using Java17 and Vaadin 22.0.6 (have also tried 23.0.0 beta3).
Thanks.
I think you need to call dataProvider refresh when you update
lit.withFunction("handleCheckClick", (item) -> {
if(selected.contains(item))
selected.remove(item);
else
selected.add(item);
testGrid().getDataProvider().refreshAll();
});

Why ComboBox setValue do not work in Vaadin?

I have simple code:
ComboBox<String> combo=new ComboBox<>("Combo");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
When I first time click button i have items "11" and "22" present in combobox and value "22" is selected.
Second click makes value cleared but items "11" and "22" are still present.
In case I select "11" or leave "22" selected in combobox and click button - value clears.
It seems that setValue() only works when combobox is empty but following code do not helps as well:
combo.setValue(null);
combo.clear();
combo.setItems("11","22");
combo.setValue(null);
combo.clear();
combo.setValue("22");
Following code sets value of ComboBox correctly, no matter if I select some value or clear it before click:
ComboBox<String> combo=new ComboBox<>("Combo");
combo.setItems("11","22");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setValue("22");
}
});
But I have to set items of Combobox dynamically and the last solution do not suitable for me.
Vaadin version is 10.0.9.
Has anyone some suggestions or advices ?
PS.
Thanks !
I've tried following code:
combo.setItems(Collections.emptyList());
combo.setItems("11","22");
combo.setValue("22");
But it do not work as well.
This code works only if value in combo is empty, but if I input something in combo the code just clears value by .setItems() and further .setValue() do not work.
If value of combo is empty the code works well.
Your code works perfectly fine in a minimum project based on https://vaadin.com/start/latest/project-base (which uses Vaadin 12.0.7)
#Route("")
#PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {
public MainView() {
ComboBox<String> combo=new ComboBox<>("Combo");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
add(combo,button);
}
}
Whatever value you set in the ComboBox via UI .. when the button is clicked, the selected value will switch to 22.
If that's an option for you, you could update to a newer Vaadin version and try it with that.
To better show what I meant in my comment, I'm writing it as an answer.
What I meant was to set an empty collection not in the clickListener but directly after initializing the ComboBox:
ComboBox<String> combo=new ComboBox<>("Combo");
combo.setItems(Collections.emptyList());
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
#Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
please try it out and let me know if that works

How to get the current swipe tabs selected index value in codename one?

I am using codename one swipe tabs dynamically to prepare some radio button but on swiping of tabs getSelectedIndex () method is giving the previous tabs value(previous in the sense suppose I move my tabs from 0 to 1 so it's giving me 0) so how to get the current tab value at which my tab is because on basis of this selectedIndexTab value I want my radio button to get selected.
Here is my code
TableLayout t1=new TableLayout(1, 5);
radioTypeContainer=new Container(t1);
for(i=0;i<5;i++){
t.addTab("Tab2"+i, new SpanLabel("Some text directly in the tab"));
firstTab = new RadioButton[i];
plain = new RadioButton("");
plain.setName("rdb"+i);
rbt =new RadioButton();
rbt.setName("rbt"+i);
radioTypeContainer.add(rbt);
finalRadioList.add(rbt.getName());
finalRadioList.add(t.getTabUIID());
}
borderLayoutContainer.add(BorderLayout.SOUTH,radioTypeContainer);
t.addSelectionListener((i1, i) -> {
Log.p("====***======="+t.getSelectedIndex());
});
Thanks in Advance
newSelected in selectionchanged method gives the current position of tab as shown in below code.
t.addSelectionListener(new SelectionListener() {
#Override
public void selectionChanged(int oldSelected, int newSelected) {
Log.p("====***======="+newSelected);
}
});

want to enable a combobox only when another combo is selected in extjs

There are two combo boxes and a button.I want a validation i.e. if "combo1" is selected then "combo2" should get enabled and when i select "combo2" then browse button should get enabled.
Mabe something like that (insert text on first combobox):
The combobox2 and button must have this configs:
hidden:true,
disabled:true,
On combobox1:
listeners:{
change: function(combobox,newValue, eOpts){
var combo2 = Ext.ComponentQuery.query('#combo2ItemId')[0];
var button = Ext.ComponentQuery.query('#buttonItemId')[0];
if(!Ext.isEmpty(newValue)) {
combo2.setHidden(false).setDisabled(false);
button.setHidden(true).setDisabled(true);
}
else {
combo2.setHidden(true).setDisabled(true);
button.setHidden(true).setDisabled(true);
}
}
On combobox2:
listeners:{
change: function(combobox,newValue, eOpts){
var button = Ext.ComponentQuery.query('#buttonItemId')[0];
if(!Ext.isEmpty(newValue)) {
button.setHidden(false).setDisabled(false);
}
else {
button.setHidden(true).setDisabled(true);
}
}
I hope this helps!
I guess you may want to disable the second combo and button when the page first shows.
Then You could listen for the change event for each combo and in your handler code disable\enable which ever controls you need to based on the values passed into the handler function arguments.

How do I get a context menu to work on a Telerik RadGridView column?

I have the following method which adds a new column to a Telerik RadGridView:
private void CreateNewColumn(FieldDescriptor fd, uint fieldno) {
fieldGrid.Columns.Add(new GridViewDataColumn() {
UniqueName = fd.fieldName,
Header = fd.displayName,
DataMemberBinding = new Binding("Fields[" + fieldno + "]"),
ContextMenu = new ContextMenu() {
Tag = fieldno,
Items = {
new MenuItem() {
Header = "Field Properties",
Command = Commands.FieldProperties,
CommandBindings = { new CommandBinding(Commands.FieldProperties, FieldProperties_Execute) }
},
new MenuItem() {
Header = "Delete Field",
Command = Commands.DeleteField,
CommandBindings = { new CommandBinding(Commands.DeleteField, DeleteField_Execute) }
}
}
}
});
}
The problem I'm having is that the context menu never appears when I right click anywhere on the grid. If I bind the context menu directly to the grid, i.e.
fieldGrid.ContextMenu = new ContextMenu() { ...
then the context menu shows up, but I have no way of determining which column the user right-clicked on. Has anyone gotten context menus to work on individual columns or column headers?
I cannot speak for Telerik's grid, but with the Infragistics grid you would attach the context menu to the grid, and then use the mouse location to determine what the user right clicked on in the grid. The Infragistics grid has some decent helper methods to facilitate the hit testing.
You can check my answer on your forum post:
http://www.telerik.com/community/forums/wpf/gridview/column-contextmenu.aspx

Resources