简体   繁体   English

从代码而不是.json文件读取和创建表

[英]Read and create table from code not from .json file

I am trying to use this code that reads the json from a file. 我正在尝试使用此代码从文件读取json。 In my code I am creating the json from my database, as you can see below. 在我的代码中,我正在从数据库中创建json,如下所示。 How can I change the code to read the json from my code and not from the file? 如何更改代码以从我的代码而非文件中读取json? In here you can see how the json is being read, in my code you can see how the data is been read to create the table. 这里,您可以看到如何读取json,在我的代码中,您可以看到如何读取数据以创建表。

var columns = ["username", "user_id", "address", "state", "postal_code", "phone", "email"];

var level_classes = {
    "NEW": "new_client",
    "RENEWAL": "renewing_client",
    "CURRENT": "current_client"
};

$(document).ready(function() {
    $.getJSON("obtainUsers.php", function(data) {
        var $table = $('<table style="width: 100%;">');
        var $tbody = $('<tbody>');
        $table.append($tbody);
        var $tr = null;

        data.forEach(function(user, index) {
            if (index % 4 === 0) {
                $tr = $('<tr>');
                $tbody.append($tr);
            }
            $td = $('<td class="' + level_classes[user.level] + '">');
            columns.forEach(function(col) {
                $td.append(user[col]);
                $td.append($('<br>'));
            });
            $tr.append($td);
        });
        $('.runninglist').append($table);
    });
});

Thanks 谢谢

You could do 你可以做

$(document).ready(function() {
    $(".TableWithClass").each(function(){
        var ThisTableHere = $(this);
        $.getJSON($(this).data("url"), function(data) {
            var $table = ThisTableHere;
            .
            .
            Use your other data parameters here
            .
            .
            $('.runninglist').append($table);
    });
});

The only difference here is that you WILL have to wait for that page to execute before the client gets the full HTML, in the other one your page will load and do an ajax call 唯一的区别是您将必须等待该页面执行,然后客户端才能获取完整的HTML,在另一个页面中,您的页面将加载并进行ajax调用

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

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