Search code examples
multidimensional-arrayspring-bootthymeleaf

how to use multidimensional array in inline javascript in thymeleaf 3


As per documentation of Datatables.net https://datatables.net/examples/advanced_init/length_menu.html i need to write some setting to data table implementation to my thmyeleaf template,

<script th:inline="javascript">
 $(document).ready(function() {
  $('#example').DataTable( {
     "lengthMenu": [[10, 25, 50, [[${rowTotal}]]], [10, 25, 50, "All"]]
   });
 });
</script>

But on rendering,it's giving me such error.

 org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "10, 25, 50, -1], [10, 25, 50, 'All'" (template: "customer/customerlist" - line 237, col 21)

And on reading this documentation on thymeleaf,it is being said When inlining, if the expression between [[...]] is not a valid Standard Expression, it is output without modification, including the double-brackets. https://github.com/thymeleaf/thymeleaf/issues/22.

How to i solve this problem?


Solution

  • Hmm, I didn't know that was how it works in thymeleaf 2. The easiest change would just be to format it differently. Something like this:

    <script th:inline="javascript">
    $(document).ready(function() {
      $('#example').DataTable({
        "lengthMenu": [
          [10, 25, 50, -1],
          [10, 25, 50, "All"]
        ]
      });
    });
    </script>
    

    If that doesn't suit you, you could split the datatable definition into it's own javascript block and use th:inline="none" or move it to it's own external javascript file. (Why are you using th:inline="javascript" in this case anyways?).