对于这种类型的标准,您必须在后端实现自己的逻辑。我不知道您的后端使用的是什么,但在我的情况下是 SQL ......所以我所做的是我创建了一个名为“q”的自定义查询参数,然后使用这样的自定义查询文本:
const queryParams = "id_contains_A0001_OR_id_contians_J003_OR_id_contains_K008";
const query = `q=${encodeURIComponent(queryParams)}`;
this.menuItemsService.getWithQuery({
q: query
});
然后在后端,我只需解析“q”参数的任何内容。例如,在我的情况下,我再次使用 SQL,因此以下内容就足够了。
if(!!q){
const queries = q.split(/_and_/).map(i=>i.toLowerCase());
queries.forEach(stringQuery=>{
const preliminarClauses: string[] = [];
const containsOR = stringQuery.match(/_or_/);
const allQueries: string[] = [];
if(!!containsOR){
allQueries.push(...stringQuery.split(/_or_/));
} else {
allQueries.push(stringQuery);
}
for(let i=0; i<allQueries.length; ++i){
const query = allQueries[i];
const operatorMatch = query.match(/!=|<>|<=|>=|_contains_|_in_|[<>=]/i);
if(!!operatorMatch){
let operator = operatorMatch[0];
const parts = query.split(operator);
const colName = "`" + parts[0] + "`";
let value = parts[1];
if(!!value){
if(value === "null"){
if(operator === "="){
preliminarClauses.push(`(${colName} IS NULL OR $${colName} = '')`);
} else if(["<>", "!="].includes(operator)){
preliminarClauses.push(`(${colName} IS NOT NULL OR $${colName} != '')`);
}
} else if(operator === "_contains_"){
preliminarClauses.push(`${colName} LIKE '%${value}%'`);
} else if(operator === "_in_"){
preliminarClauses.push(`${colName} IN (${value})}`);
} else {
const escVal = escapeStr(value);
preliminarClauses.push(`${colName} ${operator} ${escVal}`);
}
}
}
}
if(containsOR){
whereClauses.push(`(${preliminarClauses.join(" OR ")})`)
} else {
whereClauses.push(...preliminarClauses);
}
});
}
以上是代码摘录,因此您可以理解。