weixin_33724570 2014-08-15 09:11 采纳率: 0%
浏览 44

数据表Jquery Ajax

I have an issue with the reinitialisation of my datatable. My code below works by pulling in a json from getOrderStatus.php and upon success of this puts all the json data into javascript variables and then from this i can set these variables to div tags and display the data i need on my webpage. However the Datatable cannot be reinititalised once the ajax loop runs and displays the following error message "DataTables warning: table id=mytable - Cannot reinitialise DataTable". I believe i need a way to kill the table and recreate it upon the ajax refresh however i cant seem to find a way to do this ?

     $(document).ready(function ajaxLoop(){

      $.ajax({ 

        url: 'getOrderStatus.php',                     // Url of Php file to run sql         
        data: "",                                      
        dataType: 'json',                              //data format      
        success: function updateOrder(data)               //on reciept of reply
        {
          var OrdersSubmitted = data.OrderStatus[0].SUBMITTED;     //get Orders Submitted Count
          var OrdersFulfilled = data.OrderStatus[0].FULFILLED;     //get Orders Fulfilled count
          var LastTransaction = data.LastTransaction[0];           //get Last Transaction


          //--------------------------------------------------------------------
          // 3) Update html content
          //--------------------------------------------------------------------
          $('#OrdersSubmitted').html(OrdersSubmitted);
          $('#OrdersFulfilled').html(OrdersFulfilled); //Set output html divs
          $('#mytable').dataTable({
                  "data": LastTransaction,
                  "aging": false,
                  "searching": false,
                  "columns": [
                      { "title": "ORDER_ID" }, // <-- which values to use inside object
                      { "title": "STATUS" },
                      { "title": "ACC_NUMBER" },
                      { "title": "SORT_CODE" }

                      ]
                    });


        setTimeout(ajaxLoop, 2000);
        } 

      });
    });
  • 写回答

1条回答 默认 最新

  • weixin_33696822 2014-08-15 09:14
    关注

    Did you try using "bDestroy": true.

    $('#mytable').dataTable({
                      "data": LastTransaction,
                      "aging": false,
                      "searching": false,
                      "bDestroy": true,
                      "columns": [
                          { "title": "ORDER_ID" }, // <-- which values to use inside object
                          { "title": "STATUS" },
                          { "title": "ACC_NUMBER" },
                          { "title": "SORT_CODE" }
    
                          ]
                        });
    

    Another way is if you check if datatable is already init. on your table

    var table = $('#mytable');
    if ($.fn.DataTable.fnIsDataTable(table)) {
    //It's already a datatable
      //clear and destroy
      table.dataTable().fnClearTable();
      table.dataTable().fnDestroy();
    
    }
    
    
    **It seems your are using latest datatable version:**
    

    then option should be destroy:true (aging should be changed to paging):

     $('#mytable').dataTable({
                          "data": LastTransaction,
                          "paging": false,
                          "searching": false,
                          "destroy": true,
                          "columns": [
                              { "title": "ORDER_ID" }, // <-- which values to use inside object
                              { "title": "STATUS" },
                              { "title": "ACC_NUMBER" },
                              { "title": "SORT_CODE" }
    
                              ]
                            });
    

    and check on existing datatable would be:

    if($.fn.DataTable.isDataTable("#myTable"))
    {
    $('#myTable').DataTable().clear().destroy();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题