0

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

2
  • What do you mean when you say Thymeleaf does not inline anything in this case? Where is the js code going to be? In the template statically? Commented Jun 21, 2017 at 13:38
  • @inoabrian my javascript file is external to the html. Commented Jun 21, 2017 at 18:36

2 Answers 2

1

The th:inline="javascript" only works if your scripts are inline, that is in the HTML template between <script> and </script>.

If you have a separate javascript file and Thymeleaf expressions you want to evaluate in it, you need to process that js file through Thymeleaf separately using the JAVASCRIPT Template Mode.

Sign up to request clarification or add additional context in comments.

1 Comment

It sounds reasonable. Can you tell me how can I do that? I use Spring Boot and it's auto-configuration. The default template mode is HTML. How can I specify different template mode for the .js files? I also do not understand why javascript template mode is not the default for js files
0
$('input.autocomplete').autocomplete({
    data: [($ {
        companies
    })], // use  '(', ')'
    limit: 20,
    onAutocomplete: function(val) {},
    minLength: 1
});

Try the above code. I also experienced that.

1 Comment

data: [ ( ${companies} ) ], /* unescape /, data: [ [ ${companies} ] ], / escape */ , Unescape and Escape Expression are different each other

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.