Skip to content

Commit 0a1dbe7

Browse files
author
ChenGuanglin
committed
【update】过滤条件增加 Name in ('', '') 格式的查询
review by zhaoq
1 parent f39a404 commit 0a1dbe7

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ export class WebMap extends Observable {
22462246
* @param {array} allFeatures - 图层上的feature集合
22472247
*/
22482248
getFiterFeatures(filterCondition, allFeatures) {
2249-
let condition = this.replaceFilterCharacter(filterCondition);
2249+
let condition = this.parseFilterCondition(filterCondition);
22502250
let sql = "select * from json where (" + condition + ")";
22512251
let filterFeatures = [];
22522252
for (let i = 0; i < allFeatures.length; i++) {
@@ -2270,14 +2270,29 @@ export class WebMap extends Observable {
22702270

22712271
/**
22722272
* @private
2273-
* @function ol.supermap.WebMap.prototype.replaceFilterCharacter
2274-
* @description 替换查询语句 中的 and / AND / or / OR / = / !=
2275-
* @param {string} filterString - 过滤条件
2273+
* @function ol.supermap.WebMap.prototype.parseFilterCondition
2274+
* @description 1、替换查询语句 中的 and / AND / or / OR / = / !=
2275+
* 2、匹配 Name in ('', ''),多条件需用()包裹
2276+
* @param {string} filterCondition - 过滤条件
22762277
* @return {string} 换成组件能识别的字符串
22772278
*/
2278-
replaceFilterCharacter(filterString) {
2279-
filterString = filterString.replace(/=/g, '==').replace(/AND|and/g, '&&').replace(/or|OR/g, '||').replace(/<==/g, '<=').replace(/>==/g, '>=');
2280-
return filterString;
2279+
parseFilterCondition(filterCondition) {
2280+
return filterCondition
2281+
.replace(/=/g, "==")
2282+
.replace(/AND|and/g, "&&")
2283+
.replace(/or|OR/g, "||")
2284+
.replace(/<==/g, "<=")
2285+
.replace(/>==/g, ">=")
2286+
.replace(/\(?[^\(]+?\s+in\s+\([^\)]+?\)\)?/g, (res) => {
2287+
// res格式:(省份 in ('四川', '河南'))
2288+
const data = res.match(/([^(]+?)\s+in\s+\(([^)]+?)\)/);
2289+
return data.length === 3
2290+
? `(${data[2]
2291+
.split(",")
2292+
.map((c) => `${data[1]} == ${c.trim()}`)
2293+
.join(" || ")})`
2294+
: res;
2295+
});
22812296
}
22822297

22832298
/**
@@ -2892,7 +2907,7 @@ export class WebMap extends Observable {
28922907
});
28932908
if(layerInfo.filterCondition) {
28942909
//过滤条件
2895-
let condition = that.replaceFilterCharacter(layerInfo.filterCondition);
2910+
let condition = that.parseFilterCondition(layerInfo.filterCondition);
28962911
let sql = "select * from json where (" + condition + ")";
28972912
let filterResult = window.jsonsql.query(sql, {
28982913
attributes: feature.get('attributes')
@@ -3042,7 +3057,7 @@ export class WebMap extends Observable {
30423057
return function (feature) {
30433058
if(layerInfo.filterCondition) {
30443059
//过滤条件
3045-
let condition = that.replaceFilterCharacter(layerInfo.filterCondition);
3060+
let condition = that.parseFilterCondition(layerInfo.filterCondition);
30463061
let sql = "select * from json where (" + condition + ")";
30473062
let filterResult = window.jsonsql.query(sql, {
30483063
attributes: feature.get('attributes')

0 commit comments

Comments
 (0)