2017-01-14 67 views
-1

我有一個名爲search_gen的變量。這個變量是由ajax生成的(代碼如下所示)。如何將可變JavaScript轉換爲數據數據表

var search_gen; 
$.ajax({ 
    type: "POST", 
    url: link+module_name+'search_generator/'+module_active, 
    dataType: "text", 
    async: false, 
    success: function(data){ 
     search_gen = data; //or something similar 
    } 
}); 

例如此變量將包含JSON數據(顯示波紋管)

{"name":"room_type_name","value":$("#room_type_name").val()},{"name":"room_type_code","value":$("#room_type_code" 
).val()} 

如果我放置JSON以上無需使用可變它的工作示例代碼波紋管

table=$('#table').dataTable({ 
    "sScrollY": "400px", 
    "bFilter": false, 
    "bProcessing": true, 
    "bServerSide": true, 
    "sServerMethod": "GET", 
    "sAjaxSource": link+module_name+'populate_list/'+module_active, 
    "iDisplayLength": 25, 
    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], 
    "columnDefs": [ { 
      "targets": 0, 
      "orderable": false 
      }, 
      { 
      "targets": -1, 
      "orderable": false 
      } ], 
    "fnServerParams": function (aoData) { 
    aoData.push({"name":"room_type_name","value":$("#room_type_name").val()}, 
       {"name":"room_type_code","value":$("#room_type_code").val()}) 
       } 
}) 

,當我'm使用變量並放入支架aodata(seach_gen)(代碼下方)

table=$('#table').dataTable({ 
     "sScrollY": "400px", 
     "bFilter": false, 
     "bProcessing": true, 
     "bServerSide": true, 
     "sServerMethod": "GET", 
     "sAjaxSource": link+module_name+'populate_list/'+module_active, 
     "iDisplayLength": 25, 
     "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], 
     "columnDefs": [ { 
       "targets": 0, 
       "orderable": false 
       }, 
       { 
       "targets": -1, 
       "orderable": false 
       } ], 
     "fnServerParams": function (aoData) { 
     aoData.push(search_gen) 
} 
    }); 

它顯示了這樣的錯誤。

我的問題是 我如何通過我的變量search_genaodata.push()

謝謝

回答

0

我假設你是不是等待第一個AJAX調用調用.dataTable之前完成。試試下面的代碼:

var search_gen; 
$.ajax({ 
    type: "POST", 
    url: link+module_name+'search_generator/'+module_active, 
    dataType: "text", 
    async: false, 
    success: function(data){ 
     search_gen = data; //or something similar 
    } 
}).done(function(data) { 
    table=$('#table').dataTable({ 
     "sScrollY": "400px", 
     "bFilter": false, 
     "bProcessing": true, 
     "bServerSide": true, 
     "sServerMethod": "GET", 
     "sAjaxSource": link+module_name+'populate_list/'+module_active, 
     "iDisplayLength": 25, 
     "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], 
     "columnDefs": [ { 
       "targets": 0, 
       "orderable": false 
       }, 
       { 
       "targets": -1, 
       "orderable": false 
       } ], 
     "fnServerParams": function (aoData) { 
     //aoData.push(search_gen) 
     Array.prototype.push.apply(aoData,search_gen); // <<<<<<<<<<<< use this 
     } 
    }); 
}); 

或者,你可以調用.dataTablesuccess回調的第一$.ajax和使用data參數代替search_gen,如aoData.push(data)

編輯:

基於您的評論,我認爲search_gen是一個數組,在這種情況下,你將需要使用Array.prototype.push.apply(aoData,search_gen);,而不是aoData,.push(search_gen);。檢查上面的代碼。

+0

我已經嘗試了你的建議,但它仍然不起作用。並仍然有相同的錯誤信息。 –