【问题标题】:DataTables Dynamic Column headers数据表动态列标题
【发布时间】:2014-01-18 06:51:22
【问题描述】:

我在 Struts2 中使用带有服务器端实现的 Datatables 库。 我能够使用 Ajax 调用获取表格内容。但是标题是固定的。有什么方法可以从服务器端加载标题标签,就像内容一样。

<div id="container">
    <div id="demo">
        <table id="example" class="display">
            <thead>
                <tr>
                    <th width="80px">Address</th>
                    <th>Name</th>
                    <th width="15%">Town</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td colspan="5" class="dataTables_empty">Loading data from
                        server</td>
                </tr>
            </tbody>
            <tfoot>
            </tfoot>
        </table>
    </div>

【问题讨论】:

  • 这是 jQuery dataTable 插件吗?
  • @dcodesmith 是的,一样。 datatables.net
  • 列标题是否总是相同的?
  • 不,每次它从不同的表中填充数据。因此需要不同的标题。在示例中,它是硬编码的。我想以与服务器端处理数据(ajax 调用)相同的方式设置标头。
  • 好的,这是可能的,但是您每次都要从头开始创建表。而且您还必须传入列标题。这有意义吗?

标签: jquery struts2 datatables jquery-datatables


【解决方案1】:

您可以使用&lt;s:if&gt; 标签在&lt;thead&gt;标签内添加条件,具体取决于您的操作。例如,如果您的操作是 sample1.action 并且有 firstname 和 lastname 标题,而 sample2.action 有 country 和 nationality 标题。

这是一个示例代码

<thead>
        <s:if test="#context['struts.actionMapping'].name=='sample1.action'">
            <tr>
                    <th>FirstName</th>
                    <th >Lastname</th>
                </tr>
         </s:if>
        <s:elseif test="#context['struts.actionMapping'].name=='sample2.action'">
            <tr>
                    <th>Country</th>
                    <th >Nationality</th>
                </tr>
         </s:elseif>
</thead>

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 2011-05-15
    • 2018-05-03
    相关资源
    最近更新 更多