简体   繁体   English

Json对象转换为数据表

[英]Json object converted into a Datatable

Using a server call the user of my web application is creating a json object d. 使用服务器调用,我的Web应用程序的用户正在创建json对象d。 This object contains numerous keys (think of "Mean", "Stdev", etc.). 该对象包含许多键(例如“ Mean”,“ Stdev”等)。 The user is changing the underlying timeseries on a webpage. 用户正在更改网页上的基础时间序列。

    <script type="text/javascript">
        function myFunction(d) {
            $('#myid').html(d["Mean"])
        }
    </script>

An jquery $.POST is executed and if successful returns the json object and calls the method above. 执行jquery $ .POST,如果成功,则返回json对象并调用上述方法。 The above scheme works already. 以上方案已经有效。 This updates 此更新

    <div class="container-fluid">
        <div class="row-fluid">
            <div id="myid">some value</div>
        </div>
    </div>

However, I would really like to update a complete datatable (datatable as in http://datatables.net/examples/advanced_init/ ). 但是,我真的很想更新一个完整的数据表(如http://datatables.net/examples/advanced_init/中的数据表 )。

At runtime I don't know what key are in d. 在运行时,我不知道d中有什么键。 Is datatables.js the right tool for dynamic data in a table? datatables.js是表中动态数据的正确工具吗?

Yep, that is what dataTables.js was made for. 是的,这就是dataTables.js的用途。 Although it is unclear what you mean exactly with datatable , table and dataTable . 尽管不清楚您对datatabletabledataTable确切含义。

dataTable.js is responsible for the display (rendering) of a table like structure on your page with lots of functionality (like sorting, searching, filtering, pagination, editing etc.) that would otherwise be very difficult to achieve. dataTable.js负责在您的页面上显示(呈现)类似表格的表,并具有很多功能(如排序,搜索,过滤,分页,编辑等),否则这些功能将很难实现。 It is not a tool that helps you to maintain the field in a table (or datatable) of your DB. 它不是帮助您维护数据库表(或数据表)中字段的工具。 (But it can be) (但是可以)

dataTable.js uses server sided code with some cleverly helper functions to help you to use a mySql database via PHP. dataTable.js使用服务器端代码以及一些巧妙的辅助函数来帮助您通过PHP使用mySql数据库。 But it does not rely on this examples. 但这并不依赖于此示例。 You can use any serversided code and any database as long as it understands the query that is sent from dataTables.js and resturns the json-data that dataTables.js expects. 您可以使用任何服务器端代码和任何数据库,只要它能够理解从dataTables.js发送的查询并返回dataTables.js期望的json-data。

For complex (DB)-table manipulation you need to write your own server sided code. 对于复杂的(DB)表操作,您需要编写自己的服务器端代码。

Regarding your question I guess you should have a look at the serversided examples that come with dataTables.js. 关于您的问题,我想您应该看看dataTables.js随附的服务器端示例。

For getting individual values of cell or row values on the actual page the user sees you should look at the column rendering examples. 为了使用户看到实际页面上单元格或行值的单个值,用户会看到,您应该查看列渲染示例。

Don't regard this as an Answer, it is just to lengthy for a comment. 不要将其视为答案,这只是冗长的评论。 Be prepared that using dataTables.js is way different from the working code of your question. 准备使用dataTables.js与您的问题的工作代码不同。

Here's a fragment of the code. 这是代码的一部分。 It works actually now. 它现在实际起作用。 However, I find the concept (removing content and adding row-wise) super dodgy: 但是,我发现这个概念(删除内容并逐行添加)超级狡猾:

<div class="col-md-3">
                <table id="example" class="display" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th></th>
                            <th>Value</th>
                        </tr>
                    </thead>
                </table>
            </div>



            <script type=text/javascript>
                $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};

                $TABLE = $('#example').DataTable({
                                "info": false,
                                "bPaginate": false,
                                "paging": false,
                                "bScrollCollapse": false,
                                "search": false,
                                "sort": false,
                                "filter": false}

                );

            </script>

            <script type="text/javascript">
                function myFunction(d)
                {
                    console.log("Refill table");
                    console.log(d);
                    $TABLE.clear();
                    for (var key in d) {
                        $TABLE.row.add([key, d[key]]);
                    }
                    $TABLE.draw();
                }
            </script>

Please note that I have not included the code for the computation of d (done on the server with a $.ajax POST call. 请注意,我尚未包括d的计算代码(在服务器上通过$ .ajax POST调用完成。

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

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