【问题标题】:DataGrid inside InfoWindow doesn't show table dataInfoWindow 中的 DataGrid 不显示表格数据
【发布时间】:2023-04-07 17:10:01
【问题描述】:

我正在使用 GWT 和 GoogleMaps GWT API(v3.8,不,我不能使用 branflakes GoogleMaps API)。

我正在尝试将 DataGrid 表放在地图上的 InfoWindow 中。只要将 DataGrid 显示在地图外,我就可以正常工作,但是每当我在 InfoWindow 中添加 DataGrid 时,都不会显示任何表格行:

如果我右键单击信息窗口,转到“检查元素”,然后在 GWT 创建的数百万个 div 中跋涉,我可以看到数据就在那里——只是由于某种原因它不可见。

我已经做了很多谷歌搜索,但我所看到的只是 DataGrid 必须是 LayoutPanel 或 ScrollPanel 的子级。但这正是我正在做的事情:

package com.test.client;

import java.util.Arrays;

import com.google.gwt.ajaxloader.client.AjaxLoader;
import com.google.gwt.ajaxloader.client.AjaxLoader.AjaxLoaderOptions;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Document;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.maps.gwt.client.GoogleMap;
import com.google.maps.gwt.client.InfoWindow;
import com.google.maps.gwt.client.InfoWindowOptions;
import com.google.maps.gwt.client.LatLng;
import com.google.maps.gwt.client.MapOptions;
import com.google.maps.gwt.client.MapTypeId;

public class GwtTest implements EntryPoint {

    @Override
    public void onModuleLoad() {
        AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
        options.setOtherParms("sensor=false");
        Runnable callback = new Runnable() {
            public void run() {
                createMap();
            }
        };
        AjaxLoader.loadApi("maps", "3", callback, options);

        //this works
        DataGrid dg = createTable();
        RootPanel.get("tableDiv").add(new ScrollPanel(dg));
    }

    public void createMap() {

        MapOptions mapOpts = MapOptions.create();
        mapOpts.setZoom(4);
        mapOpts.setCenter(LatLng.create(37.09024, -95.712891));
        mapOpts.setMapTypeId(MapTypeId.TERRAIN);
        mapOpts.setStreetViewControl(false);

        GoogleMap map = GoogleMap.create(Document.get().getElementById("map_canvas"), mapOpts);

        DataGrid dg = createTable();

        InfoWindowOptions iwo = InfoWindowOptions.create();
        iwo.setPosition(LatLng.create(37.09024, -95.712891));
        iwo.setContent(new ScrollPanel(dg).getElement());

        InfoWindow infoWindow = InfoWindow.create(iwo);
        infoWindow.open(map);
    }

    public DataGrid createTable(){
        Column columnA = new Column(new TextCell()){
            public Object getValue(Object object) {
                return "one";
            }
        };

        Column columnB = new Column(new TextCell()){
            public Object getValue(Object object) {
                return "two";
            }
        };

        Column columnC = new Column(new TextCell()){
            public Object getValue(Object object) {
                return "three";
            }
        };

        DataGrid dataGrid = new DataGrid();
        dataGrid.addColumn(columnA, "A");
        dataGrid.addColumn(columnB, "B");
        dataGrid.addColumn(columnC, "C");

        dataGrid.setRowCount(1);
        dataGrid.setRowData(Arrays.asList("one", "two", "three"));

        dataGrid.setWidth("200px");
        dataGrid.setHeight("100px");

        return dataGrid;
    }
}

我是否缺少某些步骤?如何使用 GoogleMaps API 让 DataGrid 正确显示在 InfoWindow 中?

编辑:在没有收到任何答案后,我已将此问题交叉发布到 GWT 讨论板 here

【问题讨论】:

  • 以不同方式格式化您的信息窗口会改变什么吗?
  • @Andy 感谢您的回复。以不同方式格式化我的 InfoWindow 到底是什么意思?
  • 对不起,我的意思是把内容设置成别的东西,也许用 html 定义。虽然我猜如果你需要它作为一个可滚动的,它可能是没有意义的。
  • @Andy 我不需要它作为可滚动的。但我尝试将表格添加到其他组件,并将这些组件添加到 InfoWindow,但一切似乎都具有相同的效果。
  • @TonyKennah 我非常肯定。

标签: java google-maps gwt datagrid infowindow


【解决方案1】:

如果其他人遇到这个问题,我最终只使用了 CellTable:

package com.test.client;

import java.util.Arrays;

import com.google.gwt.ajaxloader.client.AjaxLoader;
import com.google.gwt.ajaxloader.client.AjaxLoader.AjaxLoaderOptions;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Document;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.maps.gwt.client.GoogleMap;
import com.google.maps.gwt.client.InfoWindow;
import com.google.maps.gwt.client.InfoWindowOptions;
import com.google.maps.gwt.client.LatLng;
import com.google.maps.gwt.client.MapOptions;
import com.google.maps.gwt.client.MapTypeId;

public class GwtTest implements EntryPoint {

    @Override
    public void onModuleLoad() {
        AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
        options.setOtherParms("sensor=false");
        Runnable callback = new Runnable() {
            public void run() {
                createMap();
            }
        };
        AjaxLoader.loadApi("maps", "3", callback, options);

        //this works
        CellTable dg = createTable();
        RootPanel.get("tableDiv").add(new ScrollPanel(dg));
    }

    public void createMap() {

        MapOptions mapOpts = MapOptions.create();
        mapOpts.setZoom(4);
        mapOpts.setCenter(LatLng.create(37.09024, -95.712891));
        mapOpts.setMapTypeId(MapTypeId.TERRAIN);
        mapOpts.setStreetViewControl(false);

        GoogleMap map = GoogleMap.create(Document.get().getElementById("map_canvas"), mapOpts);

        CellTable dg = createTable();


        InfoWindowOptions iwo = InfoWindowOptions.create();
        iwo.setPosition(LatLng.create(37.09024, -95.712891));
        iwo.setContent(dg.getElement());

        InfoWindow infoWindow = InfoWindow.create(iwo);
        infoWindow.open(map);

    }

    public CellTable createTable(){
        Column columnA = new Column(new TextCell()){

            @Override
            public Object getValue(Object object) {
                return "one";
            }

        };

        Column columnB = new Column(new TextCell()){

            @Override
            public Object getValue(Object object) {
                return "two";
            }

        };

        Column columnC = new Column(new TextCell()){

            @Override
            public Object getValue(Object object) {
                return "three";
            }

        };

        CellTable dataGrid = new CellTable();
        dataGrid.addColumn(columnA, "A");
        dataGrid.addColumn(columnB, "B");
        dataGrid.addColumn(columnC, "C");

        dataGrid.setRowCount(1);
        dataGrid.setRowData(Arrays.asList("one", "two", "three"));

        dataGrid.setWidth("200px");
        dataGrid.setHeight("100px");

        return dataGrid;
    }
}

【讨论】:

    【解决方案2】:

    您需要在 onModuleLoad 中使用 RootLayoutPanel 而不是 RootPanel。从根到 DataGrid 都需要有布局面板。 InfoWindow 似乎不是一个布局面板,因此您可能需要自己调整大小,请参阅:

    http://www.gwtproject.org/doc/latest/DevGuideUiPanels.html#Resize

    此外,您将 DataGrid 放在滚动面板中。你可以这样做,但你必须给它一个明确的大小。 DataGrid 有自己的滚动面板,因此您可能还可以摆脱显式创建的滚动面板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 2017-12-24
      • 2012-08-09
      相关资源
      最近更新 更多