Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GWT(单元格小部件):单元格列表中带有标签的复选框_Gwt_Widget - Fatal编程技术网

GWT(单元格小部件):单元格列表中带有标签的复选框

GWT(单元格小部件):单元格列表中带有标签的复选框,gwt,widget,Gwt,Widget,我想建立一个复选框列表,如 |===========| | x label0 | | x label1 | | x label2 | |===========| …使用CellList,我做了以下工作: final List<Boolean> checks = Arrays.asList(true, false, true, true, false); final CheckboxCell checkboxCell = new CheckboxCell();

我想建立一个复选框列表,如

|===========| 
| x label0  | 
| x label1  | 
| x label2  | 
|===========| 
…使用CellList,我做了以下工作:

final List<Boolean> checks = Arrays.asList(true, false, true,  true, false); 

final CheckboxCell checkboxCell = new CheckboxCell(); 
final CellList<Boolean> checkBoxCellList = new CellList<Boolean(checkboxCell); 
checkBoxCellList.setRowData(checks); 
但是,我如何不仅提供复选框(布尔值)的值,还提供其值呢 贴上标签,使其与

CheckBox#setText("label0")

CheckBox#setValue(true)

?

我也为CellTable做了类似的事情。所以像这样的事情可能会奏效。您需要提供自己的render()方法。因此,在下面的render()方法中,可以添加任何需要的内容

Column<MyJSONObject, String> selectCheckBoxColumn =
                new Column<MyJSONObject, String>(new MyCheckBoxCell()) {

                   @Override
                   public String getValue(MyJSONObject object) {
                      return object.getInternalId() + SEPERATOR +  "true";
                   }
                };
             MyCellTable.addColumn(selectCheckBoxColumn, "Display title");

    .....
    .....
    .....

       private class MyCheckBoxCell extends AbstractCell<String> {

          @Override
          public void render(Context context, String value, SafeHtmlBuilder sb) {


             // ##############################################
             String[] values = value.split(SEPERATOR);        
             // NOW values[0] contains the string value
             // AND values[1] contains the boolean value
             // ##############################################

             sb.appendHtmlConstant("<input type='checkbox' name='" + htmlDOMId1 + "'" +
                                   " id='" + htmlDOMId1 + "'" +
                                   " value='" + value + "'" +
                                   "></input>");

          }
       }
Column选择checkboxcolumn=
新列(新的MyCheckBoxCell()){
@凌驾
公共字符串getValue(MyJSONObject对象){
返回object.getInternalId()+分隔符+“true”;
}
};
MyCellTable.addColumn(选择CheckBoxColumn,“显示标题”);
.....
.....
.....
私有类MyCheckBoxCell扩展了AbstractCell{
@凌驾
公共void呈现(上下文上下文、字符串值、安全HtmlBuilder sb){
// ##############################################
字符串[]值=值。拆分(分隔符);
//现在,值[0]包含字符串值
//和值[1]包含布尔值
// ##############################################
某人以““””号结尾;
}
}

我正在阅读的一本GWT书籍有这样一个例子,除了作者使用了一个垂直面板,面板中有复选框。通过一点样式,您可以看到一个列表框。最简单的方法是使用单元格表,而不添加列标题(因此它看起来像单元格列表)

为复选框设置一列(使用CheckBoxCell),并为其他数据设置一列。 这是最干净的方法。不要乱搞你自己的自定义单元格,里面有html复选框


这同样适用于单元格表上的MultiSelectionModel,以获取复选框的值。

我知道这个问题很老,但我刚刚做了这个,所以我想我还是会提供一个答案

单元格列表
只能保存单个单元格类型的垂直列表。如果希望在其中包含一个
复选框
,以及一些其他数据,则需要构建一个自定义单元格并扩展
复合单元格
,该单元格将组合并呈现单个单元格中的多个单元格。(位于
单元树
中最里面的单元)

因此,您的代码应该如下所示:

//首先列出HasCell类型-MyClass是CellList中显示的对象类型(可以是简单标签的字符串)
List hasCells=new ArrayList();
//然后将复选框单元格添加到其中
添加(新的HasCell()
{
私有CheckboxCell单元格=新的CheckboxCell(真、假);
公共单元getCell()
{
返回单元;
}
公共字段更新程序getFieldUpdater()
{
返回null;
}
@凌驾
公共布尔getValue(MyClass对象)
{
返回selectionModel.isSelected(对象);
}
});
//然后添加文本单元格
添加(新的HasCell()
{
私有TextCell cell=新TextCell(myString);
@凌驾
公共单元getCell()
{
返回单元;
}
公共字段更新程序getFieldUpdater()
{
返回null;
}
公共MyClass getValue(MyClass对象)
{
返回对象;
}
});
//现在使用列表(hasCells)构造实际的复合单元
单元格myClassCell=新的复合单元格(hasCells)
{
@凌驾
公共void呈现(上下文上下文、MyClass值、SafeHtmlBuilder sb)
{
某人以““””号结尾;
超级渲染(上下文、值、sb);
某人以““””号结尾;
}
@凌驾
受保护的元素getContainerElement(元素父级)
{
//返回表中的第一个TR元素。
返回parent.getFirstChildElement().getFirstChildElement().getFirstChildElement();
}
@凌驾
受保护的void呈现(上下文上下文、MyClass值、SafeHtmlBuilder sb、HasCell-HasCell)
{
//这将在新的表格单元格中呈现复合单元格内的每个单元格
Cell-Cell=hasCell.getCell();
某人以““””号结尾;
render(context,hasCell.getValue(value),sb);
某人以““””号结尾;
}
};
//然后制作实际的单元格列表,传递复合单元格
myList=新单元列表(myClassCell);
//添加selectionModel,确保将checkBoxManager作为第二个参数传入,否则selectionModel将无法工作
myList.setSelectionModel(selectionModel,DefaultSelectionEventManager.createCheckboxManager());
//构造用于动态编辑列表的数据提供程序(list.setRowData()的替代方案)
dataProvider=新列表dataProvider(数据);
//将数据提供程序与单元列表关联
dataProvider.addDataDisplay(myList);

这基本上也是我尝试过的,但我认为有一个现成的解决方案。但是,我不能同时向MyCheckBoxCell提供布尔值和字符串名,并将其传递给checkBoxCellList,因为checkBoxCellList只接受一个只有一种参数类型的列表。@Alexander Orlov您可以这样做。因此,在getValue()函数中,只需返回一个包含布尔值和字符串值的串联字符串。然后在渲染方法中,
Column<MyJSONObject, String> selectCheckBoxColumn =
                new Column<MyJSONObject, String>(new MyCheckBoxCell()) {

                   @Override
                   public String getValue(MyJSONObject object) {
                      return object.getInternalId() + SEPERATOR +  "true";
                   }
                };
             MyCellTable.addColumn(selectCheckBoxColumn, "Display title");

    .....
    .....
    .....

       private class MyCheckBoxCell extends AbstractCell<String> {

          @Override
          public void render(Context context, String value, SafeHtmlBuilder sb) {


             // ##############################################
             String[] values = value.split(SEPERATOR);        
             // NOW values[0] contains the string value
             // AND values[1] contains the boolean value
             // ##############################################

             sb.appendHtmlConstant("<input type='checkbox' name='" + htmlDOMId1 + "'" +
                                   " id='" + htmlDOMId1 + "'" +
                                   " value='" + value + "'" +
                                   "></input>");

          }
       }