问题
Is there any way to generate where condition based on user input dynamically.I have a select box with the option '>','<','equals','starts with','ends with'.Based on this condition where clause should be generated and query should be executed.Please help me.i need examples. since i have around 80 columns in my table i cant use if else loop.
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");
}
}
回答1:
Maybe you can check this example, especially the function generateWhere(columnName, low, high)
.
You don't have to use if/else for your operator, just check for a valid input (i.e. that the operator is one of '>','<','equals','starts with','ends with') and then pass it directly to your query, something like that
var operator = ...;
var textvalue = ...;
layer.setQuery("SELECT 'geometry',hhSanitHouseType FROM " + tableid + " WHERE 'hhSanitHouseType'" + operator + " '" + textvalue + "'");
来源:https://stackoverflow.com/questions/10083120/fusiontable-query-for-where