简体   繁体   English

Ajax 调用成功后,DataTables 搜索框不起作用

[英]After Ajax call succeed, DataTables search box doesn't work

I'm working on a project that php, js, jquery, datatables are included.我正在开发一个包含 php、js、jquery、数据表的项目。

gif

I'm sending post call to a PHP page to change my table data, it's running successfully as you see.我正在向 PHP 页面发送 post 调用以更改我的表数据,如您所见,它正在成功运行。 After this call, I can't use my DataTable search feature strangely.在此调用之后,我无法奇怪地使用我的 DataTable 搜索功能。 May be the error is about $(".gunlukgelir").load(" .gunlukgelir");可能错误是关于$(".gunlukgelir").load(" .gunlukgelir"); when Ajax call is succeed, I reflesh the tables with the .gunlukgelir class name.当 Ajax 调用成功时,我用.gunlukgelir类名.gunlukgelir表。

Libraries:图书馆:

https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css
https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js
https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js
https://code.jquery.com/jquery-3.3.1.min.js

my Ajax call:我的 Ajax 调用:

$(function() {
  $("#gelirgetir").click(function() {
    var gelirtablosecimi = $("#select1").val();
    if (gelirtablosecimi) {
      $.ajax({
        type: "POST",
        url: "tabloyenile.php",
        data: {
          "gelirtablosecimi": gelirtablosecimi
        },
        success: function(result) {
          $(".gunlukgelir").load(" .gunlukgelir");
          //$(".gunlukgelir").load(window.location + " .gunlukgelir");
          notifyUser('success', 'Başarılı!', 'Tablo başarıyla güncellendi');
        },
        error: function(result) {
          notifyUser('error', 'Hata', 'error');
        }
      });
    } else {
      notifyUser('info', 'Dikkat', 'Tablo seçimi yapmadınız!');
    }

  });

HTML part: HTML部分:

<div class="form-group col-xs-6">

  <select id="select1" class="selectpicker" data-live-search="true" title="Gelirler">
    <?php echo $geliroptions;?>
  </select>
  <input type="submit" id="gelirgetir" value="Getir" class="btn btn-success" />

  <h1>Aylık Gelir Raporları</h1>
  <table id="" class="display table table-stripe table-hover table-row-border table-order-column table-nowrap gunlukgelir">
    <thead>
      <tr>
        <th>Tarih</th>
        <th>Günlük Toplam</th>
      </tr>
    </thead>
    <?php
    $gelirtabloadi = $_SESSION["gelirtabloadi"];
    $gelirgunluktoplam = $db->prepare("select tarih, hasilat + visa + butce_ici + hisse_satis + sosyal_konut + elektrik + haberlesme + iller_bank + diger AS Toplam from $gelirtabloadi");
    $gelirgunluktoplam->execute();
    while($row = $gelirgunluktoplam->fetch()){
     echo '
      <tr>
        <td>'.$row["tarih"].'</td>  
        <td>'.$row["Toplam"].'</td>                                    
      </tr>
     ';
    }
    ?>
  </table>
</div>

and PHP that Ajax calls:和 Ajax 调用的 PHP:

<?php
  session_start();
  if($_POST['gelirtablosecimi'] && $_POST['gidertablosecimi']){
    $gidertabloadi = $_POST["gidertablosecimi"];
    $gelirtabloadi = $_POST["gelirtablosecimi"];
    $_SESSION["gelirtabloadi"] = $gelirtabloadi;
    $_SESSION["gidertabloadi"] = $gidertabloadi;
  }
  if($_POST["gelirtablosecimi"]){
    $gelirtabloadi = $_POST["gelirtablosecimi"];
    $_SESSION["gelirtabloadi"] = $gelirtabloadi;
  }
  if($_POST['gidertablosecimi']){
    $gidertabloadi = $_POST["gidertablosecimi"];
    $_SESSION["gidertabloadi"] = $gidertabloadi;
  }
?>

Any suggestion?有什么建议吗? Thanks in advance!提前致谢!

I also had such a problem.我也有这样的问题。 Just add .DataTable() in success function of ajax after assigning the data to data table.将数据分配到数据表后,只需在ajax的成功函数中添加.DataTable()即可。
It has been working for me.它一直在为我工作。
Just write it as follows:只需按如下方式编写:

function TaxLoad() {
    $.ajax({
        url:"myCode.php",
        type:"POST",
        data:{functionName:"showTax"},
        success:function(data) {
            $("#tblTaxBody").html(data);
            var table= $("#dtTable").DataTable();
        }
    });
}`

Probably this issue related with my problem in the Docs.这个问题可能与我在文档中的问题有关。 If It reinitialised, it would be searching: false Anyway, If one day anybody sees this problem, just change your table choice to Bootstrap-Table .如果它重新初始化,它将searching: false无论如何,如果有一天有人看到这个问题,只需将您的表选择更改为Bootstrap-Table It's much more stable.它要稳定得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM