I am writing a small project for spring with the output of information on the jsp page. Data is displayed using the following script.

<script> $(document).ready(function () { $('#userTable').DataTable({ "lengthMenu": [10, 15, 20, 25, 50], "sAjaxDataProp": "", "sAjaxSource": "/user/all", "order": [[0, "asc"]], "aoColumns": [ {"mData": "id"}, {"mData": "login"}, {"mData": "role"} ] }); }); </script> 

Now it was necessary to add the ability to delete and modify information. Because In JS, I still understand not much, I can’t figure out how to modify the "aoColumns" block: [...]. Rather, he came up with something like this:

 "aoColumns": [ ..., {defaultContent: '<a href="" onclick="editUser(id)>Edit</a> / <a href="" onclick="deleteUser(id)" >Delete</a>'} ] 

And using JS to call the appropriate controller. Or this:

  "aoColumns": [ ..., {defaultContent: '<a href="/user/edit/id" >Edit</a> / <a href="/user/delete/id" >Delete</a>'} ] 

To directly contact the controller. But in any case it is necessary to transfer the record id to the controller.

If the data were filled in, say, through a cycle, then in that case I would write, for example, like this:

 <a href="/user/edit/${id}" >Edit</a> / <a href="/user/delete/${id}" >Delete</a> 

But my table is filled with DataTables and I do not understand how to be with id. Tell me how easy it is to do in this case? Is it possible to add a unique id to the tag or substitute this id in the link when forming the table?

    1 answer 1

    At random I found a solution, not sure that it is the most optimal, but, nevertheless, working. I attach a modified script if someone encounters the same issue.

     <script> $(document).ready(function () { $('#userTable').DataTable({ "lengthMenu": [10, 15, 20, 25, 50], "sAjaxDataProp": "", "sAjaxSource": "/user/all", "order": [[0, "asc"]], "aoColumns": [ { data: "id"}, { data: "login"}, { data: "role"}, { data: null, render: function ( data, type, row ) { var id = data.id; return "<a href =\"/user/edit/" + id + "\" >Edit</a> / <a href=\"/user/delete/" + id + "\" >Delete</a>"; } } ] }); }); </script>