javascript - 如何将自定义 url 添加到数据表

标签 javascript spring-boot datatables thymeleaf

我从使用 thymeleaf 表转变为在代码中使用数据表。我一直在尝试找到一种方法来将按钮添加到表行中,以便它与该行的 id 相关。例如在 thymeleaf 中我使用

<a th:href="@{'/flash/' + ${flash.flashcard_id}}"><button title="View" data-toggle="tooltip">&#xE417;</button></a>

但我不确定如何在数据表中实现这一点。

我尝试过使用

{ "mData": "<button>Test</button>"}

{ "mData": null, "bSortable": false, "mRender": function (o) { return '<a href=#/' + o.flashcard_id+ '>' + 'View' + '</a>'; }}

但我可能没有正确使用它们,因为我对数据表了解不多。

以前的 Thymeleaf 代码

<table class="table table-striped">
      <tr>
        <th>Jobname</th>
        <th>Flashcard Name</th>
        <th>Barcode Value</th>
        <th>Display Text</th>
        <th>Active</th>
        <th>Actions</th>
      </tr>
      <tr th:each="flash : ${flash}">
        <td th:text="${flash.jobName}"></td>
        <td th:text="${flash.flashcard_Name}"></td>
        <td th:text="${flash.barcode_Value}"></td>
        <td th:text="${flash.display_Text}"></td>
        <td th:text="${flash.active}"></td>
        <td>
            <a th:href="@{'/flash/' + ${flash.flashcard_id}}"><button title="View" data-toggle="tooltip">&#xE417;</button></a>
            <a th:href="@{'/flash/' + ${flash.flashcard_id}} + '/update'"><button title="Update" data-toggle="tooltip">&#xE417;</button></a>
            <a th:href="@{'/flash/' + ${flash.flashcard_id}} + '/deactivate'"><button title="Deactivate" data-toggle="tooltip">&#xE417;</button></a>
            <a th:href="@{'/flash/' + ${flash.flashcard_id}} + '/activate'"><button title="Activate" data-toggle="tooltip">&#xE417;</button></a>        
            <a th:href="@{'/flash/' + ${flash.flashcard_id}} + '/delete'"><button title="Delete" data-toggle="tooltip">&#xE417;</button></a>      
        </td>
      </tr>
    </table>

当前数据表代码

<table id="flashTable" class="display">
     <thead>
         <tr>
            <th>Jobname</th>
            <th>Flashcard</th>
            <th>Barcode</th>
            <th>Display</th>
            <th>Active</th>
            <th>Actions</th>
        </tr>
    </thead>
</table>

<script>
      $(document).ready( function () {
             var table = $('#flashTable').DataTable({
                    "sAjaxSource": "/flashs",
                    "sAjaxDataProp": "",
                    "aoColumns": [
                        { "mData": "jobName"},
                        { "mData": "flashcard_Name" },
                        { "mData": "barcode_Value" },
                        { "mData": "display_Text" },
                        { "mData": "active" },
                        { "mData": "<button>Test</button>"}
                    ]
             })
        });
</script>

我正在寻找一种方法,将抽认卡的 id 和部分 url 结合起来,以便在按下相应按钮时转到网站的不同位置。它在 thymeleaf 中运行良好,只是不确定如何将其转换为数据表。任何帮助都会很棒。

最佳答案

您使用的是数据表的旧版本,因此这可能在语法上略有偏差,但您可以使用 ColumnDefs 来覆盖渲染行为。这允许您指定目标列(要应用渲染函数的列的索引)和渲染函数,该函数具有 dataField 的参数(在 aoColumn 的 mData 部分中指定)以及完整对象(第三个参数)。请参阅https://legacy.datatables.net/usage/columns了解更多信息

<table id="flashTable" class="display">
     <thead>
         <tr>
            <th>Jobname</th>
            <th>Flashcard</th>
            <th>Barcode</th>
            <th>Display</th>
            <th>Active</th>
            <th>Actions</th>
        </tr>
    </thead>
</table>

<script>
      $(document).ready( function () {
             var table = $('#flashTable').DataTable({
                    "sAjaxSource": "/flashs",
                    "sAjaxDataProp": "",
                    "aoColumns": [
                        { "mData": "jobName"},
                        { "mData": "flashcard_Name" },
                        { "mData": "barcode_Value" },
                        { "mData": "display_Text" },
                        { "mData": "active" },
                        { "mData": "flashcard_id"}
                    ],
                    "aoColumnDefs": [
                        {
                            "aTargets" : [5],
                            "mRender": function(dataField, callType, fullRowObj) {
                                return '<a href=#/' + dataField + '>' + 'View ' + fullRowObj.flashcard_Name + '</a>';
                            }
                        }
                    ]
             })
        });
</script>

关于javascript - 如何将自定义 url 添加到数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57479049/

相关文章:

javascript - 在异步函数内部,回调函数的返回值返回 Promise(undefined)

eclipse - 如何避免 eclipse 上的远程系统资源管理器操作?

css - 无法在数据表附近添加侧边栏

jquery - Bootstrap 数据表表不一样

javascript - 对所有用户使用 Office 365 身份验证

javascript - 在同一个异步函数/定时器重置中使用多个setTimeout

spring - Spring Kafka 和 Spring Integration Kafka 的区别

jquery - 数据表内联问题,我想在一行中显示所有操作。请看图片

javascript - 未捕获的类型错误 : Cannot call method 'on' of undefined - Backbone. js

java - Spring Data Elasticsearch 不会隐式地从注解创建映射