我正在关注Cell Table的GWT展示示例,但无法让它完全符合我的要求。我正在向单元格表传递一个成员列表,每个成员都有一个数字的arraylist。我想把这个数字列表放在selectionCell中,但据我所知,它不可能。 GWT示例分别查询类别,然后放入选择单元格,同时我希望选择单元格由传递给表格的对象填充。这是相关的GWT代码
final Category[] categories = ContactDatabase.get().queryCategories();
List<String> categoryNames = new ArrayList<String>();
for (Category category : categories) {
categoryNames.add(category.getDisplayName());
}
SelectionCell categoryCell = new SelectionCell(categoryNames);
Column<ContactInfo, String> categoryColumn = new Column<ContactInfo, String>(
categoryCell) {
@Override
public String getValue(ContactInfo object) {
return object.getCategory().getDisplayName();
}
};
这是我到目前为止的尝试
List<String> lotNumbers = new ArrayList<String>();
//this doesn't work, I can't call object.getLotNumbers();
SelectionCell lotNumberCell = new SelectionCell(lotNumbers);
Column<Member, String> lotNumberColumn = new Column<Member, String>(lotNumberCell) {
@Override
public String getValue(Member object) {
return object.getLotNumbers().get(0);
}
};
答案 0 :(得分:2)
SelectionCell未设置为动态呈现内容,它使用字符串的静态列表在每次显示时呈现下拉列表。如果你想拥有一个动态下拉列表,你必须创建一个实现Cell<List<String>>
的新单元格类型或者你可以迭代生成下拉列表的其他类型。
答案 1 :(得分:0)
这是我对DynamicSelectionCell的实现。
使用单个DynamicSelectionCell对象并使用 addOption 方法为每行添加选项。选项存储在Map中,Key是行号。
对于表格中的每一行 $ i ,将呈现存储在密钥 $ i 的Map中的选项。
适用于DataGrid,CellTable。
<强> CODE 强>
public class DynamicSelectionCell extends AbstractInputCell<String, String> {
public TreeMap<Integer, List<String>> optionsMap = new TreeMap<Integer, List<String>>();
interface Template extends SafeHtmlTemplates {
@Template("<option value=\"{0}\">{0}</option>")
SafeHtml deselected(String option);
@Template("<option value=\"{0}\" selected=\"selected\">{0}</option>")
SafeHtml selected(String option);
}
private static Template template;
private TreeMap<Integer, HashMap<String, Integer>> indexForOption = new TreeMap<Integer, HashMap<String, Integer>>();
/**
* Construct a new {@link SelectionCell} with the specified options.
*
* @param options the options in the cell
*/
public DynamicSelectionCell() {
super("change");
if (template == null) {
template = GWT.create(Template.class);
}
}
public void addOption(List<String> newOps, int key){
optionsMap.put(key, newOps);
HashMap<String, Integer> localIndexForOption = new HashMap<String, Integer>();
indexForOption.put(ind, localIndexForOption);
refreshIndexes();
}
public void removeOption(int index){
optionsMap.remove(index);
refreshIndexes();
}
private void refreshIndexes(){
int ind=0;
for (List<String> options : optionsMap.values()){
HashMap<String, Integer> localIndexForOption = new HashMap<String, Integer>();
indexForOption.put(ind, localIndexForOption);
int index = 0;
for (String option : options) {
localIndexForOption.put(option, index++);
}
ind++;
}
}
@Override
public void onBrowserEvent(Context context, Element parent, String value,
NativeEvent event, ValueUpdater<String> valueUpdater) {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
String type = event.getType();
if ("change".equals(type)) {
Object key = context.getKey();
SelectElement select = parent.getFirstChild().cast();
String newValue = optionsMap.get(context.getIndex()).get(select.getSelectedIndex());
setViewData(key, newValue);
finishEditing(parent, newValue, key, valueUpdater);
if (valueUpdater != null) {
valueUpdater.update(newValue);
}
}
}
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
// Get the view data.
Object key = context.getKey();
String viewData = getViewData(key);
if (viewData != null && viewData.equals(value)) {
clearViewData(key);
viewData = null;
}
int selectedIndex = getSelectedIndex(viewData == null ? value : viewData, context.getIndex());
sb.appendHtmlConstant("<select tabindex=\"-1\">");
int index = 0;
try{
for (String option : optionsMap.get(context.getIndex())) {
if (index++ == selectedIndex) {
sb.append(template.selected(option));
} else {
sb.append(template.deselected(option));
}
}
}catch(Exception e){
System.out.println("error");
}
sb.appendHtmlConstant("</select>");
}
private int getSelectedIndex(String value, int ind) {
Integer index = indexForOption.get(ind).get(value);
if (index == null) {
return -1;
}
return index.intValue();
}
}