什么结构应该在后端返回JSON,以传递每个数据表的列页脚的值?

时间:2014-09-03 15:03:50

标签: javascript jquery json datatables

现在我设法传递要显示的列定义,名称,可见性和数据。

文档中有很多关于如何在客户端站点上计算它的信息,但我想在后端进行。

这是JSON的演示,我在后端返回:https://gist.github.com/andilab/717b73cbfd876ddfeed1

我使用以下js(coffescript)代码获取列和数据的定义:

    $.ajax
        dataType: "json"
        type: "GET"
        url: "/the/url/"
        success: (data) ->
            $("#example").DataTable
                data: data.aaData
                columns: data.aaColumnsDefs

我使用1.10.2

的版本datatables

QUESTION 是后端返回的JSON的正确形式,以便我能够传递每个列页脚的值?

2 个答案:

答案 0 :(得分:0)

您应该使用括号和逗号。 像这样:

$.ajax({
    dataType: "json",
    type: "GET",
    url: "/the/url/",
    data: {field_name:value,...}, // here you passing data to server side
    success: function(data){
        // here write code that treats the data received
    }
});

http://api.jquery.com/jquery.ajax/

答案 1 :(得分:0)

在数据表论坛http://datatables.net/forums/discussion/7326/footer-with-javascript-array-as-source-of-data 上的答案后,似乎无法以相同的方式执行此操作 - 自动方式,例如列。 但是,您可以通过在DOM上定义自己的操作来部分自动化,并使用footerCallback: ()向JSON传递有关页脚的信息。

$.ajax
    dataType: "json"
    type: "GET"
    url: "/the/url/"
    success: (data) ->
        generate_empty_cells = Array(data.footers.length+1).join "<th style='text-align:right'></th>"
        $("#example tfoot tr").append(generate_empty_cells)
        $("#example").DataTable
            data: data.aaData
            columns: data.aaColumnsDefs
            footerCallback: () ->
                api = @api()
                   for th, index in footers
                        $(api.column(index).footer()).html(th)

HTML:

<table id="example">
    <tfoot>
        <tr>
        </tr>
    </tfoot>
</table>

在HTML中我们定义空行,并在JS中根据传递的数组的长度生成适当数量的单元格generate_empty_cells = Array(data.footers.length+1).join "<th style='text-align:right'>。如果您不想在页脚中显示某些列值,只需将null放在数组中的适当位置。

JSON的示例已在相关链接的gist中更新。 请注意,aaDataaaColumnDefsfooters等名称可以是您喜欢的任何名称。