已启用编辑的 FeatureTable 微件

尝试一下在线预览

此示例演示如何使用  FeatureTable 微件进行编辑。 在此示例中,启用了可编辑数据以在微件内进行编辑。 为了启用此功能,必须将表的 editingEnabled 属性设置为  true。 设置后,表格中的单元格值可以通过双击进行更新。

请注意,必须启用托管数据的服务才能进行编辑。 在此特定示例中,授予对数据的完全编辑权限。 还可以在字段级别进一步限制编辑功能。 这是通过在表的字段配置中将表的  fieldConfig.editable 属性设置为 true  来完成的。 在此示例中,不允许编辑 Case No.  字段。

除了显示表格编辑之外,此示例还演示了如何自定义要素表的菜单。 创建了自定义 缩放到要素 按钮菜单 ,以缩放到表中任何选定行的相应要素的范围。

                                                   
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
40
41
42
43
44
45
46
47
48
49
50
51
view.when(() => {
  // Create the feature layer
  featureLayer = new FeatureLayer({
    portalItem: {
      id: "3807c58dd48c4d32810042d8edf4a2fe"
    },
    outFields: ["*"],
    title: "Chicago crime incidents"
  });
  map.add(featureLayer);

// Create the feature table
const featureTable = new FeatureTable({
  view: view, // This must be set to enable highlight in the map
  layer: featureLayer,
  editingEnabled: true, // This must be set to enable editing in the table
  menuConfig: { // Creates a custom menu to zoom to selected features
    items: [{
      label: "Zoom to feature(s)",
      iconClass: "esri-icon-zoom-in-magnifying-glass",
      clickFunction: (event) => {
        zoomToSelectedFeature();
      }
    }]
  },
  // Autocast the FieldColumnConfigs
  fieldConfigs: [{
    name: "Case_Number",
    label: "Case No.",
    editable: false, // This disables editing for this specific field
    direction: "asc"
  },
  {
    name: "Primary_Type",
    label: "Crime type"
  },
  {
    name: "Location_Description",
    label: "Location description"
  },
  {
    name: "Arrest",
    label: "Arrest"
  },
  {
    name: "incident_date",
    label: "Date of incident"
  }],
  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
20
21
// Get the FeatureLayer's layerView and 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
    });
  });
});
...

最后,缩放到所选表格行/要素的范围。 如果选择了一个要素,它将缩放到该点位置。 如果选择了多行/要素,它会缩放到所有点位置的组合范围。 这是在自定义 ButtonMenu  中处理的。

                      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// This function is called above in the Feature Table's menuConfig property for `clickFunction`
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);
      }
    });
  });
}
...

已知限制

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

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