Search code examples
formsattributessmartgwt

Change form item editor type dynamically in smartGwt


I have a smartGwt DynamicForm with a FormItem

FormItem item = createTextItem();           
form.setFields(item);

After creating the and setting fields, I need to dynamically set an editor type for the item. I have to do it dynamically based on some conditions.

I'm calling item.setEditorType(new PasswordItem()); just after I call form.editRecord(record); so that the new editor type should appear. But it is not working.
Tried calling item.redraw() and is not working.

My goal is to set the editor type dynamically based on the record that is edited.Please help.


Solution

  • Try with Custom Data Binding (see page 23 for more details). What you tried won't work, AFAIK, because the ListGridField has already been created with the initial custom editor, and it can't be changed dynamically with setEditorCustomizer.

    Take a look at this sample (based on this showcase demo), which does what you want to do to the password field when it is being edited in the DynamicForm, and after the changes have been saved (please pay attention to the comments, as without some of these settings it won't work as expected):

    public void onModuleLoad() {  
    
        final DataSource dataSource = ItemSupplyLocalDS.getInstance();  
    
        final DynamicForm form = new DynamicForm();  
        form.setIsGroup(true);   
        form.setNumCols(4);  
        form.setDataSource(dataSource);
        // very important for not having to set all fields all over again
        // when the target field is customized
        form.setUseAllDataSourceFields(true); 
    
        final ListGrid listGrid = new ListGrid();  
        listGrid.setWidth100();  
        listGrid.setHeight(200);  
        listGrid.setDataSource(dataSource);  
        listGrid.setAutoFetchData(true);  
    
        IButton editButton = new IButton("Edit");  
        editButton.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) { 
                form.editRecord(listGrid.getSelectedRecord()); 
                // when the button is clicked, the password field is rendered with 
                // a plain text item editor, for easy verification of values entered
                FormItem passwordField = new FormItem("passwordFieldName");
                passwordField.setEditorProperties(new TextItem());              
                form.setFields(passwordField);  
                form.markForRedraw();
            }  
        });  
    
        IButton saveButton = new IButton("Save");  
        saveButton.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) {  
                form.saveData(); 
                // when the button is clicked, the password field is rendered with 
                // a password editor, for added privacy/security
                FormItem passwordField = new FormItem("passwordFieldName");
                passwordField.setEditorProperties(new PasswordItem());              
                form.setFields(passwordField);  
                form.markForRedraw();
            }  
        });  
    
        VLayout layout = new VLayout(15);  
        layout.setWidth100();
        layout.setHeight100();
        layout.addMember(listGrid);  
        layout.addMember(editButton); 
        layout.addMember(form);  
        layout.addMember(saveButton);  
        layout.draw();  
    }