I would like to use an autocomplete jquery component in my Thymeleaf template. The autocomplete function of Materializecss front-end framwork looks like this:
$('input.autocomplete').autocomplete({
data: {
"Apple": null,
"Microsoft": null,
"Google": 'http://placehold.it/250x250'
},
limit: 20, // The max amount of results that can be shown at once. Default: Infinity.
onAutocomplete: function(val) {
// Callback function when value is autcompleted.
},
minLength: 1, // The minimum length of the input for the autocomplete to start. Default: 1.
});
As you can see I need a data object including the list of elements. I would like to embed this variable from server side as this list is a dynamic one. As the Thymeleaf documentation says
<script type="text/javascript" th:inline="javascript" th:src="@{/js/example.js}"></script>
Based on the documentation the following example should work:
$('input.autocomplete').autocomplete({
data: [[${companies}]],
limit: 20, // The max amount of results that can be shown at once. Default: Infinity.
onAutocomplete: function(val) {
// Callback function when value is autcompleted.
},
minLength: 1, // The minimum length of the input for the autocomplete to start. Default: 1.
});
The problem is that Thymeleaf does not inline anything in this case. Embedding serverside variables or command objects work fine with Thymeleaf but I can not make it work for javascript. I use Spring Boot 1.5.4, Thymeleaf 3.0.2