2016-03-07 71 views
0

我在代碼中主要使用GWT。我現在有一個TextInputCell列:基於this SO answer我知道我可以在GWT的佔位符文本(或任何其他財產)添加到正規的文本字段這樣將屬性添加到單元格(在代碼中)

public class EditPanel extends Composite{ 

    @UiField 
    protected DataGrid<MyObject> dataGrid; 

    public EditPanel(final ActionHandler actionHandler){ 
     ... 

     Column<MyObject, String> inputColumn = new Column<MyObject, String>(new TextInputCell()){ 
      ... // Override of the getValue 
     }; 
     inputColumn.setFieldUpdater(...); 
     this.dataGrid.addColumn(inputColumn, "Column title"); 

     ... 
    } 

    ... 
} 

TextField myInputField = new TextField(); 
myInputField.getElement().setPropertyString("placeholder", "some placeholder text"); 

但是,在TextInputCell上,並沒有真正的getElement()方法來檢索輸入字段。

當通過我整個protected getInputElement(parent)方法傳來TextInputCell類代碼看,所以我也設法得到一個佔位符以下解決方法:

final TextInputCell myInputCell = new TextInputCell(){ 

    @Override 
    protected InputElement getInputElement(final Element parent){ 
     final InputElement inputElement = super.getInputElement(parent); 
     inputElement.setPropertyString("placeholder", "my placeholder text"); 
     return inputElement; 
    } 
}; 
Column<MyObject, String> inputColumn = new Column<MyObject, String>(myInputCell){ 
    ... 
}; 

它的工作原理,但我有兩個問題:

  1. 顯然,這是非常醜陋的設置像這樣..
  2. getInputElement(parent) - 方法最初並不叫。當我關注輸入字段之一時,我確實佔據了佔位符,但該屬性並不總是默認添加。

有沒有人有實際的解決方案,以便將屬性添加到(TextInput)單元格的一列,而不是這個醜陋也許工作解決?


編輯:有些事情我已經試過:

1)試圖檢索與getRowElement方法的元素,就像在this SO question & answer

this.dataGrid.addColumn(inputColumn, "Column title"); 
if (this.dataGrid.getRowCount() > 0){ 
    final Element element = this.dataGrid.getRowElement(0); 
    element.setProperty("placeholder", "some placeholder text"); 
} 

這並不因爲getRowCount()工作總是返回0.我也試過this.dataGrid.redraw();之前,但它仍然返回0.

2)覆蓋TextInputCellgeert3建議:

public class MyTextInputCell extends TextInputCell{ 

    @Override 
    public InputElement getInputElement(Element parent){ 
     return super.getInputElement(parent); 
    } 
} 

的問題?我不知道要爲父參數添加什麼內容。我試過this.getElement()

MyTextInputCell textInputCell = new MyTextInputCell(); 
Column<MyObject, String> inputColumn = new Column<MyObject, String>(textInputCell){ 
    ... // Override of the getValue 
}; 
inputColumn.setFieldUpdater(...); 
this.dataGrid.addColumn(inputColumn, "Column title"); 
textInputCell.getInputElement(this.getElement()).setPropertyString("placeholder", "some placeholder text"); 

但這似乎並不奏效。當我嘗試thisthis.dataGridinputColumn它給出了一個錯誤,因爲它們不被認爲是Elements(我也不能施放它們)。

3)使用我最初的解決方法。

問題:當頁面加載時,我無法真正找到強制執行getInputElement(parent)方法調用的方法,而不是當輸入字段爲焦點時。

很煩人,沒有辦法直接訪問GWT中的TextInputCell的InputField ...

回答

0

我一直在閱讀一些細胞。這些實際上被重用,單個Cell可以呈現自己的多個DOM實例,並且它可以處理來自多個呈現實例的事件。這意味着一個Cell沒有一個一對一的關聯元素,但是當它被重用時接收「current」元素,並作爲幾個方法的「父」參數傳遞。

所以最後我認爲這意味着您在問題中描述的原始解決方案似乎是一個有效的解決方案,而不是「醜陋」。