如何更改数据表中的数据

时间:2019-10-15 05:22:46

标签: javascript jquery ajax datatable datatables

我有一个DataTable(https://datatables.net),我想做一些事情,它们都与刷新表本身有关。这是我的代码:

let statusList = getStatusList();

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

getRes(function (result) { // APPLIED CALLBACK
  $('#resdatatable').dataTable({
     data: result,             // YOUR RESULT
      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'
  }];
}

function changeResStatus(str1) {
    var id = str1;
    var status = document.getElementById("resstatus" + id).value;
    var mailres = "";

    var r = confirm("Change Status for ID # " + id + " to " + status + "?");
    if (r == true) {

        if (document.getElementById("resstatus" + id).value == "Confirmed"){
            var s = confirm("Send ID # " + id + " a confirmation email?");
            if (s == true) {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("result").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","sendconfirmationemail.php?id="+id,true);
        xmlhttp.send();
        }
        }

        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("result").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","changeresstatus.php?id="+id+"&status="+status,true);
        xmlhttp.send();

    }else{
        document.getElementById("result").innerHTML = "<div class='errormessage'>Change status action aborted</div>";
        getrestable();
    }
    }

您可以看到我的DataTable在页面加载时呈现。我想更改cityselect时更新DataTable,现在我有了:

<select id="cityselect" onchange="getRes()">

但这不起作用。我该怎么做?

编辑:我将问题更改为仅问一个问题。

1 个答案:

答案 0 :(得分:0)

实施这一行代码

 $('#Datatable').DataTable().ajax.reload();