I can't figure out how to programmatically select items in checkboxlist.
This method of cource doesn't compile, but I want to show you what a result I want to get.
public ColumnsSelector(Dictionary<string, bool> dataPropertyNames)
: this()
{
foreach (var item in dataPropertyNames)
{
checkedListBox1.Items.Add(item.Key);
checkedListBox1.Items[checkedListBox1.Items.IndexOf(item.Key)].Checked = item.Value;
}
}
How do you force with this problem?
Use CheckedListBox.SetItemCheckState:
checkedListBox.SetItemCheckState(checkedListBox1.Items.Count - 1, CheckedState.Checked);
which works for checked, unchecked, and indeterminate. You can also use CheckedListBox.SetItemChecked:
checkedListBox.SetItemChecked(checkedListBox1.Items.Count - 1, true);
checkedListBox1.Items.Add(item.Key);
checkedListBox1.SetItemChecked(checkedListBox1.Items.Count - 1, item.Value);
or just
checkedListBox1.Items.Add(item.Key, item.Value);
Related
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();
});
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
populate combobox code:
ComboBox getCategoryComboBox = new ComboBox();
searchOptionForm.add(getCategoryComboBox);
getCategoryComboBox.setUIID("TextField");
getCategoryComboBox.addItem("Choose Category");
for (Map<String, Object> entry : alacc.responseCategory) {
String categoryName = (String) entry.get("name");
String categoryId = (String) entry.get("id");//how to set this to combobox item
getCategoryComboBox.addItem(categoryName);
}
categoryId is taken from for loop above, how to set it in each combobox items? I need to get the categoryId of each selected combobox item, how can i get this?
You have several ways to do this.
One way is to just do:
getCategoryComboBox.addItem(entry);
Which would provide you the full entry on getSelectedItem() effectively solving that problem.
To make the name render properly though you would need to do this:
cb.setRenderer(new DefaultListCellRenderer<Object>() {
public Component getCellRendererComponent(Component list, Object model, Object value, int index, boolean isSelected) {
if(value instanceof Map) {
value = ((Map)value).get("name");
}
return super.getCellRendererComponent(list, model, value, index, isSelected);
}
});
Notice you will also need to define the theme constant otherPopupRendererBool to false for this to work properly.
I've a listener on cellClick, I get the selected Record but I can't find a way to understand if this record is checked
Method ListGrid.isSelected(ListGridRecord) returns true if row is selected, not if is checked
My Code:
listGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
listGrid.addCellClickHandler(new CellClickHandler() {
#Override
public void onCellClick(CellClickEvent event) {
if(event.getColNum() == 0 && idMenu != null){
boolean isChecked = event.getRecord().???;
if(isChecked)
....
else
....
}
I've tried also with event.getRecord().getAttributeAsBoolean("_checkField") with no success...
I found a simply solution...
My task is solved using a special boolean field in the DataSource named, for example, "checked"
In ListGrid I've a field "checked", and with a RecordClickHandler I can manage check or uncheck event.
DataSource code:
DataSourceBooleanField checkField = new DataSourceBooleanField("checked");
ListGrid code:
listGrid.addRecordClickHandler(new RecordClickHandler() {
#Override
public void onRecordClick(RecordClickEvent event) {
Record rec = event.getRecord();
boolean checked = rec.getAttributeAsBoolean("checked");
if(checked){
...
}else{
...
}
rec.setAttribute("checked", !checked);
catPgrid.saveAllEdits();
catPgrid.refreshFields();
}
});
ListGridField checkField = new ListGridField("checked", "Sel");
Maybe getSelectedRecords() method would help you!
Here is an API reference: http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/widgets/grid/ListGrid.html#getSelectedRecords()
Definitely this will provide all records which are selected (using checkbox) but there should be some values which you could use for identifying each record uniquely!
I'm trying to update the content of a combo box (using Griffon 1.2.0, with the JavaFX plugin).
My model:
class MyModel {
List monthList = FXCollections.observableList([new DateMidnight()])
def convertDate = [
fromString: { String s ->
return new DateMidnight(DateTimeFormat.forPattern("yyyy-MM").parseDateTime(s))
},
toString: { DateMidnight d ->
return "2011-10"
}
] as StringConverter
}
My view contains:
comboBox(items: (model.monthList), converter: model.convertDate)
Now I have a controller action which gets invoked when they push a button:
def load = {
execInsideUIAsync {
def months = myService.buildMonthList()
model.monthList.addAll(months)
}
}
The problem is that the combo box content never changes. Can anyone help me understand what I'm missing?
There's no documentation on ComboBox yet http://groovyfx.org/docs/guide/single.html#choiceBoxComboBox
Also, have I implemented the converter correctly?
The problem is that GroovyFX.comboBox creates a new List instead of using the one you pass as argument for items: This problem occurs with tableView as well. A temporary workaround would be to set the items property directly, like this
comboBox(id: 'combo')
noparent { combo.items = model.monthList }