2017-02-18 53 views
0

我有變化從ajax返回,並返回JSON類型。但是這種變化無法推送到dataTable的aaData。 我有console.log這個變化,並複製粘貼到數據表aaData,它也運行。從ajax Retun數據並推送到數據表

$.ajax({ 
    type: "POST", 
    url: "/khuyenmai/ajax/chonhanghoa", 
    data: {data : value}, 
    cache: false, 
    dateType: "json", 
    success: function(data){ 
     //var dulieu = JSON.stringify(data);; 
     console.log(data); 
     $(".table_hanghoa").dataTable({ 
      "aaData" : data, 
      "aoColumns": [ 
       { "sTitle": "Mã Hàng", "mData": "ma"}, 
       { "sTitle": "Tên Hàng", "mData": "name" }, 
       { "sTitle": "Thuộc Nhóm", "mData": "nhom"}, 
       { "sTitle": "Hình Đại Diện", "mData": "hinh"}, 
       { "sTitle": "Chọn", "mData": "check"} 
      ], 
     }); 
    } 
}); 
+0

分享你在'console.log(data)中獲得什麼;'? – C2486

+0

我在底部 –

回答

0

解決:) :)

<script> 
    $('#chonnhom').change(function(e){ 
    $('#table_hanghoa').dataTable().fnDestroy(); 
    var value = $('#chonnhom').val(); 
    var table = $('#table_hanghoa').DataTable({ 
     processing: true, 
     "ajax": { 
      url: "/khuyenmai/ajax/chonhanghoa", 
      dataSrc: "", 
      type : 'POST', 
      data: { data: value} 
     }, 
     columns: [ 
        {data: "ma"}, 
        {data: "name"}, 
        {data: "nhom"}, 
        {data: "hinh"}, 
        {data: "check"} 
       ], 
    }); 
     table.draw(); 
     e.preventDefault(); 
    }); 
</script> 
0

嘗試一些事情是這樣的:

$(document).ready(function() { 
    var dataSet = []; 
    dataSet.push(
     [1,"Sasha","Brenna","0800 1111"], 
     [2,"Sage","Mia","(01012) 405872"], 
     [3,"Chelsea","Allistair","076 0578 0265"], 
     [4,"Uta","Savannah","070 1247 5884"], 
     [5,"Remedios","Berk","0995 181 0007"], 
    ); 

    // In your case the dataSet is coming from ajax call 

    $('#data_table').DataTable({ 
     data: dataSet 
    }); 
}); 

table id="data_table"> 
     <thead> 
      <tr> 
       <th>Emp Code</th> 
       <th>First Name</th> 
       <th>Last Name</th> 
       <th>Mobile</th> 
      </tr> 
     </thead> 

     <tbody> 
     <!-- Dynamic Body --> 
     </tbody> 
</table> 
+0

表揚我的類型是json –

+0

您可以使用JSON.parse()方法將其轉換。 –

0

這是我的JSON

[{"ma":"HH13117001","name":"\u00c1o Kho\u00e1c H\u00e0n Qu\u1ed1c 2016","nhom":"\u00c1o Kho\u00e1c H\u00e0ng Qu\u1ed1c - Nam","hinh":"<img src='http:\/\/warehouse.client\/public\/img\/imggoods\/1486890821_TTxv.jpg' class='imgtt'>","check":"<input type='checkbox' name='idhh[]' value='43' style='font-size: 30px;' checked >"},{"ma":"HH13117002","name":"\u00c1o Thun H\u00e0n 2","nhom":"\u00c1o Kho\u00e1c H\u00e0ng Qu\u1ed1c - Nam","hinh":"<img src='http:\/\/warehouse.client\/public\/img\/imggoods\/1486914583_jN4a.jpg' class='imgtt'>","check":"<input type='checkbox' name='idhh[]' value='50' style='font-size: 30px;' checked >"}] 

,當我加入這個我的代碼它的運行以及

$.ajax({ 
      type: "POST", 
      url: "/khuyenmai/ajax/chonhanghoa", 
      data: {data : value}, 
      cache: false, 
      dateType:"json", 
      success: function(data){ 
      //var dulieu = JSON.stringify(data);; 
      console.log(data); 
      $(".table_hanghoa").dataTable({ 
       "aaData" : [{"ma":"HH13117001","name":"\u00c1o Kho\u00e1c H\u00e0n Qu\u1ed1c 2016","nhom":"\u00c1o Kho\u00e1c H\u00e0ng Qu\u1ed1c - Nam","hinh":"<img src='http:\/\/warehouse.client\/public\/img\/imggoods\/1486890821_TTxv.jpg' class='imgtt'>","check":"<input type='checkbox' name='idhh[]' value='43' style='font-size: 30px;' checked >"},{"ma":"HH13117002","name":"\u00c1o Thun H\u00e0n 2","nhom":"\u00c1o Kho\u00e1c H\u00e0ng Qu\u1ed1c - Nam","hinh":"<img src='http:\/\/warehouse.client\/public\/img\/imggoods\/1486914583_jN4a.jpg' class='imgtt'>","check":"<input type='checkbox' name='idhh[]' value='50' style='font-size: 30px;' checked >"}], 
       "aoColumns": [ 
       { "sTitle": "Mã Hàng", "mData": "ma"}, 
       { "sTitle": "Tên Hàng", "mData": "name" }, 
       { "sTitle": "Thuộc Nhóm", "mData": "nhom"}, 
       { "sTitle": "Hình Đại Diện", "mData": "hinh"}, 
       { "sTitle": "Chọn", "mData": "check"}], 
      }); 
      } 
     });