Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to create a custom FormItem to handle a many-to-one relationship?

    Hi list!

    I'm trying to create a custom FormItem to handle a many-to-one relationship. Until now, I have a ManyToOneSelectItem to handle a user selection, with a SelectionWindow. The code:

    Code:
    public class ManyToOneSelectItem extends TextItem implements SelectionChangedHandler {
    
        private DataSource dataSource;
    
        public ManyToOneSelectItem(final String name, String title) {
            super(name, title);
    
            setShouldSaveValue(true);
    
            PickerIcon searchPicker = new PickerIcon(PickerIcon.SEARCH, new FormItemClickHandler() {
    
                @Override
                public void onFormItemClick(FormItemIconClickEvent event) {
                    SelectionWindow winModal = new SelectionWindow(dataSource, true);
                    winModal.getGrid().addSelectionChangedHandler(ManyToOneSelectItem.this);
                    winModal.show();
                }
            });
    
            setIcons(searchPicker);
    
        }
    
        @Override
        public void setOptionDataSource(DataSource dataSource) {
            super.setOptionDataSource(dataSource);
            this.dataSource = dataSource;
        }
    
        @Override
        public void onSelectionChanged(SelectionEvent event) {
            this.setValue(event.getSelectedRecord().getAttribute(getValueField()));
        }
    }
    Code:
    public class SelectionWindow extends Window {
    
        private ListGrid grid = GridFactory.getInstance().simple();
    
        public SelectionWindow(DataSource ds, boolean single) {
            AppMessages messages = GWT.create(AppMessages.class);
            this.setWidth("80%");
            this.setHeight("60%");
            this.setTitle(messages.selectionWindow());
            this.setShowMinimizeButton(false);
            this.setIsModal(true);
            this.centerInPage();
    
    
    
            final IButton okButton = ButtonFactory.apply();
            okButton.setDisabled(true);
            grid.addSelectionChangedHandler(new SelectionChangedHandler() {
    
                @Override
                public void onSelectionChanged(SelectionEvent event) {
                    if (grid.anySelected()) {
                        okButton.setDisabled(false);
                    }
                }
            });
    
            okButton.addClickHandler(new ClickHandler() {
    
                @Override
                public void onClick(ClickEvent event) {
                    destroy();
                }
            });
    
            
            HLayout buttonLayout = new HLayout();
            buttonLayout.addMember(okButton);
    
            VLayout layout = new VLayout();
            layout.setWidth100();
            layout.setHeight100();
    
            grid.setDataSource(ds);
            grid.setShowFilterEditor(true);
            if (single) {
                grid.setSelectionType(SelectionStyle.SINGLE);
            } else {
                grid.setSelectionType(SelectionStyle.SIMPLE);
            }
            grid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
    
            layout.addMember(grid);
            layout.addMember(buttonLayout);
    
            this.addItem(layout);
    
        }
    
        public ListGrid getGrid() {
            return grid;
        }
        
    }
    I'm setting the optionDataSource, the valueField and the displayField on the ManyToOneSelectItem, and everything works fine when showing the form, the right text is shown, and the right value is send when any other field is changed and I call saveData(). But now I want to set a value to this form field, I'm using the FormItem.setValue() method without success.

    How can I set a value to the form field, in a way that the form text is updated, and the right value is sent when calling saveData()?

    Thanks in advance, and apologize my bad English.

    #2
    Anyone does know how to set a field value from a databound form? How to select a value for a field with a many-to-one relationship with the main entity?

    Thanks!

    Comment

    Working...
    X