arcgis for js实现FeatureLayer图层弹窗展示所有field字段方式二

效果和说明

feature图层中的矢量要素包含很多的属性,在点击要素时popupTemplate弹窗显示所有field字段值

上一篇文章提到对field值进行转义,在研究时发现新的弹窗展示所有字段的另一种方式,更加便捷,且样式可自定义

实现代码

html 复制代码
<!DOCTYPE html>
<html lang="zn">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <style>
      body {
        margin: 0;
      }
      #mapview {
        position: absolute;
        width: 100%;
        height: 100%;
      }
      * {
        margin: 0;
        padding: 0;
      }

      .popup-template-content {
        border: 1px solid #ccc;
        border-bottom: 0;
      }
      .popup-template-content .field-row {
        margin: 0;
        display: flex;
        border-bottom: 1px solid #ccc;
        border-left: 3px solid #ff6600;
      }
      .popup-template-content .field-row .dt {
        padding: 5px 10px;
        flex: 1;
        border-right: 1px solid #ccc;
      }
      .popup-template-content .field-row .db {
        padding: 5px 10px;
        flex: 2;
        text-align: left;
      }
    </style>

    <link rel="stylesheet" href="https://js.arcgis.com/4.23/esri/themes/light/main.css" />
    <script src="https://js.arcgis.com/4.23/"></script>
  </head>
  <body>
    <div id="mapview"></div>

    <script>
      require(['esri/Map', 'esri/views/MapView', 'esri/layers/FeatureLayer'], function (
        Map,
        MapView,
        FeatureLayer
      ) {
        // 初始化底图
        window.map = new Map({
          basemap: 'dark-gray-vector'
        })

        // 创建2维视图
        let view = new MapView({
          container: 'mapview',
          map: map,
          zoom: 11,
          center: [104.783916597735, 32.55699155692144] // 设置地图的初始化中心点坐标
        })

        let layer = new FeatureLayer({
          url: 'http://xxxxxxxxxxx/MapServer', // 这里替换成自己的图层
          spatialReference: view.spatialReference,
          outFields: ['*'],
          hasM: true,
          hasZ: true,
          popupTemplate: {
            title: '图层弹窗Title',
            content: function (feature) {
              console.log(feature)

              // 获取字段
              var attributes = feature.graphic.attributes
              let html = '<div class="popup-template-content">'
              for (const key in attributes) {
                // 排除不要的属性
                if (['FID', 'OID_'].includes(key)) {
                  continue
                }
                html += `<p class="field-row">
                  <span class="dt">${key}</span>
                  <span class="db">${attributes[key]}</span>
                </p>`
              }
              html += '</div>'

              return html
            }
          }
        })
        console.log(layer)

        map.add(layer)
      })
    </script>
  </body>
</html>

对字段名和字段值进行转义就简单了,但是文章标题有限,为了方便查找我后续就再水一篇, 嘿嘿!!!

相关推荐
云水一下1 分钟前
Vue.js从零到精通系列(七):高级特性实战——Teleport、异步组件、自定义指令与TypeScript深度结合
前端·vue.js·typescript
qq4356947013 分钟前
Vue05
前端·vue.js
qq_422152575 分钟前
PDF 解密工具怎么选?2026 年文档密码移除方案与注意事项
java·前端·pdf
YHHLAI9 分钟前
前端工程化调用 AI 多模态生图模型:Qwen Image Demo 实战
前端·人工智能
触底反弹22 分钟前
一文彻底搞懂 JavaScript 栈和队列(建议收藏)
javascript·算法·面试
To_OC22 分钟前
我一直以为 Ajax 是个黑盒,直到我写了这 50 行代码
前端·后端·全栈
用户0595401744627 分钟前
RAG 记忆层踩坑实录:用户偏好凭空消失,我排查了 4 小时,最后用 LangChain + Chroma 搭了套自动化回归测试
前端·css
Asize31 分钟前
Prompt 驱动 NLP:从 ES6 模块化到文本推理实战
javascript·人工智能·机器学习
程序猿阿伟33 分钟前
《Chrome隔离机制的维度落地指南》
前端·chrome
用户0543243297035 分钟前
AI 生成的代码怎么在前端安全预览 + 一键运行:sandbox iframe 实战 🔒
前端