如何过滤通过iframe嵌入的Contour仪表盘?实现与Workshop过滤联动
如何过滤iframe嵌入的Contour仪表盘?
核心逻辑:通过URL查询参数传递过滤条件
Contour支持直接在仪表盘URL后追加filter_<字段名>=<筛选值>格式的查询参数来实现过滤,多条件用&拼接即可。下面是具体操作步骤:
1. 获取正确的过滤参数格式
先在Contour的web界面手动设置好你需要的筛选条件,设置完成后复制浏览器地址栏的完整URL。此时URL里会自动生成对应的filter_*参数,直接复用这些参数能避免字段名拼写、格式错误的问题。
比如你筛选了地区为us-west、产品为widgets,地址栏会类似:https://your-contour-instance.com/dashboards/sales-dashboard?filter_region=us-west&filter_product=widgets
2. 嵌入带过滤参数的Contour仪表盘到Workshop
把上述带过滤参数的URL作为iframe的src属性值,嵌入到你的Workshop页面中:
<iframe id="contour-dashboard" src="https://your-contour-instance.com/dashboards/sales-dashboard?filter_region=us-west&filter_product=widgets" width="100%" height="600" frameborder="0"></iframe>
3. 实现Workshop筛选与Contour仪表盘的同步
如果需要在Workshop操作筛选控件时自动更新Contour的过滤状态,用JavaScript监听筛选控件的事件,动态修改iframe的src属性即可:
举个例子,假设Workshop里有一个地区选择下拉框(ID为region-select):
// 监听下拉框选择事件 document.getElementById('region-select').addEventListener('change', function(e) { const selectedRegion = e.target.value; const contourIframe = document.getElementById('contour-dashboard'); // 仪表盘基础URL(不带过滤参数) const baseDashboardUrl = 'https://your-contour-instance.com/dashboards/sales-dashboard'; // 构建带过滤参数的新URL,用encodeURIComponent处理特殊字符 const filteredUrl = `${baseDashboardUrl}?filter_region=${encodeURIComponent(selectedRegion)}`; // 更新iframe的src,触发仪表盘刷新过滤 contourIframe.src = filteredUrl; });
如果有多个筛选条件(比如同时选地区和产品),就拼接多个&filter_<字段名>=<值>参数:
const filteredUrl = `${baseDashboardUrl}?filter_region=${encodeURIComponent(selectedRegion)}&filter_product=${encodeURIComponent(selectedProduct)}`;
注意事项
- 确保你的Contour实例允许跨域嵌入(检查Contour的配置或服务器CORS设置,避免iframe加载失败)
- 字段名必须和Contour仪表盘里的字段完全一致,大小写敏感,别写错
- 部分Contour版本支持多值过滤,用逗号分隔即可,比如
filter_region=us-west,eu-central
内容的提问来源于stack exchange,提问作者Vaibhav Najan
相关产品推荐
相关产品推荐

