【问题标题】:Return all the attributes(fields) in a feature layer for a PopupTemplate - ArcGis Javascript返回 PopupTemplate 要素图层中的所有属性(字段) - ArcGis Javascript
【发布时间】:2022-01-11 11:19:04
【问题描述】:

有没有办法为 PopupTemplate 返回要素层中的所有属性(字段),而无需在 Angular 的 fieldInfos 对象中声明所有属性(字段)?

.ts

const template = {
        title: "{NAME} in {COUNTY}",
        content: ,
        fieldInfos : fieldInfos
      };
      
              
     const layer = new FeatureLayer({
      url: this.featureLayerUrl,
      visible: true,
      popupTemplate : template
      });
      
      var fieldInfos = layer.map(layer.fields, function(field){
         return {
                "fieldName": field.name,
                "label": field.alias,
                "visible": true

    webmap.add(layer);
    

.html

    <!-- Map Div -->

我正在使用 arcgis-js-api 版本 4.2.1。
但是当我使用这个例子时,它正在工作。 (但我想动态设置这些字段。)

const fields = [{
  name: "NAME",
  alias: "Name",
  type: "string"
    }, {
  name: "County",
  alias: "County",
  type: "string"
}, {
  
const config = {
  fields: fields,
  title: "County land"
}; 

【问题讨论】:

    标签: javascript angular arcgis arcgis-js-api arcgis-server


    【解决方案1】:

    如果你只是想创建默认的弹出模板(包含所有字段的表格),那么你只需要使用FeatureLayer函数createPopupTemplate,它会为你做所有事情(ArcGIS JS API - FeatureLayer createPopupTemplate)。

    现在如果你想做一些额外的配置,你可以使用相同的方法或popupUtils 方法,它有一个额外的参数。

    这是我为您制作的一个示例,以展示可能的用途,我所做的只是更改 alias 的值以使其看起来更好。

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset='utf-8' />
        <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
        <title>FeatureLayer field and popup example - 4.21</title>
    
        <link rel='stylesheet' href='https://js.arcgis.com/4.21/esri/themes/light/main.css' />
        <script src='https://js.arcgis.com/4.21/'></script>
    
        <style>
            html,
            body,
            #viewDiv {
                padding: 0;
                margin: 0;
                height: 100%;
                width: 100%;
            }
        </style>
        <script>
            require([
                'esri/views/MapView',
                'esri/Map',
                'esri/layers/FeatureLayer',
                'esri/support/popupUtils'
            ], function (MapView, Map, FeatureLayer, popupUtils) {
                const layer1 = new FeatureLayer({
                    url: 'https://services9.arcgis.com/RHVPKKiFTONKtxq3/arcgis/rest/services/USGS_Seismic_Data_v1/FeatureServer/1'
                });
                const layer2 = new FeatureLayer({
                    portalItem: {
                        id: "f9e348953b3848ec8b69964d5bceae02"
                    }
                });
                const layersToCreateMyPopupTemplate = [layer2, layer1];
                const map = new Map({
                    basemap: 'gray-vector',
                    layers: layersToCreateMyPopupTemplate
                });
    
                const view = new MapView({
                    map: map,
                    container: 'viewDiv',
                    center: [-98, 40],
                    zoom: 4
                });
                const toNiceName = function(text) {
                    if (!text) {
                        return null;
                    }
                    return text
                        .toLowerCase()
                        .split(/_|__|\s/)
                        .join(' ');
                };
                const createMyPopupTemplate = function(layer) {
                    console.log(layer.fields); // layer fields
                    const config = {
                        fields: layer.fields.map(field => (
                            {
                                name: field.name,
                                type: field.type,
                                alias: toNiceName(field.alias)
                            }
                        )),
                        title: toNiceName(layer.title)
                    };
                    console.log(config); // config parameter
                    return popupUtils.createPopupTemplate(config);
                }
                for (const layer of layersToCreateMyPopupTemplate) {
                    view.whenLayerView(layer).then(function (layerView) {
                        const popupTemplate = createMyPopupTemplate(layer)
                        console.log(popupTemplate); // popup template
                        if (!popupTemplate) {
                            console.log("FeatureLayer has no fields.")
                        } else {
                            layer.popupTemplate = popupTemplate;
                        }
                    });
                }
            });
        </script>
    </head>
    
    <body>
        <div id='viewDiv'></div>
    </body>
    
    </html>

    示例中config 变量的fields 属性自动转换为esri/layers/support/Field 的实例,这与PopupTemplate 类型为FieldInfofieldInfos 属性不同。

    【讨论】:

    • 这太棒了!!但是,我遇到了这种错误。 config 不可分配给“配置”类型的参数。属性“字段”的类型不兼容。另外我还有 2 个图层,我也想获取该图层的所有字段,这可能吗?
    • 我更新了问题。请看一看。
    • 我已经更新了sn-p并添加了额外的与..相关的信息。如果你想使用它直接放入弹出模板的fieldInfos,你需要适应它,至少1 ) name for fieldName, 2) 删除 type, 3) alias for label
    猜你喜欢
    • 2019-01-09
    • 1970-01-01
    • 2020-08-08
    • 2016-11-23
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2019-03-29
    • 2019-11-28
    相关资源
    最近更新 更多