动态创建 GWT CellTable

发布于 2024-12-01 06:34:37 字数 1586 浏览 0 评论 0 原文

我遇到了 GWT CellTable 的问题。我需要动态创建单元格表,但我没有实体(Bean)类来执行此操作。我已经看过所有 celltable 的示例,并且搜索了很多,以便在没有实体类的情况下做到这一点。

我需要根据数据库中存储的一些元数据动态填充表。我能够创建表结构

考虑有两个类,一个是 GRID,另一个是 COLUMN,用于元数据和列定义。 GRID 将具有列列表作为列定义

    Class GRID {
List<COLUMN> columns;
}

Class COLUMN {
  String columnName;
}

现在我需要从数据库获取网格并循环列以填充单元格(列),如下所示:

com.google.gwt.user.cellview.client.Column<String, String> textColumn = new Column<String, String>(
                        new EditTextCell()) {
                    @Override
                    public String getValue(String object) {
                        return object;
                    }
                };

现在,我需要向单元格表添加一些数据。但如果没有实体,就没有如何填充不同列的示例。我自己尝试过:(考虑网格有 3 列)

List<String> data;

String a1 = "A1";
            String a2 = "A2";
            String a3 = "A3";

            data = new ArrayList<String>();
            data.add(a1);
            data.add(a2);
            data.add(a3);

final AsyncDataProvider<String> provider = new AsyncDataProvider<String>() {
            @Override
            protected void onRangeChanged(HasData<String> display) {
                updateRowData(0, data);
            }
          };
          provider.addDataDisplay(grid);
provider.updateRowCount(data.size(), true);

我能够产生如下输出:

A1 A1 A1

A2 A2 A2

A3 A3 A3

实际上输出应该只是一行,

A1 A2 A3

但未能这样做。

请帮忙。

谢谢,

问候,

I am stuck in a problem with GWT CellTable. I need to create celltable dynamically and I dont have entity (Bean) class to do so. I have seen all the examples of celltable and searched a lot to do so without entity class.

I need to populate table dynamically according to some metadata stored in database. I am able create table structure

Consider there are two classes one is GRID and another is COLUMN for the metadata and columns definition. GRID will have list of COLUMNS as a columns definition

    Class GRID {
List<COLUMN> columns;
}

Class COLUMN {
  String columnName;
}

Now i need to get grid from database and loop the columns to populate the cells (Columns) like :

com.google.gwt.user.cellview.client.Column<String, String> textColumn = new Column<String, String>(
                        new EditTextCell()) {
                    @Override
                    public String getValue(String object) {
                        return object;
                    }
                };

Now, I need to add some data to the celltable. but without entity there are no example how to populate the different columns. I have tried my self like : (Consider Grid have 3 columns)

List<String> data;

String a1 = "A1";
            String a2 = "A2";
            String a3 = "A3";

            data = new ArrayList<String>();
            data.add(a1);
            data.add(a2);
            data.add(a3);

final AsyncDataProvider<String> provider = new AsyncDataProvider<String>() {
            @Override
            protected void onRangeChanged(HasData<String> display) {
                updateRowData(0, data);
            }
          };
          provider.addDataDisplay(grid);
provider.updateRowCount(data.size(), true);

I am able to produce the output like :

A1 A1 A1

A2 A2 A2

A3 A3 A3

Actually output should be one row only like

A1 A2 A3

But failed to do so.

Please help.

Thankx,

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一向肩并 2024-12-08 06:34:37

您可以将“行”表示为 List 实例,您必须将参数化从 String 更改为 List GridColumn 和数据提供者;当然,您必须使用 List> 调用 updateRowData,而不是 List

每列还需要一个 Column 实例,按索引从 List 中取出值:

class IndexedColumn extends Column<List<String>, String>
   private final int index;
   public IndexedColumn(int index) {
     super(new EditTextCell();
     this.index = index;
   }

   @Override
   public String getValue(List<String> object) {
      return object.get(this.index);
   }
}

You can represent your "rows" as List<String> instances, you have to change your parameterization from String to List<String> in your Grid, Column and data provider; and of course you have to call updateRowData with a List<List<String>>, not a List<String>.

You also need one Column instance per column, taking the value out of the List<String> by index:

