简体   繁体   English

单击事件将条目填充到jQuery数据表中

[英]Populate Entries to a jQuery Datatable on click event

I'm trying to populate my table with the entries of an Ajax Request. 我正在尝试用Ajax请求的条目填充表。

I can confirm that my Json data has the appropriate format in order to be presented in the Datatable. 我可以确认我的Json数据具有适当的格式,以便可以在数据表中显示。

The problem is that after the request, the entries are not populated even if i'm able to see them in my console. 问题是,在请求之后,即使我能够在控制台中看到这些条目,也不会填充它们。

$(document).ready(function () {

    var $submitButton = $('.ladda-button[type="button"]').ladda();

    var table = $('.table').DataTable({
        processing: true,
        searching: false,
        responsive: true,
        filter: false,
        deferLoading:0,
        serverSide: true,
        bLengthChange: false,
        bSort: false,
        ajax: {
            url: "myUrl",
            type: 'post',
            dataSrc: function (json) {
                $submitButton.ladda('stop');
                var entries = json.data;
                return entries;
            }
        },


    });

    $(document).on('click',$submitButton, function(e){
        e.preventDefault();
        $submitButton.ladda('start');
        table.ajax.reload();
        return false;
    });
});

I think you are reloading the table prematurely. 我认为您正在过早地重新加载表。 Since you're using ajax you only want to reload once your data is available. 由于您使用的是Ajax,因此只希望在数据可用后重新加载。

Try this as a test(reloading after 10 secs) 尝试一下作为测试(10秒后重新加载)

setTimeout( function () {
    table.ajax.reload();
}, 10000 );

I'm not familiar with ladda but maybe its event based and you can modify your code to call reload after the stop event or something similar. 我对ladda并不熟悉,但也许是基于事件的,因此您可以修改代码以在stop事件或类似事件之后调用reload

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

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