Search code examples
smartgwtlistgrid

Customize SmartGwt ListGrid dynamically for passwords


I have a com.smartgwt.client.widgets.grid.ListGrid for my configurations screen.
I have 3 ListGridFields name, value, isHidden.
I want to use PasswordItem if isHidden is true, and TextItem if isidden is false.

How can I customize the grid?

I tried with setEditorCustomizer, but it only works when I am editing a cell. In view mode I am able to see the text.


Solution

  • I don't think there's a way to do what you want (show the PasswordItem editor when visualizing the ListGrid's fields). As you already found out, setEditorCustomizer works only when in editing mode.

    But you can mask the field values. Here is how to do it:

    // very important for not having to set all fields all over again
    // when the target field is customized
    listGrid.setUseAllDataSourceFields(true);
    
    // customize the isHidden field to make it respond to changes and
    // hide/show the password field accordingly
    ListGridField isHidden = new ListGridField("isHiddenFieldName");
    isHidden.addChangedHandler(new ChangedHandler() {
        @Override
        public void onChanged(ChangedEvent event) {
            // the name of this field has to match the name of the field you
            // want to hide (as defined in your data source descriptor, 
            // ListGridField definition, etc).
            ListGridField passwordField = new ListGridField("passwordFieldName");
            if ((Boolean) event.getValue() == true) {
                passwordField.setCellFormatter(new CellFormatter() {
                    @Override
                    public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                        return ((String) value).replaceAll(".", "*");
                    }
                });
            }
            // you need to re-add here the isHidden field for the ChangeHandler to
            // be present when recreating the ListGrid
            listGrid.setFields(isHidden, passwordField);  
            listGrid.markForRedraw();
        }
    });
    // add the customized field to the listGrid, so that we can have the
    // desired ChangeHandler for the isHidden field
    listGrid.setFields(isHidden);