【问题标题】:Populating a DataTable with JSON, need to render a hyperlink in one column with a value found in a different JSON field使用 JSON 填充 DataTable,需要在一列中呈现一个超链接,该超链接具有在不同 JSON 字段中找到的值
【发布时间】:2015-11-20 21:07:31
【问题描述】:

我有一个带有数据的示例 json 对象:

[ 
    {
        "id": 1,
        "title": "Fred",
        "author": "Flintstone"
    }, {
        "id": 2,
        "title": "Fred",
        "author": "Flintstone"
    }, {
        "id": 3,
        "title": "Fred",
        "author": "Flintstone"
    }.....

HTML

<table class="table" id="tblRunbook">
                                            <thead>
                                                <tr>
                                                    <th width="60px">ID</th>
                                                    <th width="300px">Title</th>
                                                    <th width="200px">Author</th>
                                                </tr>
                                            </thead>
                                            <tbody>

                                            </tbody>
                                        </table>

JavaScript:

   $(document).ready(function() {
        $('#tblRunbook').dataTable({
            <!-- Retrieve a static json file, you could also have a URL to a controller method. -->
            "sAjaxSource" : "/getRunbooks",
            "sAjaxDataProp": "",
            <!-- Indicate to dataTable what the field names are we want, in the order we want them in the table. -->
            "aoColumns": [
                          {"data": "id"},
                          {"data": "title",
                             "render": function ( data, type, row, meta ) {
                                return '<a href="runbook?rbID=" + data.id + '>' + data + '</a>';}
                          },
                          {"data": "author"}
                ]


        });
    });

ID 的列将被隐藏,但我想创建链接 runbook?rbID = ID。我想访问 JSON 对象中的前一个字段,在本例中为 "data" : "id",并将其设置在函数返回 '&lt;a href="runbook?rbID=" + data.id + '&gt;' 的第二个字段中,其中 data.idID

【问题讨论】:

  • 您可以使用 jQuery map 从无限的其他数据源创建新的数据源,并为数据表使用新模型。数据表只能与一种来源一起使用。但如果它分配的数据可能会很慢,那么在服务器上重做模型可能是一个更好的主意。
  • 我觉得这个datatables.net/forums/discussion/200/…会帮助解决。

标签: javascript jquery html json twitter-bootstrap


【解决方案1】:

我找到了解决办法。

var runbookID;



        "aoColumns": [
                      {"data": function(data, type, row, meta){
                          runbookID = data.id;
                          return data.id;}
                      },
                      {"data": "title",
                         "render": function ( data, type, row, meta ) {
                            return "<a href='runbook?rbID=" + runbookID + "'>" + data + '</a>';}
                      },
                      {"data": "author"}
            ]

【讨论】:

  • 我有同样的问题,这确实有效。但这不是从两个 json 字段制作链接的最佳方式。想知道从那以后你是否学会了另一种方法?谢谢!
猜你喜欢
  • 2016-12-12
  • 2023-03-13
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 2019-11-22
  • 2014-07-23
相关资源
最近更新 更多