weixin_33726318 2014-11-24 10:50 采纳率: 0%
浏览 22

如何在Ajax中使用数据表

I want to try pagination using datatables.net via ajax.

I have 2 input fields which sends start date and end date and following is the table structure

<table class="table table-hover" id="example">
              <thead>
          <tr>
          <th>Loan ID</th>
            <th>Date</th>
            <th>Loan Amount</th>
            <th>Loan Type</th>


          </tr>
        </thead>
        <tbody id="amount"></tbody></table>

when the button is clicked,I am calling this function

$('#recv').click(function(){
      var fromDate=$('#from').val();
      var toDate=$('#to').val();

      var test=$.ajax({
          type: "GET", 
           url: "/project/getDetails",  
              data:{"fromDate":fromDate,"toDate":toDate},
              dataType:"json",async:false 
      }).responseText;
      var result = eval('('+test+')');
      $('#example').dataTable( {
          "bServerSide": true,
            "sAjaxSource": result,
            "bProcessing": true,
            "aoColumns": [
                            { "sName": "Loan_ID",
                                "bSearchable": false,
                                "bSortable": false

                            },
                            { "sName": "Date" },
                            { "sName": "Loan_Amount" },
                            { "sName": "Loan_Type" }
                        ]
        } );

When Ever I click on the button then I get error in alert box saying datatables warning id=example ajax error

Can anybody please tell me how to solve?

  • 写回答

1条回答 默认 最新

  • weixin_33695450 2014-11-24 12:10
    关注

    Let datatables take care of the ajax call:

    var oTable = $('#example').dataTable( {
        "bServerSide": true,
        "iDeferLoading": 1,
        "sAjaxSource": "/project/getDetails",
        "fnServerParams": function (aoData) {
            aoData.push({ "name": "fromDate", "value": $('#from').val() });
            aoData.push({ "name": "toDate", "value": $('#to').val() });
            },
        "bProcessing": true,
        "aoColumns": [
                      { "sName": "Loan_ID",
                        "bSearchable": false,
                        "bSortable": false
                      },
                    { "sName": "Date" },
                    { "sName": "Loan_Amount" },
                    { "sName": "Loan_Type" }
                  ]
            } );
    
    $('#recv').click(function(){
        oTable.fnDraw();
    });
    

    Points to note:

    • fnServerParams this passes your start & end date to the getDetails function
    • iDeferLoading - prevent table population on page load
    • fnDraw() populates the datatable from your click event
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?