2012-04-10 71 views
0

有沒有什麼方法可以根據用戶輸入動態地生成條件。我有一個選擇框'>','<','等於','以'開始','以'結尾' 。基於這個條件where子句應該被生成並且查詢應該被執行。請幫助我,我需要例子。 由於我在我的表中有大約80列,我不能使用if else循環。fusiontable query for where

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"); 
    } 
} 

回答

0

也許你可以check this example,尤其是函數generateWhere(columnName, low, high)。只需檢查一個有效的輸入(即運算符是'>','<','等於','以'開始','以'結尾),然後直接傳遞給您的查詢,類似的東西

var operator = ...; 
var textvalue = ...; 
layer.setQuery("SELECT 'geometry',hhSanitHouseType FROM " + tableid + " WHERE 'hhSanitHouseType'" + operator + " '" + textvalue + "'"); 
+0

謝謝!簡單和偉大的想法...... – 7783 2012-04-11 05:18:53

+0

如果假設我有三個變量,如'columnname','運營商','textvalue'。是否有可能獲得列名作爲變量,並將其傳遞給query.please電話我如何做這個。 – 7783 2012-04-11 06:49:54

+0

我不想在查詢中指定hhSanitHouseType。它也應該直接從一個名爲'columnname'的變量中獲取。 – 7783 2012-04-11 06:51:59