具有地图的 FeatureTable 微件

尝试一下在线预览

此示例演示如何将 FeatureTable 微件添加到您的应用程序。 FeatureTable 微件允许用户查看和排序来自 FeatureLayer 的数据和属性。 在此示例中,除了关联的地图外,还会显示该表。 通过单击右上角的缩放至选定要素 按钮,可以将表格中的任何选定要素缩放到其范围。

可以在表格中选择一个要素,并使其关联的要素自动反映为地图中的突出显示。 为了使其正常工作,必须设置表的 view 属性。 目前,API 中没有直接的方法来连接在地图中选择一个要素并在表格中选择其关联的行。 此示例演示如何在地图中选择一个要素并将该选择反映在表中。

此外,此示例还演示了如何过滤并仅显示当前在地图中显示的关联要素。 它通过侦听视图的 范围 何时更新来做到这一点。 因此,每当视图的范围发生变化时,FeatureTable 的 filterGeometry 都会设置为这个新范围,并且只有落在该区域内的那些要素才会显示。

它是如何工作的呢?

首先,我们添加具有指定 FeatureTable 和要显示的 字段FeatureLayer。 此外,还必须设置 视图 ,以便表中的任何选定行显示其关联的要素,如地图中突出显示的那样。

                                       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
view.when(() => {
  //grabs the first layer in the map
  const featureLayer = webmap.layers.getItemAt(0);
  featureLayer.title = "USFS Recreational areas";

  // Create the feature table
  const featureTable = new FeatureTable({
    layer: featureLayer,
    view: view, // required for feature highlight to work
    // Autocast the FieldColumnConfigs
    fieldConfigs: [{
      name: "RECAREANAM",
      label: "Recreation area name",
      direction: "asc"
    },
    {
      name: "FORESTNAME",
      label: "Forest name"
    },
    {
      name: "OPENSTATUS",
      label: "Open status"
    },
    {
      name: "OPEN_SEASO",
      label: "Season begins"
    },
    {
      name: "RECAREADES",
      label: "Recreation description"
    },
    {
      name: "RESTRICTIO",
      label: "Restrictions"
    }
  ],
  container: document.getElementById("tableDiv")
});
...

接下来,监听表的  selection-change  事件。 如果该行(要素)被选中,或者添加到所选要素中,则将其添加到  features[]  数组中。 如果未选中,请将其删除。

                   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Listen for the table's selection-change event
featureTable.on("selection-change", (changes) => {
  // If the selection is removed, remove the feature from the array
  changes.removed.forEach((item) => {
    const data = features.find((data) => {
      return data.feature === item.feature;
    });
    if (data) {
      features.splice(features.indexOf(data), 1);
    }
  });
  // If the selection is added, push all added selections to array
  changes.added.forEach((item) => {
    const feature = item.feature;
    features.push({
      feature: feature
    });
  });
});

接下来,监听视图的 范围 何时更新。一旦发生这种情况,将更新的范围传递给表的 filterGeometry

         
1
2
3
4
5
6
7
8
9
featureLayer.watch("loaded", () => {
  watchUtils.whenFalse(view, "updating", () =>  {
    // Get the new extent of view/map whenever map is updated.
    if (view.extent) {
      // Filter and show only the visible features in the feature table
      featureTable.filterGeometry = view.extent;
    }
  });
});

此应用程序还侦听视图的 immediate-click 事件。 它对点位置执行  hitTest ,如果适用,则选择表中相应要素的行。

          
1
2
3
4
5
6
7
8
9
10
// Listen for the click on the view and select any associated row in the table
view.on("immediate-click", (event) => {
  view.hitTest(event).then((response) => {
    const candidate = response.results.find((result) => {
      return result.graphic && result.graphic.layer && result.graphic.layer === featureLayer;
    });
    // Select the rows of the clicked feature
    candidate && featureTable.selectRows(candidate.graphic);
  });
});

最后,缩放到所选表格要素的范围。 如果选择了一个要素,它将缩放到该点位置。 如果选择了多行/要素,它会缩放到所有点位置的组合范围。

                      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// fires when "Zoom to selected feature(s)" button is clicked
function zoomToSelectedFeature() {
  // Create a query off of the feature layer
  const query = featureLayer.createQuery();
  // Iterate through the features and grab the feature's objectID
  const featureIds = features.map((result) => {
    return result.feature.getAttribute(featureLayer.objectIdField);
  });
  // Set the query's objectId
  query.objectIds = featureIds;
  // Make sure to return the geometry to zoom to
  query.returnGeometry = true;
  // Call queryFeatures on the feature layer and zoom to the resulting features
  featureLayer.queryFeatures(query).then((results) => {
    view.goTo(results.features).catch((error) => {
      if (error.name != "AbortError"){
        console.error(error);
      }
    });
  });
}
...

除了将表与现有地图一起使用外,还可以将 FeatureTable 微件用作独立表。 有关如何执行此操作的示例,请参阅  FeatureTable 微件  示例。

已知限制

有关限制的完整列表,请参阅微件的 参考手册  文档。

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.