1

现在我从 Html 视图中静态显示列名。例如,这些都是我的列 标题:PlatformName、、、、。这些都包含在我的 HTML 视图的表格中。lengthwidthheight

在客户端使用 Datatable 初始化我正在显示

"aoColumns" : [
    "mDataProp" : "PlatformName",
    "mDataProp" : "length "
    "mDataProp" : "width "
    "mDataProp" : "height "
] 

一切对我来说都很好。但是我的新任务是从服务器端添加这些列名(我需要从服务器端处理列名并通过JSON格式将其发送到客户端),以便这些列可以在表格中动态显示。

任何人都可以告诉我如何做到这一点,链接到博客或示例代码将有很大帮助......

4

1 回答 1

2

您需要对服务器进行 AJAX 调用以获取列定义。下面是一个粗略的例子,说明你如何做到这一点。我假设您从服务器返回的数据是 JSON 格式,并且它有一个名为 columns 的属性,该属性与 datatablesaoColumns属性的格式正确。

$(function() {
    //use jQuery AJAX to get column definitions
    $.ajax({
        url: 'path/to/serverside/columns',
        dataType: 'json',
        success: function(data) {
            //once you have column definitions, use them to initialize your table 
            initializeTable(data.columns);
        }
    });
});

//initialize datatables as you were before
function initializeTable(columnsDef) {
    $('#myTable').datatables({
        ...
        aoColumns: columnsDef,
        ...
    });
}
于 2013-05-30T12:16:42.153 回答