Dorado 7 : 01. 全局监听器范例(SEFC)

这是一个调整DataGrid表格列默认操作功能的实例:它将默认单击表格头打开表格列选择的功能关闭,并调整为表格列排序的功能。

public class DataGridListener extends GenericObjectListener<DataGrid> {

	public boolean beforeInit(DataGrid grid) throws Exception {

		return true;
	}

	public void onInit(DataGrid grid) throws Exception {
		List<Column> columns = grid.getColumns();
		for (Column column:columns){
			if (column instanceof ColumnGroup){
				List<Column> subColumns = ((ColumnGroup) column).getColumns();
				for (Column subColumn:subColumns){
					if (supportSort(subColumn)) attachSortEvent((DataColumn)subColumn);
				}
			}else{
				if (supportSort(column)) attachSortEvent((DataColumn)column);
			}
		}

	}
	
	private void attachSortEvent(DataColumn column){
		List<ClientEvent> listeners = column.getClientEventListeners("onHeaderClick");
		if (listeners!=null && listeners.size()>=1){
			//do nothing
		}else{
			String script = "var sortState = self.get('sortState');" +
					"if (sortState=='asc'){"+
						"self.get('grid').sort(self, true);"+
					"}else{"+
						"self.get('grid').sort(self, false);"+
					"};";		
			column.addClientEventListener("onHeaderClick", new DefaultClientEvent(script));
		}
	}
	
	private boolean supportSort(Column column){
		return (column instanceof DataColumn) ;
	}

}