weixin_33737134 2018-02-28 13:55 采纳率: 0%
浏览 38

数据表,Javascript和Json

I have a Django Python webapp , i have a function :

def showreport(newrequest) :
        rep1 = get_report_data(newrequest,2)
        data={['columns':rep1[0],'rows':rep1[1]}
        return JsonResponse(data,safe=False)

i call this function from javascript in HTML page , the data return is an array with two elements, 1 represent columns and the other the data.

I want to present in the HTML page the data in a DataTable object , and since the columns and data is dynamic i want to create the DataTable dynamically

In the HTML

In JavaScript $(document).ready(function () {

 $("#showresults").on('click', function(evt) {
  evt.preventDefault();
 $('#show_loading').show();
$('#theTable').hide();
froms = document.getElementById('startdate').value;
tos = document.getElementById('todate').value;        
    $.ajax({
            type: "POST",
            url: 'showreport',
            data: {
                'start_date' : froms,
                'end_date' :tos,
                   'csrfmiddlewaretoken': '{{ csrf_token }}'
            },
            success: function (data, textStatus, jqXHR) {

               $('#show_loading').hide();          
   var rowSet=data['rows'];
 var columnset =data['columns'];

       $('#theTable').DataTable({
    "processing": true,
    searching: false,
    paging: false,
   "bInfo" : false,
 columns:  [columnset]  ,
    data: [rowSet]
   } ); 
$('#theTable').show();

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('#show_loading').hide();
                alert("Error, please try again!");
            }
});  
        });    

    });

Now the problem i have is that the Columns are not presented and the data is presenting only 1 row and it is not separated to columns.

In inspect mode , i can see {"rows": [["Test1", "Test2"],["Test3", "Test4"] etc..., "columns": ["col1","col2"]}

What am i doing wrong.

Thanks, N

  • 写回答

1条回答 默认 最新

  • 7*4 2018-02-28 14:30
    关注

    I think you have an array of an array :)

    try

    change this

    columns: [columnset],
    data: [rowSet]
    

    to this

    columns: columnset,
    data: rowSet
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog