简体   繁体   中英

Create GWT CellTable Dynamically

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,

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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