2017-03-17 64 views
3

我正在製作過濾器以深入查看報告。我有兩個字段依賴於另一個字段(這是一個多選列表)的選擇。當我從這個多選字段中選擇一個或多個字段時,它看起來像是作爲一個數組被傳入並最終不被識別。這看起來好像我的參數被傳遞作爲數組,我想是問題該功能的參數是必需的,但未通過

Browser Debug Screen

JavaScript代碼:

function getMO() 
{ 
    var fa_code = $('#tslcFilterFA').val(); 
    var ao_code = $('#tslcFilterAO').val(); 
    var wq_code = $('#tslcFilterWQ').val(); 
    var newOption = ''; 
    $('#tslcFilterMO').empty(); 
    $('#tslcFilterMO').append(newOption); 
    $('#TSLC_MO_Loading').css('display','inline'); 

    $.ajax({ 
     url: '../TSLC/getData.cfc', 
     type:"POST", 
     cache: false, 
     dataType: "text", 
     async:true, 
     data: {method: "getMO", 
      fa_code: fa_code, 
      ao_code: ao_code, 
      wq_code: wq_code 
     }, 
     success: function(returnMsg) 
      { 

       try 
       { 
        var obj = JSON.parse(returnMsg); 
        $.each(obj.DATA, function(index,row) { 
         if (obj.DATA.length == 1) 
         { 
          var newOption = '<option selected="selected" value="' + row[0] + '">' + row[1] + '</option>'; 
         } 
         else 
         { 
          if (row[2] == "1") 
          { 
           var newOption = '<option selected="selected" value="' + row[0] + '">' + row[1] + '</option>'; 
          } 
          else 
          { 
           var newOption = '<option value="' + row[0] + '">' + row[1] + '</option>'; 
          } 


         } 

         $('#tslcFilterMO').append(newOption); 


        }); 
        try 
        { 
         $('#tslcFilterMO').multiselect('destroy'); 
        } 
        catch(e) {} 
        $('#tslcFilterMO').multiselect({ 
         selectedList: 4 
         }).multiselectfilter(); 
        $('.ui-multiselect').css('width','225px'); 
        $('#TSLC_MO_Loading').css('display','none'); 

       } 
       catch(e) 
       { 
        alert('getMO Error parsing JSON'); 
       } 



      }, 
     error: function(httpRequest, textStatus, errorThrown) 
       { 
        alert("getMO status=" + textStatus + ",error=" + errorThrown); 
      } 
     }); 
    } 

我試圖改變這種行:

var ao_code = $('#tslcFilterAO').val(); 

這樣:

var ao_code = $('#tslcFilterAO').multiselect('getChecked').map(function() {return this.value;}).get(); 

我也嘗試在URLDecode()中包裝我的ao_code變量以查看它是否將值作爲字符串而不是數組傳遞,但都不起作用。

CF碼(從組件):

<cffunction name="getMO" access="remote" returntype="any" returnformat="plain" hint="Get distinct Managing Orgs based on FA=ATT_IT and AO"> 
    <cfargument name="fa_code" type="string" required="true"> 
    <cfargument name="ao_code" required="true"> 
    <cfargument name="wq_code" required="true"> 

    <cfquery name="qMO" datasource="#request.dbdsn_ic4p#" username="#request.dbuser_m66266#" password="#request.dbpw_m66266#"> 
     SELECT DISTINCT managing_org MANAGING_ORG, DECODE(managing_org,'*','*ALL*',managing_org) MANAGING_ORG_DISPLAY, DECODE(managing_org,'*',1,2) sortcol 
     <cfif #fa_code# NEQ "ATT_IT"> 
      FROM HAS_TICKET_STATE_GUI_LOOKUP 
      WHERE client_id = '#fa_code#' 
     <cfelse> 
      FROM IT_TICKET_STATE_GUI_LOOKUP 
      WHERE 1=1 
     </cfif> 
     <cfif #ao_code# neq ""> 
      AND active_org IN (<cfqueryparam value="#ao_code#" cfsqltype="cf_sql_varchar" list="true" />) 
     </cfif> 
     <cfif #wq_code# neq ""> 
      <!--- !COM: is workaround for commas in listbox items! ---> 
      AND work_queue IN (#replace(ListQualify(wq_code,"'",",","CHAR"),":COM!",",","all")#) 
     </cfif> 
     ORDER BY 3, 1 
    </cfquery> 

    <cfreturn serializeJson(qMO)> 
</cffunction> 
+0

不相關的問題,但用於active_org的cfqueryparam標籤將同樣適合work_queue。 –

+0

傳統選項也許? –

回答

-1

你有沒有考慮jQuery的serializeArray()功能和param()功能的組合?它創建一個類似於容易解析的查詢字符串的字符串。您可以發送整個表單。

示例:$ .param($('#yourform')。serializeArray(),false);

或者,也許,只需使用serializeArray函數來發送您的函數JSON字符串?

3

更改這條線在你的JS代碼

var ao_code = $('#tslcFilterAO').val(); 

var ao_code = $('#tslcFilterAO').val().join(","); 

這應該給你,你在你的函數在期待多選場字符串值的列表CFC。

join()方法將數組的所有元素連接到一個字符串中。 More on "join" here

+0

儘管數據本身包含逗號,但您應該使用join()和cfqueryparam兩種不同的分隔符。 – Leigh

+0

謝謝...那有效。 – Vince

0

這篇文章幫我解決我的問題...... https://christierney.com/2011/06/07/returning-multiple-value-elements-to-coldfusion-remote-method-via-jquery-ajax/

function getWQ() 
{ 
var fa_code = $('#tslcFilterFA').val(); 
var ao_code = $('#tslcFilterAO').val(); 
if ($.isArray(ao_code)) 
    var ao_code_array = ao_code.join(","); 
else 
    var ao_code_array = ao_code; 
var mo_code = $('#tslcFilterMO').val(); 
if ($.isArray(mo_code)) 
    var mo_code_array = mo_code.join(","); 
else 
    var mo_code_array = mo_code; 
var newOption = ''; 
$('#tslcFilterWQ').empty(); 
$('#tslcFilterWQ').append(newOption); 
$('#TSLC_WQ_Loading').css('display','inline'); 

$.ajax({ 
    url: '../TSLC/cfcs/getData.cfc', 
    type:"POST", 
    cache: false, 
    dataType: "text", 
    async:true, 
    data: {method: "getWQ", 
     fa_code: fa_code, 
     mo_code: mo_code_array, 
     ao_code: ao_code_array 
    }, 
    success: function(returnMsg) 
    { 
+0

如果您的鏈接失敗,您應該在代碼中提供註釋,以解釋爲什麼以及如何回答OP的問題。 – Mike