0

有没有办法根据用户输入动态生成 where 条件。我有一个带有选项 '>'、'<'、'equals'、'starts with'、'ends with' 的选择框。基于此条件 where 子句应该生成并且应该执行查询。请帮助我。我需要示例。因为我的表中有大约 80 列我不能使用 if else 循环。

function querymap()


{
var querypass=document.getElementById('query-pass').value.replace(/'/g, "\\'");
 if(querypass=='hhSanitHouseType')
   {
     var operator=document.getElementById('operatorstring').value.replace(/'/g, "\\'");
     if(operator=='>')
        { 
          var textvalue=document.getElementById("text-value").value.replace(/'/g, "\\'");
          layer.setQuery("SELECT 'geometry',hhSanitHouseType FROM " + tableid + " WHERE 'hhSanitHouseType' > '" + textvalue + "'");


        }
     }
   else
   {
   alert("false");
   }
}
4

1 回答 1

0

也许你可以看看这个例子,尤其是函数generateWhere(columnName, low, high)

您不必为您的运算符使用 if/else,只需检查有效输入(即运算符是 '>'、'<'、'equals'、'starts with'、'ends with'之一)然后将其直接传递给您的查询,就像这样

 var operator = ...;
 var textvalue = ...;
 layer.setQuery("SELECT 'geometry',hhSanitHouseType FROM " + tableid + " WHERE 'hhSanitHouseType'" + operator + " '" + textvalue + "'");
于 2012-04-10T15:54:15.313 回答