class IndexedColumn extends Column<List<String>, String>
   private final int index;
   public IndexedColumn(int index) {
     super(new EditTextCell();
     this.index = index;
   }

   @Override
   public String getValue(List<String> object) {
      return object.get(this.index);
   }
}
白芷 2024-12-08 06:34:37

我有一个类似的问题需要解决。作为 GWT 的初学者,Thomas Broyer 的回答对于我解决问题来说有点抽象。也许这段代码也可以帮助我旁边的其他人:

public class DatGridExDynReady implements EntryPoint {

// quick showcase data representation, could be set up more dynamically during
// runtime when parsing server response data
private static final ArrayList<String> dataRow1 = new ArrayList<String>();
private static final ArrayList<String> dataRow2 = new ArrayList<String>();
private static final ArrayList<String> dataRow3 = new ArrayList<String>();
private static List<ArrayList<String>> data = new ArrayList<ArrayList<String>>();

public void onModuleLoad() {
        dataRow1.add("1"); dataRow1.add("a"); // quick showcase data
        dataRow2.add("2"); dataRow2.add("b");
        dataRow3.add("3"); dataRow3.add("c");
        DatGridExDynReady.data.add(dataRow1);
        DatGridExDynReady.data.add(dataRow2);
        DatGridExDynReady.data.add(dataRow3);

        DataGrid<List<String>> table = new DataGrid<List<String>>();            
        TextCell c1 = new TextCell(); // 1. text column creation showcase
        Column<List<String>, String> myC1 = new Column<List<String>, String>(c1) {
            public String getValue(List<String> object) {
                return object.get(0);
            }
        };
        table.addColumn(myC1, "col1");          
        TextCell c2 = new TextCell(); // 2. text column creation showcase
        Column<List<String>, String> myC2 = new Column<List<String>, String>(c2) {
            public String getValue(List<String> object) {
                return object.get(1);
            }
        };
        table.addColumn(myC2, "col2");
        table.setRowCount(data.size(), true);
        table.setRowData(0, data);
        DockLayoutPanel rPanel = new DockLayoutPanel(Unit.PX);
        rPanel.setSize("100%", "200px");
        rPanel.addWest(table, 200);
        RootPanel.get().add(rPanel);
}

}

I had a similar problem to solve. As a beginner in GWT the answer of Thomas Broyer has been a little bit to abstract for me to get my problem solved. Maybe this snippet of code helps someone else beside me as well:

public class DatGridExDynReady implements EntryPoint {

// quick showcase data representation, could be set up more dynamically during
// runtime when parsing server response data
private static final ArrayList<String> dataRow1 = new ArrayList<String>();
private static final ArrayList<String> dataRow2 = new ArrayList<String>();
private static final ArrayList<String> dataRow3 = new ArrayList<String>();
private static List<ArrayList<String>> data = new ArrayList<ArrayList<String>>();

public void onModuleLoad() {
        dataRow1.add("1"); dataRow1.add("a"); // quick showcase data
        dataRow2.add("2"); dataRow2.add("b");
        dataRow3.add("3"); dataRow3.add("c");
        DatGridExDynReady.data.add(dataRow1);
        DatGridExDynReady.data.add(dataRow2);
        DatGridExDynReady.data.add(dataRow3);

        DataGrid<List<String>> table = new DataGrid<List<String>>();            
        TextCell c1 = new TextCell(); // 1. text column creation showcase
        Column<List<String>, String> myC1 = new Column<List<String>, String>(c1) {
            public String getValue(List<String> object) {
                return object.get(0);
            }
        };
        table.addColumn(myC1, "col1");          
        TextCell c2 = new TextCell(); // 2. text column creation showcase
        Column<List<String>, String> myC2 = new Column<List<String>, String>(c2) {
            public String getValue(List<String> object) {
                return object.get(1);
            }
        };
        table.addColumn(myC2, "col2");
        table.setRowCount(data.size(), true);
        table.setRowData(0, data);
        DockLayoutPanel rPanel = new DockLayoutPanel(Unit.PX);
        rPanel.setSize("100%", "200px");
        rPanel.addWest(table, 200);
        RootPanel.get().add(rPanel);
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文