2015-02-23 83 views
1

我想讓我的flextables列可調整大小。 因此,用戶可以決定列的寬度。 一個解決方案是使用CSS resize屬性,但這在IE中不起作用。 有人有一個想法如何使這在IE中工作?使GWT Flextable列可調整大小

我的想法是趕上onBrowserEvent和註冊一些鼠標手,但我沒有得到正確的元素來調整大小。

我h你能幫助我。 在此先感謝!

回答

1

嗨我找到了我自己的解決方案。也許這可以幫助某人。

public class ResizableFlexTable 
    extends FlexTable 
{ 
    private int mouseDownX; 
    private boolean mouseDown; 
    private int columnWidth; 
    private TableCellElement headerElem; 
    private int column; 
    private ResizeMode resizeMode; 

    /** 
    * 
    */ 
    public ResizableFlexTable() 
    { 
    super(); 
    sinkEvents(Event.ONMOUSEMOVE); 
    sinkEvents(Event.ONMOUSEDOWN); 
    sinkEvents(Event.ONMOUSEUP); 
    sinkEvents(Event.ONCLICK); 
    Event.setEventListener(getElement(), this); 
    } 

    @Override 
    public void onBrowserEvent(Event event) 
    { 
    if (event.getType().equals("change")) 
    { 
     super.onBrowserEvent(event); 
     return; 
    } 

    boolean eventMouseUp = event.getType().equals("mouseup"); 
    boolean eventClick = event.getType().equals("click"); 
    boolean eventMouseDown = event.getType().equals("mousedown"); 
    boolean eventMouseMove = event.getType().equals("mousemove"); 

    if (eventMouseUp) 
    { 
     mouseDownX = -1; 
     mouseDown = false; 
    } 
    if (resizeMode == ResizeMode.RESIZING && eventClick) 
    { 
     resizeMode = ResizeMode.ENDING; 
    } 

    TableCellElement tableCellElement = findNearestParentCell((Element) event.getEventTarget() 
     .cast()); 
    if (tableCellElement == null) 
    { 
     super.onBrowserEvent(event); 
     return; 
    } 

    Element trElem = tableCellElement.getParentElement(); 
    if (trElem == null) 
    { 
     super.onBrowserEvent(event); 
     return; 
    } 
    TableRowElement tr = TableRowElement.as(trElem); 
    // Element sectionElem = tr.getParentElement(); 
    if (tr == null) 
    { 
     super.onBrowserEvent(event); 
     return; 
    } 

    int col = tableCellElement.getCellIndex(); 
    if (tr == getTableHeadElement()) 
    { 
     int mouseX = event.getClientX(); 
     int right = tableCellElement.getAbsoluteLeft() + tableCellElement.getOffsetWidth(); 
     if (!mouseDown && eventMouseDown) 
     { 
     if (mouseX <= right && mouseX >= right - 10) 
     { 
      headerElem = tableCellElement; 
      column = col; 
      mouseDownX = event.getClientX(); 
      mouseDown = true; 
      columnWidth = headerElem.getOffsetWidth(); 
     } 
     } 
     else if (eventMouseMove) 
     { 
     if (mouseDown || mouseX <= right && mouseX >= right - 10) 
     { 
      tableCellElement.getStyle().setCursor(Cursor.COL_RESIZE); 
     } 
     else 
     { 
      tableCellElement.getStyle().setCursor(Cursor.AUTO); 
     } 
     } 
    } 
    else 
    { 
     super.onBrowserEvent(event); 
    } 

    if (headerElem != null && eventMouseMove && mouseDown) 
    { 
     int move = event.getClientX() - mouseDownX; 
     @SuppressWarnings ("hiding") 
     int columnWidth = this.columnWidth + move; 
     if (columnWidth > 24) 
     { 
     getColumnFormatter().getElement(column).setAttribute("width", columnWidth + "px"); 
     resizeMode = ResizeMode.RESIZING; 
     } 
    } 

    if (resizeMode != ResizeMode.RESIZING && resizeMode != ResizeMode.ENDING && eventClick) 
    { 
     super.onBrowserEvent(event); 
    } 
    else if (resizeMode == ResizeMode.ENDING) 
    { 
     resizeMode = ResizeMode.NO; 
    } 
    } 

    /** 
    * @return 
    */ 
    private TableRowElement getTableHeadElement() 
    { 
    TableRowElement row = getRowFormatter().getElement(0).cast(); 
    return row; 
    } 

    /** 
    * Diese Methode ist 1 zu 1 aus der Oberklasse übernommen. Nicht ändern!!! 
    */ 
    private TableCellElement findNearestParentCell(Element elem) 
    { 
    while ((elem != null) && (elem != getElement())) 
    { 
     String tagName = elem.getTagName(); 
     if ("td".equalsIgnoreCase(tagName) || "th".equalsIgnoreCase(tagName)) 
     { 
     return elem.cast(); 
     } 
     elem = elem.getParentElement(); 
    } 
    return null; 
    } 
} 

這不是很好,但它適用於我。