我的网络应用程序中有一个简单的网格,编码如下:
Grid users_list = new Grid();
users_list.addColumn("username", String.class);
users_list.addColumn("email", String.class);
我想在每个填充网格的条目中添加鼠标监听器(鼠标左键)。这在Vaadin有可能吗?
答案 0 :(得分:3)
使用SelectionListener或实现纯JavaScript解决方案。 SSCCE为此:
protected void init(VaadinRequest request)
{
VerticalLayout layout = new VerticalLayout();
Collection<Person> people = new ArrayList<Person>();
people.add(new Person("Nicolaus Copernicus", 1543));
people.add(new Person("Galileo Galilei", 1564));
people.add(new Person("Johannes Kepler", 1571));
BeanItemContainer<Person> container = new BeanItemContainer<Person>(Person.class, people);
Grid grid = new Grid(container);
layout.addComponent(grid);
com.vaadin.ui.JavaScript.getCurrent().addFunction("rowClicked", new JavaScriptFunction()
{
@Override
public void call(JsonArray arguments)
{
System.out.println(arguments.get(0).toString());
}
});
com.vaadin.ui.JavaScript.getCurrent().execute("addRowListener()");
setContent(layout);
}
JavaScript的:
function addRowListener() {
var table = document.getElementsByClassName("v-grid-tablewrapper")[0];
table = table.getElementsByTagName("table")[0];
var rows = table.getElementsByTagName("tr");
for (i = 0; i < rows.length; i++) {
var currentRow = table.rows[i];
var createClickHandler =
function(row)
{
return function() {
var cell = row.getElementsByTagName("td")[0];
var id = cell.innerHTML;
rowClicked(id);
};
};
currentRow.onclick = createClickHandler(currentRow);
}
}
您可以使用导入com.vaadin.annotations.JavaScript
注释在MainUI类的顶部附加JavaScript。您需要将JavaScript文件放在与MainUI类完全相同的包中。