如何将变量JavaScript推送到aodata数据表中

时间:2017-01-14 05:17:54

标签: javascript json ajax datatable

我有一个名为search_gen的变量。此变量由ajax生成(代码如下所示)。

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()})
                }
})

当我使用变量并放入括号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)
}
    });

它显示了这样的错误。 enter image description here

我的问题是 如何将变量search_gen传递给aodata.push()

谢谢

1 个答案:

答案 0 :(得分:0)

我假设您在调用.dataTable之前没有等待第一个AJAX调用完成。请尝试以下代码:

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

或者,您可以在.dataTable的{​​{1}}回调内拨打success,并使用$.ajax参数代替data,例如search_gen

修改

根据您的评论,我认为aoData.push(data)是一个数组,在这种情况下,您需要使用search_gen而不是Array.prototype.push.apply(aoData,search_gen);。检查上面的代码。