【问题标题】:Reload DataTable with new JSON data not working使用新的 JSON 数据重新加载 DataTable 不起作用
【发布时间】:2021-04-04 07:50:32
【问题描述】:

我有一个包含数据的 DataTable,也有一个日期过滤器。当我请求具有过滤器值的数据时,相关数据会根据过滤器来。我想根据过滤后的数据更新 DataTable。我尝试了很多代码,但没有一个对我有用。

我的代码:

          $('#pending-appointments-daterange').daterangepicker({
              opens: 'right', 
              showWeekNumbers: true
            }, function(start, end, label) {

              $.ajax({
                type: "GET",
                url: '/get_pending_appointments',
                data: {
                  from: start.format('YYYY-MM-DD'),
                  to: end.format('YYYY-MM-DD')
                }
              })
              .done(function(res) {
                //$('#pending-appointments-datatable').DataTable().destroy();
                //$('#pending-appointments-datatable').DataTable().clear()
                //$('#pending-appointments-datatable').DataTable().ajax.json() = res.dataTableData
                //$('#pending-appointments-datatable').DataTable().clear().draw();
                //$('#pending-appointments-datatable').DataTable().draw();

               $('#pending-appointments-datatable').DataTable().clear().draw();

                //console.log(res)
                //$('#pending-appointments-datatable').url(res).load();
                //$('#pending-appointments-datatable').DataTable().ajax.reload();
              })
              .fail(function(err) {
                console.log(err)
              });

            });

如何使用过滤后的数据重新加载数据表?请帮助我。谢谢。

【问题讨论】:

    标签: javascript html jquery ajax datatables


    【解决方案1】:

    如果 res 是您的项目数组,您必须清除现有行,然后再次添加行。

              $('#pending-appointments-daterange').daterangepicker({
                  opens: 'right', 
                  showWeekNumbers: true
                }, function(start, end, label) {
    
                  $.ajax({
                    type: "GET",
                    url: '/get_pending_appointments',
                    data: {
                      from: start.format('YYYY-MM-DD'),
                      to: end.format('YYYY-MM-DD')
                    }
                  })
                  .done(function(res) {
    
                    $('#pending-appointments-datatable').DataTable().clear();
                    $('#pending-appointments-datatable').DataTable().rows.add(res);
                    $('#pending-appointments-datatable').DataTable().draw();
    
                    //console.log(res)
                    //$('#pending-appointments-datatable').url(res).load();
                    //$('#pending-appointments-datatable').DataTable().ajax.reload();
                  })
                  .fail(function(err) {
                    console.log(err)
                  });
    
                });

    【讨论】:

    • 我从后端获取 JSON 作为 res,如何转换为数组?
    • 请举个例子
    猜你喜欢
    • 2018-02-05
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多