编辑:
我有一个SmartGWT / PureMVC项目。在MyMediator的initView()中,我使用DataSource创建一个ListGrid。它表现得很好。这是myDataSource代码:
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)
}
}
我在myViewGrid中有以下设置,我在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);
到此为止,一切都很好。然后,根据一些通知,在继承的PureMVC handleNotification方法中,我想显示隐藏列:
// 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
因此专栏始终隐藏! 请帮助!
答案 0 :(得分:0)
myListGrid.setShowHiddenFields(true);
将显示隐藏的字段。希望这对你有用。
答案 1 :(得分:0)
好的,我有答案:
1.为什么会这样?答案是setHidden(true)
实际上是从网格中删除列。我使用方法myListGrid.getFields()
检查了它,并且在执行setHidden
方法后我有一个字段LESS。该字段仍存在于myDataSource中,但在myListGrid中为null
。
2.如何解决这个问题?我使用showIf
条件修复了它:
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();
}
});
然后,我在需要时再次呼叫myListGrid.refreshFields()
(通知到达时)。
感谢您的意见和帮助,但是:)