Set maxLength attribute on ComboBox - vaadin

I want to add a maxLength to the custom value on the ComboBox but when I do
getElement().setAttribute("maxLength","30");
Vaadin add it to the wrapping vaadin-combo-box element. How can I access from Java the underlying highlighted input element as show in the picture?

Applying a max-length on the client-side does not guarantee that the value on the server-side is secured.
Instead of mingling with the inner workings of the ComboBox's field, I would suggest to apply this validation on the server-side. In the ComboBox::addCustomValueSetListener or by using a StringLengthValidator on the Binder you have attached to the form/field.

Vaadin ComboBox doesn't support setting a max length to custom values (although TextField does).
You could create a feature request ticket about it here: https://github.com/vaadin/web-components/issues/new?assignees=&labels=&template=feature-request.yml

I found out that you can executejs on client side through element API. I solved using this snippet:
element.executeJs("this.childNodes.item(0).maxLength = 30")
"childNodes.item(0)" is the underlying input element of combobox

Related

What is the replacement for ListSelect in Vaadin 22?

I'm trying to update an application from Vaadin 7/8 to Vaadin 22.
ListSelect is gone, so I need a new widget field that satisfies these requirements:
Allows choosing multiple items
Suitable for use in FormLayout with value bound using Binder
Scrolls automatically when there are more than a handful of choices
None of the widgets in Vaadin 22 seem to qualify:
Select and ComboBox only choose one item
ListBox says ...designed to be used as a lightweight scrollable selection list rather than a form input field
Grid does not implement HasValue so can't be used with Binder
CheckBoxGroup doesn't look like it will scroll (requirement #3) (I haven't actually tried yet though)
What am I missing here?? What is the new equivalent of the old ListSelect?
If your Grid is set to multiselect mode, you can use grid.asMultiSelect() to return a MultiSelect object that does implement HasValue and can be used with Binder. Similarly, in single-select mode, grid.asSingleSelect() returns a bindable SingleSelect object.

ComboBox with hidden keys

Is it possible in vaadin 12 to have comboboxes such that it displays a user-friendly value but behind the scenes it stores a hidden code? I found this example from 5 years ago but it doesn't apply to Vaadin 12 comboboxes: https://vaadin.com/forum/thread/7821327/combo-box-hidden-values
(If there's a good, reasonably clean way to do it, please point me in the right direction! I would think this is a common sought-after feature)
Items are assigned to ComboBox either directly using setItems or indirectly through setDataProvider.
The item itself is not sent to the browser and shown in the dropdown. Instead, for each item ComboBox generates a string label that is shown in the UI and an internal id that is used on the server for mapping back to the original item instance when the user makes a selection.
The generated id is internal to ComboBox and has no external meaning. In particular, it's not based on any value in the item itself such as the item's primary key in the application's database.
The label is by default based on doing toString() for each item. You can customize how the label is created by assigning an item label generator callback that receives an item instance and returns the label string to use for that item.
If you for example have a combo box for selecting persons, then you could configure it in e.g. this way:
ComboBox<Person> personSelector = new ComboBox<>();
personSelector.setItems(allPersons);
personSelector.setItemLabelGenerator(person ->
person.getFirstName() + " " + person.getLastName());
If I understand you correctly, there is built-in feature in ComboBox for this, the method is called setItemLabelGenerator(..), which allows to define e.g. lambda expression that returns String which is used for ComboBox items instead of the property from underlying data object.
The linked Forum discussion you found is about similar thing in our previous generation of the framework, there has been some renaming of the API here.

Columns order in ListGrid Smartgwt

How can I programmatically change the order of columns in ListGrid? I have search for an appropriate method in javadoc but found nothing. Is it possible?
Check out setViewState method and this example that saves and restores grid preferences (including column order). So in short, you are interested in getting viewState string from ListGrid data and using it. Remember to redraw grid after setting new viewState.

How to add a non-bound column to a DevExpress DB QuantumGrid

I am using these components:
UniDac for connection to mysql database
DevExpress for QuantumGrid
IDE:
Embarcadero Rad Studio XE2
I have a cxGrid component with one level and a cxGrid1DBTableView specified as the level's View. I can get data from my database and edit it in the grid. I want to add a column that is not in the bound DataSet. When I specify the Column properties value as CheckBox I can see the column but I can't change the value from unchecked to checked by clicking it. The field doesn't have a DataBinding assigned to it. I tried other types of Properties but all are the same I cant change the row value in the grid.
I've been searching for a way to fix this for couple of days, so im hoping you guys can help me.
Are you trying to add a checkbox item that does not have a database field behind it? I have this on one of my forms.
In addition to setting the Properties to "Checkbox" you need to set the DataBinding -> ValueType to "Boolean". The DataBinding->FieldName can be left blank.
To access the values or change their defaults you can use the DataController like this:
View.DataController.Values[i, CheckBoxFieldIndex] := true;
In addition you need to set
DataController.DataModeController.SmartRefresh := true;
To set that option you will also need a KeyField defined for the Controller (DataController.KeyFieldNames)
Setting the SmartRefresh to true will prevent the grid from trying to get an updated value from the underlying dataset. You need to prevent the refresh or the value for the non-bound column will be set back to Null. This comes with some restriction on how you update your dataset. Any changes made to the data in code will not be reflected by the grid unless you explicitly refresh the grid.
You also have to fill the field View.DataController.KeyFieldNames with one of the datasets fields. At least I need it in Delphi 7.

Custom ViewHandler to override javax.faces.viewstate hidden field value

Can we change the value of the inputHidden field "javax.faces.viewState" before the page render.
For the field
The value should be changed to a different length value. Can this be done by using a custom viewHandler?
Can we achieve this by extending the class to ResponseWriter.
The field is rendered by the ResponseStateManager that you obtain from the current render kit.
If you look up its API you'll see that you can't just override the field's value. You have to replace the entire thing! Since state saving is quite complex (think about both server and client algos), I would think twice about attempting this.
An alternative is using a Servlet filter to capture the entire response. The hidden field's name is standardized and you could search and replace on it. For a postback you could use the same filter to restore the param.

Resources