简体   繁体   English

Jquery - 引导数据表刷新问题数据

[英]Jquery - Bootstrap DataTable Refresh issue DATA

Could you please assist me with this issue related to Bootstrap - Jquery Table?您能否帮助我解决与 Bootstrap - Jquery Table 相关的问题?

I would like to retrieve data from a database server In JSON Format (through an ajax call) into a bootstrap DataTable and then reload the DataTable only.我想从 JSON 格式的数据库服务器中检索数据(通过 ajax 调用)到引导数据表中,然后仅重新加载数据表。

Here you are the code, this will be run once pressed the click button (search):这是代码,一旦按下点击按钮(搜索)就会运行:

var prova = null;

$(document).ready(function(){
        prova = $('#prova_table').DataTable({
            paging: true,
            searching: false
        });
        prendivalori();
       });  


function prendivalori() {
       $("#bottone").click(function() {
        $("#prova_table").empty();
        var sopravvissuti = $('#sopravvissuti').val();
        var vita = $('#vita').val();
        var provincia = $('#provincia').val();
        var campi = $('#campi').val();
        var table = $('#prova_table');

        $.ajax({
                type: 'GET',
                url: './php/select.php',
                data: {'sopravvissuti': sopravvissuti, 'vita': vita, 'provincia':provincia, 'campi':campi},
                dataType: 'json',
                success: function(data) {
                                    table.append("<thead><tr><th class='th-sm'>Cognome</th><th class='th-sm'>Nome</th><th class='th-sm'>Sesso</th></tr></thead>");
                                    console.log(data);
                                    len=data[0].length;
                                    table.append("<tbody>");
                        for(i=0; i< len; i++){

                            temp=data[0][i]
                            table.append("<tr><td>" + temp[1] + "</td><td>" + temp[0] + "</td><td>" 
                            + temp[2] +"</td></tr>");
                        }
                                                table.append("</tbody>");

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

                        }

                ,
                error: function(data) {
                        alert('Assicurarsi di aver selezionato tutte le caselle');

                }
        });

    });
};

Here you are the error message received once clicked button data...这是单击按钮数据后收到的错误消息...

DataTables warning: table id=prova_table - Invalid JSON response. DataTables 警告:表 id=prova_table - 无效的 JSON 响应。 For more information about this error, please see http://datatables.net/tn/1有关此错误的更多信息,请参阅http://datatables.net/tn/1

The json data received from the server are correct because we can see them properly into the table but we are unable to use all the functions Bootstrap DataTable provides like Pagination search and number of entries ....从服务器接收到的 json 数据是正确的,因为我们可以在表格中正确看到它们,但是我们无法使用 Bootstrap DataTable 提供的所有功能,例如分页搜索条目数...。

I'm using all the updated links for running the website..我正在使用所有更新的链接来运行网站..

ETC ECT等电通

Here you are a JSON response:这是 JSON 响应:

JSON 响应

Many thanks in advance for all you kind support Have a nice day Andrea非常感谢您的所有支持祝您有美好的一天安德里亚

You should not do table.append() and any other direct dom changes on table.你不应该在 table 上做 table.append() 和任何其他直接的 dom 更改。 Jquery data table will do this for you if you pass options to it in right way.如果您以正确的方式将选项传递给 Jquery 数据表,它将为您执行此操作。

Do it this way.这样做。

First initialize the datatable like below with column names if available如果可用,首先使用列名初始化数据表,如下所示

var table = $("#myTable").DataTable({
    data:[],
    columns: [
                { "data": "Cognome"  },
                { "data": "Nome" },
                { "data": "Sesso" },
                { "data": "data di nascita" }
    ],        
});

in on click of button, do a get ajax call and in done callback clear the table table.clear().draw();在单击按钮时,执行 get ajax 调用并在完成回调中清除表table.clear().draw(); and table.rows.add(result).draw() to render data to the table.table.rows.add(result).draw()将数据呈现到表中。

    $.ajax({
            url: "https://www.mocky.io/v2/5e89289e3100005600d39c17",
            type: "get",
        }).done(function (result) {
            table.clear().draw();
            table.rows.add(result).draw();
         })

JSFiddle: https://jsfiddle.net/k0d1mzgL/ JSFiddle: https://jsfiddle.net/k0d1mzgL/

 var table = $("#myTable").DataTable({ data:[], columns: [ { "data": "Cognome","title": "Cognome"}, { "data": "Nome","title": "Nome"}, { "data": "Sesso","title": "Sesso"}, { "data": "data di nascita","title": "data di nascita" } ], }); $("#getDataBtn").click(function(){ $.ajax({ url: "https://www.mocky.io/v2/5e89289e3100005600d39c17", type: "get", }).done(function (result) { table.clear().draw(); table.rows.add(result).draw(); }).fail(function (jqXHR, textStatus, errorThrown) { //if failed console.log("Due to https issue,this request cant be made, go check jsfiddle link provided in answer"); }); });
 <link href="//cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" > <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script> <table id="myTable"> </table> <button id="getDataBtn">Get data</button>

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

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