游.程 2019-10-16 21:03 采纳率: 0%
浏览 76

刷新DataTable的问题

I am having two issues with my DataTable (https://datatables.net) first issue is I am trying to get my Data Table to display new information when a select box is changed, here is my code:

    let statusList = getStatusList();

function getRes(callback) { // ADDED CALLBACK
    let city = document.getElementById("cityselect").value;
    $.ajax({
        type: 'get',
        url: 'getreservationstable.php?city='+city,
        dataType: 'json',
        cache: false,
        success: callback  // USED CALLBACK
    });
}

function changeCity()
{
    $('#resdatatable').DataTable().ajax.reload();
}

getRes(function (result) { // APPLIED CALLBACK
  $('#resdatatable').DataTable({
     data: result,             // YOUR RESULT
     destroy: true,
      columns: [
        { data: 'id', title: 'ID' },
        { data: 'bookingdatetime', title: 'Booking Date' },
        { data: 'name', title: 'Name' },
        { data: 'class', title: 'Class' },
        { data: 'pickupdatetime', title: 'Pick up' },
        { data: 'duration', title: 'Duration' },
        { data: 'dropdatetime', title: 'Drop off' },
        { data: 'age', title: 'Age' },
        { data: 'coverage', title: 'Coverage' },
        { data: 'quote', title: 'Quote' },
        {
          data: 'status',
          title: 'Status',
          render: function(data, type, row) {
            let isKnown = statusList.filter(function(k) { return k.id === data; }).length > 0;
            if (isKnown) {
              return $('<select id ="resstatus'  + row.id + '" onchange="changeResStatus(' + row.id + ')">', {
                id: 'resstatus-' + row.id, // custom id
                value: data
              }).append(statusList.map(function(knownStatus) {
                let $option = $('<option>', {
                  text: knownStatus.text,
                  value: knownStatus.id
                });
                if (row.status === knownStatus.id) {
                  $option.attr('selected', 'selected');
                }
                return $option;
              })).on('change', function() {
                changeresstatus(row.id); // Call change with row ID
              }).prop('outerHTML');
            } else {
              return data;
            }
          }
        }
      ]
    });
});

/**
 * jQuery plugin to convert text in a cell to a dropdown
 */
(function($) {
  $.fn.createDropDown = function(items) {
    let oldTxt = this.text();
    let isKnown = items.filter(function(k) { return k.id === oldTxt; }).length > 0;
    if (isKnown) {
      this.empty().append($('<select>').append(items.map(function(item) {
        let $option = $('<option>', {
          text: item.text,
          value: item.id
        });
        if (item.id === oldTxt) {
          $option.attr('selected', 'selected');
        }
        return $option;
      })));
    }
    return this;
  };
})(jQuery);

// If you remove the renderer above and change this to true,
// you can call this, but it will run once...
if (false) {
  $('#resdatatable > tbody tr').each(function(i, tr) {
    $(tr).find('td').last().createDropDown(statusList);
  });
}


function getStatusList() {
  return [{
    id: 'Confirmed',
    text: 'Confirmed'
  }, {
    id: 'Unconfirmed',
    text: 'Unconfirmed'
  }, {
    id: 'Open',
    text: 'Open'
  }, {
    id: 'Closed',
    text: 'Closed'
  }, {
    id: 'Canceled',
    text: 'Canceled'
  }];
}

And I have a select box that looks like this:

<select id="cityselect" onchange="changeCity()">
<option value="la">Los Angeles</option>
<option value="oc">Orange County</option>
<option value="sf">San Francisco</option>
</select>

When I try to change the city, it gives me this error:

DataTables warning: table id=resdatatable - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

And Console reads:

Uncaught TypeError: Cannot set property 'data' of null

The JSON response is 100% valid. Can someone explain to me why this is not working?

Edit: Here is the JSON data, it's legit, it loads on page load but no on select switch:

[{"id":"1","bookingdatetime":"10\/02\/19 @ 10:35 pm","name":"Tony ","class":"travel","pickupdatetime":"10\/03\/19 @ 01:00 pm","duration":"013 days","dropdatetime":"10\/16\/19 @ 06:30 pm (sf) ","age":"21+","coverage":"inclusive","quote":"$973","status":"Confirmed"},{"id":"11","bookingdatetime":"10\/03\/20 @ 02:17 pm","name":"Daniel  ","class":"travel","pickupdatetime":"10\/04\/19 @ 04:30 am","duration":"027 days","dropdatetime":"10\/31\/19 @ 06:30 pm ","age":"21+","coverage":"inclusive","quote":"$1356","status":"Confirmed"},{"id":"12","bookingdatetime":"10\/03\/19 @ 02:17 pm","name":"Richard Phillips ","class":"suv","pickupdatetime":"10\/08\/19 @ 12:30 pm","duration":"007 days","dropdatetime":"10\/15\/19 @ 04:30 pm (sf) ","age":"21+","coverage":"inclusive","quote":"$849","status":"Unconfirmed"},{"id":"13","bookingdatetime":"10\/03\/19 @ 02:38 pm","name":" ESCOBAR ","class":"convert","pickupdatetime":"10\/12\/19 @ 10:00 am","duration":"014 days","dropdatetime":"10\/26\/19 @ 08:30 pm (oc) ","age":"19","coverage":"inclusive","quote":"$1638","status":"Unconfirmed"},{"id":"14","bookingdatetime":"10\/03\/19 @ 02:40 pm","name":"Tony ","class":"local","pickupdatetime":"10\/04\/19 @ 02:00 pm","duration":"005 days","dropdatetime":"10\/09\/19 @ 05:30 pm ","age":"21+","coverage":"inclusive","quote":"$254","status":"Confirmed"},{"id":"15","bookingdatetime":"10\/03\/19 @ 02:51 pm","name":"YA ES","class":"local","pickupdatetime":"10\/30\/19 @ 11:00 am","duration":"092 days","dropdatetime":"01\/30\/20 @ 11:00 pm ","age":"21+","coverage":"inclusive","quote":"$2376","status":"Confirmed"},{"id":"16","bookingdatetime":"11\/17\/20 @ 02:56 pm","name":"yayoi escobar","class":"local","pickupdatetime":"10\/30\/19 @ 11:00 am","duration":"092 days","dropdatetime":"01\/30\/20 @ 11:00 pm ","age":"21+","coverage":"inclusive","quote":"$0","status":"Confirmed"},{"id":"18","bookingdatetime":"10\/03\/19 @ 03:39 pm","name":"yayoi escobar","class":"local","pickupdatetime":"10\/09\/19 @ 12:30 pm","duration":"020 days","dropdatetime":"10\/29\/19 @ 08:00 pm ","age":"21+","coverage":"inclusive","quote":"$653","status":"Confirmed"},{"id":"19","bookingdatetime":"10\/03\/19 @ 03:40 pm","name":"yayoi escobar","class":"local","pickupdatetime":"10\/08\/19 @ 12:00 pm","duration":"013 days","dropdatetime":"10\/21\/19 @ 09:00 pm ","age":"21+","coverage":"inclusive","quote":"$454","status":"Confirmed"},{"id":"20","bookingdatetime":"10\/03\/19 @ 03:52 pm","name":"Daniel ","class":"travel","pickupdatetime":"10\/22\/19 @ 01:00 pm","duration":"006 days","dropdatetime":"10\/28\/19 @ 07:30 pm ","age":"21+","coverage":"inclusive","quote":"$469","status":"Unconfirmed"},{"id":"25","bookingdatetime":"10\/03\/19 @ 03:39 pm","name":"yayoi escobar","class":"local","pickupdatetime":"10\/09\/19 @ 12:30 pm","duration":"020 days","dropdatetime":"10\/29\/19 @ 08:00 pm ","age":"21+","coverage":"inclusive","quote":"$653","status":"Confirmed"}]
  • 写回答

1条回答 默认 最新

  • weixin_33694620 2019-10-19 06:51
    关注

    Seams like you try to trigger $('#resdatatable').DataTable().ajax.reload(); on changeCity() event, but never define ajax option on your DataTable constructor.

    eg from documentation.

    $(document).ready(function() {
        $('#example').DataTable( {
            "ajax": "data/arrays.txt"
        } );
    } );
    

    As you defined a separate call using $.ajax, my guess is you would like to keep that logic and adapt your onChange event, so best option would be to rewrite changeCity this way :

    function changeCity()
    {
        getRes(function (result) {
            var datatable = $('#resdatatable').DataTable()
            datatable.clear();
            datatable.rows.add(result);
            datatable.draw();
        });
    }
    

    Refer to this post about how to manually update datatables table with new JSON data.

    API references:

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置