如何将 jQuery 数据表导入 Grails 2.4.4

How to import jQuery DataTables into Grails 2.4.4

我有一个 Grails 2.4.4 应用程序,我正在尝试实现一个利用 jQuery DataTable. I see there is an old DataTable plugin 的 GSP,但它看起来没有维护并且与 Grails 2.x 不兼容。更不用说,应该 是一种在 Grails 中简单地包含 any JS 库而无需明确要求插件的方法。

这是我的 BuildConfigplugins 部分:

plugins {
    build ":release:3.0.1"
    build ":tomcat:7.0.52.1"

    compile "org.grails.plugins:gson:1.1.4"
    compile ":rest-client-builder:1.0.3"
    compile ":yammer-metrics:3.0.1-2"

    runtime ":jquery:1.11.1"
    runtime ":cached-resources:1.0"
    runtime ":resources:1.2.14"
    compile ":cache-headers:1.1.7"

    compile ":rest-client-builder:1.0.3"
    compile ":export:1.6"
    compile ":standalone:1.2.3"
    compile ":cache-headers:1.1.7"
    compile ":scaffolding:2.1.2"
    compile ':cache:1.1.3'

    runtime ":resources:1.2.14"
    runtime ":hibernate:3.6.10.15"
    runtime ":database-migration:1.4.0"
    runtime ":jquery:1.11.1"
}

由于超出此问题范围的原因,我无法 删除或更改 plugins 部分中的任何现有声明,但我可以添加它们。我听说 "assets pipeline" 是一种将 JS 库添加到 Grails 应用程序的新酷方法,但我能找到的有关此管道的所有文献都是含糊不清的。而且我找不到任何使用此管道将 DataTables 包含在 Grails 应用程序中的真实世界具体示例。

DataTable 的“Hello World!”版本似乎是这样的:

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
        </tr>

        ...lots of more rows
    </tbody>
</table>

$(document).ready(function() {
    $('#example').DataTable();
} );

所以我问:如何在 GSP 中获取(上图)"Hello World" DataTable 运行? 具体 配置、插件等。我需要连接才能使它工作吗?

如果您的 DataTable js 文件保存在此目录中 web-app\js,您可以在需要 DataTable 的视图中使用此 Grails 标记。

<g:javascript src="jquery.datatables.min.js" />

同样,您可以获得所需的 css 文件

<g:external dir="css" file="jquery.datatables.css" />

加载所需文件后,您可以调用 jQuery 函数

<g:javascript>
    $(document).ready(function() {
        $('#example').DataTable();
    } );
</g:javascript>