【问题标题】:Datatables generate column and get row id Server-side数据表生成列并获取行 ID 服务器端
【发布时间】:2015-11-23 16:37:50
【问题描述】:

我有一个使用Datatables 服务器端的数据表。我已经创建并填写了表格,如下所示。现在我需要用按钮生成列,但我需要有行的 id ..

我的数据返回:

{"data":[{"id":"13","name":"gerrard","adress":"new york"}, .... }

我想要一些类似的东西,如果我点击这个按钮,它会显示行的 id .. 有我的代码:

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>name</th>
                <th>adress</th>
            </tr>
        </thead>
</table>

$(document).ready(function() {
    var oTable = $('#example').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "server-side-process",
            },

        "columns": [
                    { "data": "name" },
                    { "data": "adress" },
                ]
    } );

} );

我在文档中找到了这个example,但我无法从返回的数据中获取 id .. 请问可以这样做吗?谢谢你..

【问题讨论】:

    标签: ajax datatable datatables server-side datatables-1.10


    【解决方案1】:

    您需要使用render 属性:

    "render": function ( data, type, full) {
                return "<button type='button' class='btn btn-success' data-id=" + full[3] + ">PAY</button>";
        }
    

    full 参数是数据行的完整数据源,因此您可以通过索引访问它。在您的情况下,您将使用full[0],因为它位于第一个位置。

    这是一个working example,它使用不同的数据,但您应该能够看到它是如何工作的。

    【讨论】:

    猜你喜欢
    • 2018-04-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多