简体   繁体   English

动态创建GWT CellTable

[英]Create GWT CellTable Dynamically

I am stuck in a problem with GWT CellTable. 我遇到了GWT CellTable的问题。 I need to create celltable dynamically and I dont have entity (Bean) class to do so. 我需要动态创建celltable,我没有实体(Bean)类这样做。 I have seen all the examples of celltable and searched a lot to do so without entity class. 我已经看过了celltable的所有例子,并且在没有实体类的情况下进行了很多搜索。

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,另一个是COLUMN,用于元数据和列定义。 GRID will have list of COLUMNS as a columns definition GRID将COLUMNS列表作为列定义

    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. 现在,我需要向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) 我试过自己的样子:( 考虑网格有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);

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, Thankx,

Regards, 问候,

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; 您可以将“行”表示为List<String>实例,您必须在GridColumn和数据提供程序中将参数化从String更改为List<String> ; and of course you have to call updateRowData with a List<List<String>> , not a List<String> . 当然,您必须使用List<List<String>>调用updateRowData ,而不是List<String>

You also need one Column instance per column, taking the value out of the List<String> by index: 每列还需要一个Column实例,通过索引从List<String>取出值:

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);
   }
}

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. 作为GWT的初学者,Thomas Broyer的答案对我来说有点抽象,以解决我的问题。 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);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM