Ian Nato
Ian Nato

Reputation: 1037

Spring thymleaf how to add th:href="@{}" from jquery datatable

I am trying to add a href link from jquery datatable but the thymleaf th:href="@{}" is not being recognizing when the page is being processed. here is my code.

 $("#categoriesTable").DataTable({

            "Processing":true,
            "ServerSide":true,
            "ajax": {"url": "/ajax/categories", "dataSrc": ""},
            "columns": [
                {"data": "id"},
                {"data": "name"},
                {"data": "description"},
                {"data": "create_at"},
                {"data": null,
                "render": function(data, type, row){
                    return '<a class="btn btn-primary btn-xs"  th:href="@{/category/'+data.id+'/edit}"  >Edit</a>'

                },
                    "targets": -1
                }
            ]
        });

Upvotes: 0

Views: 727

Answers (1)

Venu Duggireddy
Venu Duggireddy

Reputation: 806

You can define javascript variable that will hold the context value and use it when rendering the row

<script th:inline="javascript>
    /*<![CDATA[*/
       var context = [[@{/}]];
    /*]]>*/
</script>

$("#categoriesTable").DataTable({

            "Processing":true,
            "ServerSide":true,
            "ajax": {"url": "/ajax/categories", "dataSrc": ""},
            "columns": [
                {"data": "id"},
                {"data": "name"},
                {"data": "description"},
                {"data": "create_at"},
                {"data": null,
                "render": function(data, type, row){
                    return '<a class="btn btn-primary btn-xs"  href=""+context+"/"+data.id+"/edit">Edit</a>'
                    },
                    "targets": -1
                }
            ]
        });

Upvotes: 1

Related Questions