datasourcefieldhiddensmartgwtlistgrid

SmartGWT DataSourceField.setHidden not showing the field hidden?


EDITED:
I have a SmartGWT/PureMVC project. In the initView() of MyMediator, I create a ListGrid with DataSource. It shows well. Here is myDataSource code:

public class MyDataSource extends DataSource {
    private static MyDataSource instance;
    private static final String COLUMN_ONE = "One";
    private static final String COLUMN_TWO = "Two";
    public static MyDataSource getInstance() {
        if (instance == null) instance = new MyDataSource("ID");
        return instance;
    }
    private MyDataSource(String id) {
        setDataProtocol(DSProtocol.CLIENTCUSTOM);
        setDataFormat(DSDataFormat.CUSTOM);
        setClientOnly(false);
        constructDataSource(id);
    }
    private void constructDataSource(String id) {
        setID(id);
        DataSourceTextField one = new DataSourceTextField(COLUMN_ONE);
        DataSourceTextField two= new DataSourceTextField(COLUMN_TWO);
        setFields(one, two);
    }
    protected Object transformRequest(DSRequest request){
        DSResponse response = new DSResponse();
        response.setAttribute("clientContext", request.getAttributeAsObject("clientContext"));
        response.setStatus(0);
        switch (request.getOperationType()) {
            case FETCH:
                executeFetch(request.getRequestId(), request, response);
            // and the other operation types
        }
        return request.getData();
    }
    protected void executeFetch(final DSRequest request, final DSResponse response) {

        // call Async method to fetch data from database
        // do response.setData() with the list of records received
        // processResponse(request.getRequestId(), response)
    }
}

I have the following settings to myListGrid which I create in initView():

// myDataSource is a private field of myMediator, so that I can access it in the notifications
myDataSource = MyDataSource.getInstance();

myListGrid.setShowRecordComponents(true);
myListGrid.setShowRecordComponentsByCell(true);
myListGrid.setDataSource(myDataSource);
myListGrid.setCanEdit(false);
myListGrid.setCanCollapseGroup(false);
myListGrid.setFreezeFields(false);
myListGrid.setCanGroupBy(false);
myListGrid.setCanMultiSort(false);
myListGrid.setCanSort(false);
myListGrid.setCanAutoFitFields(false);
myListGrid.setCanResizeFields(false);
myListGrid.setCanPickFields(false);
myListGrid.setAutoFetchData(false);
// fetchData() in an onDraw handler

// upon first launch, column "One" should be hidden
myDataSource.getField(MyDataSource.COLUMN_ONE).setHidden(true);

Up to here, everything is fine. Then, upon some notification, in the inherited PureMVC handleNotification method I want to show the hidden column :

// Test 1
myDataSource.getField(MyDataSource.COLUMN_ONE).setHidden(false); // not working

// Test 2
myDataSource.getField(MyDataSource.COLUMN_ONE).setHidden(false);
myListGrid.redraw(); // not working

// Test 3
myDataSource.getField(MyDataSource.COLUMN_ONE).setHidden(false);
myListGrid.refreshFields(); // not working

// Test 4
myDataSource.getField(MyDataSource.COLUMN_ONE).setHidden(false);
myDataSource.invalidateCache(); // not working

// Test 5
myDataSource.destroy();
myDataSource = MyDataSource.getInstance();
myListGrid.setDataSource(myDataSource);
myListGrid.fetchData(); // not working because getInstance() method returns the old instance in which the column is hidden

// Test 6
myListGrid.setShowHiddenFields(true); // not working

So the column stays always hidden! Please, help!


Solution

  • Ok, I have the answers:

    1. Why does this happen? The answer is that setHidden(true) is actually REMOVING the column from the grid. I checked it with the method myListGrid.getFields() and I have one field LESS after executing the setHidden method. The field still exists in myDataSource, but it is null in myListGrid.

    2. How to fix this? I fixed it by using the showIf condition:

    myListGrid.addDrawHandler(new DrawHandler() {
        public void onDraw(DrawEvent event) {
            myListGrid.getField(MyDataSource.COLUMN_ONE).setShowIfCondition(new ListGridFieldIfFunction() {
                @Override
                public boolean execute(ListGrid grid, ListGridField field, int fieldNum) {
                    // write the condition needed
                    // return true/false to respectively show/hide the field
                }
            });
            myListGrid.refreshFields();
        }
    });
    

    Then, I call myListGrid.refreshFields() again when needed (upon notification arrived for ex.). Thank you for your comments and help, however :)