简体   繁体   English

使用javascript和datatables在json中获取数据

[英]Get data in json using javascript and datatables

I have a javascript as below, which can fetch the data from backed in json format. 我有一个如下所示的javascript,它可以从支持json格式的数据中获取数据。 But How can i pass it to another function , ie datatables to populate it 但是我如何将其传递给另一个函数,即数据表来填充它

<script>
var returndata;

$.getJSON("/api/dashboard_data/", success);

function success(data) {
    returndata = data;
    window.alert(returndata);
    return returndata;
    // do something with data, which is an object
}

$(document).ready(function() {
      $('#example').DataTable( {
        data: returndata,
        columns: [
            { title: "Action" },
            { title: "Input" },
            { title: "State" },
            { title: "Completed" },
           { title: "Project" },
        ]
    } );
} );
 </script>

In above code in window.alert(returndata), i get the json data which has been returned from backed. 在window.alert(returndata)中的上述代码中,我得到了从支持返回的json数据。

But the same variable "returndata" when i use it in function ready it is empty. 但是当我在函数准备中使用它时,相同的变量“ returndata”为空。 How can i get it in ready function. 我如何在就绪功能中获取它。

You are calling two asynchronous functions here. 您在这里调用两个异步函数。 $.getJSON() and $(document).ready() . $.getJSON()$(document).ready() It looks that ready() is faster than getJSON() which means returndata is empty when you try to fill your data table. 看起来ready()getJSON()更快,这意味着当您尝试填充数据表时returndata为空。

Try this to make sure you have always the correct order: 尝试执行此操作以确保您的订单始终正确:

<script>

$(document).ready(function() {
      $.getJSON("/api/dashboard_data/", function(returndata) {
           $('#example').DataTable( {
               data: returndata,
               columns: [
                    { title: "Action" },
                    { title: "Input" },
                    { title: "State" },
                    { title: "Completed" },
                    { title: "Project" },
               ]
           });
      }); 
});
</script>

Firstly, which jQuery plugin are you using for DataTables? 首先,您将哪个jQuery插件用于DataTables? Is it this one ? 它是这一个 The first thing I would do would be to put everything inside the $document.ready() as the documentation demonstrates. 我要做的第一件事是将所有内容放入$document.ready()如文档所示。 This ensures that all your code executes after the DOM is ready. 这样可以确保在DOM准备好后所有代码都可以执行。 Let me know what happens after that. 让我知道那之后会发生什么。

Also this part of the documentation could help if you are using the DataTables API. 此外,如果您正在使用DataTables API,则部分文档也可能会有所帮助。 It could be as simple as a misspelling depending on what you're trying to do as quoted from the docs here: 它可能像拼写错误一样简单,具体取决于您要执行的操作,如此处的文档所述:

The result from each is an instance of the DataTables API object which has the tables found by the selector in its context. 每个对象的结果都是DataTables API对象的实例,该对象具有选择器在其上下文中找到的表。 It is important to note the difference between $( selector ).DataTable() and $( selector ).dataTable() . 重要的是要注意$( selector ).DataTable()$( selector ).dataTable()之间的区别。 The former returns a DataTables API instance, while the latter returns a jQuery object. 前者返回DataTables API实例,而后者返回jQuery对象。

I know this is not a good solution, just a hack. 我知道这不是一个好的解决方案,只是一个hack。 You can use window.setInterval or window.setTimeout function to check for data and execute the required function. 您可以使用window.setInterval或window.setTimeout函数来检查数据并执行所需的函数。 Don't forget to clear the interval. 不要忘记清除间隔。

Follow the Datatables documentation: https://datatables.net/examples/server_side/simple.html 请遵循Datatables文档: https : //datatables.net/examples/server_side/simple.html

You'll have to do something like this: 您将必须执行以下操作:

$('#example').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": _getData(),
    "columns": [
        {title: "Action"},
        {title: "Input"},
        {title: "State"},
        {title: "Completed"},
        {title: "Project"}
    ]
});

function _getData(data, callback) {
    $.getJSON("/api/dashboard_data/", success);
    function success(data) {
    // you'll probably want to get recordsTotal & recordsFiltered from your server
        callback({
            recordsTotal: 57,
            recordsFiltered: 57,
            data: data 
        })
    }
}

I haven't tested this code, but this should guide you in the right direction :) 我尚未测试此代码,但这应该可以指导您正确的方向:)

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

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