如何在安全的 html 上创建事件。因为我想对单元格表中的列进行过滤。所以我在表格的标题处添加了搜索框。我想访问文本框并在文本框上做一些事件。
代码:
final MultiSelectionModel<MasterTable> selectionModel=new MultiSelectionModel<MasterTable>();
CellTable<MasterTable> cell=new CellTable<MasterTable>();
cell.setSelectionModel(selectionModel, DefaultSelectionEventManager.<MasterTable> createCheckboxManager());
Column<MasterTable, Boolean> checkColumn = new Column<MasterTable, Boolean>(new CheckboxCell(true, false)) {
@Override
public Boolean getValue(MasterTable object) {
return selectionModel.isSelected(object);
}
};
cell.addColumn(checkColumn);
cell.setColumnWidth(checkColumn, 20, Unit.PX);
personCollection.getList().add(new MasterTable("Art", "Shake", 21));
personCollection.getList().add(new MasterTable("Joe", "Black", 47));
personCollection.getList().add(new MasterTable("Sam", "PArish", 97));
TextColumn<MasterTable> namecolumn = new TextColumn<MasterTable>() {
@Override
public String getValue(MasterTable s) {
return s.getName();
}
};
TextColumn<MasterTable> lastNameColumn = new TextColumn<MasterTable>() {
@Override
public String getValue(MasterTable s) {
return s.getLastName();
}
};
TextColumn<MasterTable> ageColumn = new TextColumn<MasterTable>() {
@Override
public String getValue(MasterTable s) {
return Integer.toString(s.getAge());
}
};
// cell.addColumn(namecolumn,"Name");
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.appendHtmlConstant("<input type=\"text\" id=\"nameInput\" /><br/><br/><select><option>one</option></select><br/><center><b>Name</b></center>");
cell.addColumn(namecolumn,builder.toSafeHtml());
SafeHtmlBuilder builder1 = new SafeHtmlBuilder();
builder1.appendHtmlConstant("<input type=\"text\"/><br/><br/><select><option>one</option></select><br/><center><b>Last name</b></center>");
cell.addColumn(lastNameColumn,builder1.toSafeHtml());
SafeHtmlBuilder builder2 = new SafeHtmlBuilder();
builder2.appendHtmlConstant("<input type=\"text\"/><br/><br/><select><option>one</option></select><br/><center><b>Age</b></center>");
cell.addColumn(ageColumn,builder2.toSafeHtml());
personCollection.addDataDisplay(cell);
verticalPanel.add(searchpanel);
verticalPanel.add(cell);
RootPanel.get("errorLabelContainer").add(verticalPanel);
final Element nameInput = DOM.getElementById("nameInput");
if(nameInput!=null)
{
Event.sinkEvents(nameInput, Event.ONCHANGE);
Event.setEventListener(nameInput, new EventListener() {
@Override
public void onBrowserEvent(Event event) {
personCollection.setFilter(nameInput.getInnerText());
personCollection.refresh();
}
});
}
我得到的第一个问题nameInput是null。为什么?
这只是样本。我想动态创建此列。那么是否可以为运行时生成的 DOM 元素动态添加 DOM 事件?