【问题标题】:Display Column name from server side,Datatable jquery从服务器端显示列名,数据表 jquery
【发布时间】:2013-05-30 12:00:20
【问题描述】:

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

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

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

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

谁能告诉我如何做到这一点,链接到博客或示例代码会很有帮助...

【问题讨论】:

    标签: jquery asp.net json jquery-datatables


    【解决方案1】:

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

    $(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,
            ...
        });
    }
    

    【讨论】:

    • @santhoshpatil 如果我需要在 mvc 视图中呈现不同数量的列,我们如何定义列?
    • 嘿@Zane,很高兴这篇文章对你有帮助。如果您在某事上需要帮助,最好发布一个新问题,这样更多人会看到它,您会更快地获得一些帮助:) 另外,有相同问题的人将能够更轻松地找到它。祝你好运!
    • 我终于解决了这个问题......你的回答很有帮助。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 2014-09-06
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多